Search code examples
iosswiftfacebook-loginfacebook-sdk-4.0

Facebook Login Swift don't works On Device (But do on IOS Simulator)


I configure in my app authentication with Facebook Login in Swift 2.

Everything works perfectly on IOS Simulator (IOS 8 and 9): User click on "Login with Facebook", then a safari page opening, then authentication continue and finally user is logged correctly:

enter image description here

But when I try on my Ipod Touch, it ALWAYS block and fail on this message (Sorry, something went wrong. We're working on it and we'll get it fixed as soon as we can), and my user is not logged:

enter image description here

I have tried a lot of things :

  1. Change Facebook SDK (I have tried to 4.0 Facebook SDK)
  2. Upgrade my ipod touch on IOS 8.4, next on IOS 9.
  3. Downgrade Xcode 7beta to XCode 6
  4. Remove Facebook App on Ipod Touch and Reinstall it

But still not working.. Any help would be appreciated!

Here is my code:

AppDelegate.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        //For Facebook Login
        return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)

    }

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
        return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
    }

ViewController.swift

class ViewController: UIViewController, FBSDKLoginButtonDelegate {

    @IBOutlet weak var signInButtonFB: FBSDKLoginButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        //Configure Facebook
        signInButtonFB.readPermissions = ["public_profile", "email", "user_friends"]
        signInButtonFB.delegate = self


        //Configure Facebook SignIn
        if (FBSDKAccessToken.currentAccessToken() != nil)
        {
            print("Logged in !")

        }
        else
        {
            print("not logged")

        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //MARK: Facebook
    func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {

        print("Début login Button")
        if ((error) == nil)
        {
            print("Login Complete")

            returnUserData()
        }
        else {
            print ("Voici l'erreur:"+error.localizedDescription)
        }
    }

    func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
        print("User Logged Out")
    }

Solution

  • Looks like my app Facebook was still in development mode.. I forget to set "Contact Name" and allow my app to public access.

    SETUP FOR FACEBOOK APP:

    1. Go to : https://developers.facebook.com/apps/
    2. My Apps -> Settings -> Basic -> Contact Email = Give email valid

    3. My Apps -> Status and Review Tab -> "Do you want to make this app and all its live features available to the general public?" = Yes