Facebook SDK 4.7登录错误处理

时间:2015-10-19 06:36:25

标签: ios xcode facebook swift fbsdk

我在我的swift应用程序中添加了FBSDK 4.7,当它打开safari视图控制器登录时,我尝试点击"完成"没有在电子邮件/密码字段中输入任何内容,应用程序中断。它吐出"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")
        }   
     })
   }

1 个答案:

答案 0 :(得分: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 Set是否nil

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