Facebook SDK 4.7 登录错误处理

Facebook SDK 4.7 Login error handling

我将 FBSDK 4.7 添加到我的 swift 应用程序中,当它打开 Safari 视图控制器进行登录时,我尝试点击 "Done" 而不在 email/password 字段中输入任何内容,该应用程序中断。吐出来"fatal error: unexpectedly found nil while unwrapping an Optional value"

如何处理这个错误?

当前登录码:

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")
        }   
     })
   }

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

这是使用 Facebook 4.7 SDK 的工作代码 | Swift2 | Xcode7.

@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)
            }
        })
    }
}

我们必须检查 grantedPermissionsSet 是否为 nil

if fbloginresult.grantedPermissions != nil { //Here we have to check that the set contains value or not