Swift ios facebook connects to application if user cancels login

I use the following code to login to facebook:

@IBAction func fbLoginBtnDidTouch(sender: AnyObject) { let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager() fbLoginManager.logInWithReadPermissions(["email"], fromViewController: self) { (result, error) -> Void in if (error == nil){ let fbloginresult : FBSDKLoginManagerLoginResult = result if(fbloginresult.grantedPermissions.contains("email")) { self.getFBUserData() } } } } 

AppDelegate:

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 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) } 

This will launch FB in web browsing. If the user accepts the application from the FB account, he will return to the application as expected.

But if the user clicks NO or clicks Finish in the web browser using FB, the application will crash (fatal error: zero was unexpectedly found when deploying an optional value)

Marking this line:

 if(fbloginresult.grantedPermissions.contains("email")) 

So, why does the application crash if the user clicks the No or Finish button?

+5
source share
1 answer

First check if the user is canceled. The application crashed because there were no permissions granted. FBSDKLoginManagerLoginResult

 if (error == nil){ let fbloginresult : FBSDKLoginManagerLoginResult = result if result.isCancelled { return } if(fbloginresult.grantedPermissions.contains("email")) { self.getFBUserData() } } 
+20
source

Source: https://habr.com/ru/post/1246361/


All Articles