1

I added FBSDK 4.7 to my swift app and when it opens the safari view controller to login, and I try to tap "Done" without inputting anything into the email/password fields, the app breaks. It spits out "fatal error: unexpectedly found nil while unwrapping an Optional value"

How do I handle this error?

Current login code:

if let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager(){
fbLoginManager.logInWithReadPermissions(["email"],fromViewController:self,
handler:{(result:FBSDKLoginManagerLoginResult!,error:NSError!) -> Void in
if (error == nil){
            let fbloginresult : FBSDKLoginManagerLoginResult = result
            if(fbloginresult.grantedPermissions.contains("email"))
            {
                print("Logged in successfully")
                self.getFBUserData()
                //fbLoginManager.logOut()
            }
        }
        else{
            print("HELPPPPPPPPPP")
        }   
     })
   }
4

1 回答 1

0

无需分配变量的类型(result, error)

这是使用 Facebook 4.7 SDK 的工作代码 | 斯威夫特 2 | Xcode 7.

@IBAction func btnFBLoginPressed(sender: AnyObject) {
    let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
    fbLoginManager.loginBehavior = FBSDKLoginBehavior.Web
    fbLoginManager.logInWithReadPermissions(["email"], fromViewController: self, handler: { (result, error) -> Void in
        if (error == nil){
            let fbloginresult : FBSDKLoginManagerLoginResult = result
            if fbloginresult.grantedPermissions != nil { //Here we have to check that the set contains value or not
                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"]).startWithCompletionHandler({ (connection, result, error) -> Void in
            if (error == nil){
                print(result)
            }
        })
    }
}

我们必须检查grantedPermissions'sSet是否nil

if fbloginresult.grantedPermissions != nil { //Here we have to check that the set contains value or not
于 2015-10-19T06:40:21.580 回答