Swift FBSDK issue - Facebook ID is nil for some users
<p>I have spent ages trying to figure out why for maybe 5% of users that download my app cannot login with Facebook successfully. </p>

<p>95% of the time it performs as expected all all is right with the world.</p>

<p>But for the other 5% the app crashes on the 1st ViewController page. Then when they relaunch the app the are sent successfully to the 2nd page to check to see if they already exist in the database, but the Facebook id is nil and the error dialogue is displayed.</p>

<p>I cannot debug it as my device along with the vast majority don't seem to have this error. </p>

<p>If anyone could suggest where the problem might be I would be eternally grateful! </p>

<p>1st Page View Controller:</p>

<pre><code>import UIKit

class ViewController: UIViewController {




@IBAction func btnFBLoginPressed(sender: AnyObject) {



    let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()



    fbLoginManager .logInWithReadPermissions(["email"], fromViewController: self, handler: { (result, error) -&gt; Void in
        if (error == nil){

            let fbloginresult : FBSDKLoginManagerLoginResult = result
            if result.isCancelled {
                return
            }

            if(fbloginresult.grantedPermissions.contains("email"))
            {
                self.getFBUserData()

              //fbLoginManager.logOut()
            }
        }
    })
}

func getFBUserData(){
    if((FBSDKAccessToken.currentAccessToken()) != nil){
        FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email, gender, age_range, locale"]).startWithCompletionHandler({ (connection, result, error) -&gt; Void in
            if (error == nil){


                let userName : NSString = result.valueForKey("name") as! NSString
                let facebookID : NSString = result.valueForKey("id") as! NSString
                let firstName : NSString = result.valueForKey("first_name") as! NSString
                let lastName : NSString = result.valueForKey("last_name") as! NSString
                let email : NSString = result.valueForKey("email") as! NSString
                let gender : NSString = result.valueForKey("gender") as! NSString


                let locale : NSString = result.valueForKey("locale") as! NSString


                let defaults = NSUserDefaults.standardUserDefaults()

                defaults.setObject("\(userName)", forKey: "userName")
                defaults.setObject("\(facebookID)", forKey: "facebookID")
                defaults.setObject("\(firstName)", forKey: "firstName")
                defaults.setObject("\(lastName)", forKey: "lastName")
                defaults.setObject("\(email)", forKey: "email")


                defaults.setObject("\(gender)", forKey: "gender")



                defaults.setObject("\(locale)", forKey: "locale")








            }
        })
    }
}


override func viewDidAppear(animated: Bool) {




      if((FBSDKAccessToken.currentAccessToken()) != nil){

        print("logged in")

    self.performSegueWithIdentifier("login",sender:self)



      }else{

        print("Not logged in")

    }

}


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


    // let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()

    //  fbLoginManager.logOut()


}

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


}
</code></pre>

<p>Second Page after login segue:</p>

<pre><code>import Foundation

import UIKit
import Parse
import Bolts

class loginViewController: UIViewController {


override func viewDidAppear(animated: Bool)
{



    let defaults = NSUserDefaults.standardUserDefaults()

    if let facebookID = defaults.stringForKey("facebookID"){

        let firstName = defaults.stringForKey("firstName")
        let lastName = defaults.stringForKey("lastName")
        let email = defaults.stringForKey("email")
        let gender = defaults.stringForKey("gender")


       // Lots of code that queries database 



    } else{


        let alert = UIAlertView()
        alert.title = "Error"
        alert.message = "Error Code: 01"
        alert.addButtonWithTitle("OK")
        alert.show()


            let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()

            fbLoginManager.logOut()

            self.performSegueWithIdentifier("backToLoginScreen", sender: nil)




    }


        }




}
</code></pre>