在登录Facebook FBSDK输入错误代码:Swift 4.2中的308

时间:2019-01-05 06:17:33

标签: ios swift facebook fbsdk swift4.2

我在Swift中收到FBSDK输入错误代码:308 点击Facebook登录后,点击>>继续浏览后继续出现错误,并没有任何反应。

我已经解决了这个问题: 当我点击继续一段时间后,我已经有了访问令牌,通过它输入错误。

@objc func facebookTouchUpInside() {
    facebookButtonView.backgroundColor = UIColor.clear
    facebookLabel.textColor = UIColor.white
    facebookIconImageView.image = UIImage(named: "facebook_white")

    let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
    fbLoginManager.logIn(withReadPermissions: ["public_profile", "email"], from: self) { (result, error) -> Void in
        if (error == nil){
            let fbloginresult : FBSDKLoginManagerLoginResult = result!
            if (result?.isCancelled)!{
                return
            }
            if(fbloginresult.grantedPermissions.contains("email")) {
                self.getFBUserData()
            }
        }
    }
}

因此,在登录之前,我检查是否已经拥有访问令牌(如果是),那么登录后我将执行操作 如果访问令牌为nil,则执行fblogin。 登录后执行操作后,从facebook sdk注销。

1 个答案:

答案 0 :(得分:0)

@objc func facebookTouchUpInside() {
    facebookButtonView.backgroundColor = UIColor.clear
    facebookLabel.textColor = UIColor.white
    facebookIconImageView.image = UIImage(named: "facebook_white")

    **guard FBSDKAccessToken.current() == nil else{
        self.getFBUserData()
        return
    }**

    let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
    fbLoginManager.logIn(withReadPermissions: ["public_profile", "email"], from: self) { (result, error) -> Void in
        if (error == nil){
            let fbloginresult : FBSDKLoginManagerLoginResult = result!
            if (result?.isCancelled)!{
                return
            }
            if(fbloginresult.grantedPermissions.contains("email")) {
                self.getFBUserData()
            }
        }
    }
}

如果我已经有了访问令牌(如果是的话),那么登录后我将执行操作

func getFBUserData(){
    if((FBSDKAccessToken.current()) != nil){
        FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in
            if (error == nil){
                print(result as! [String: Any])
                if let fbObject = result as? [String: Any] {
                    if let email = fbObject["email"] as? String {
                        var loginObject = [
                            "email": email,
                            "first_name": fbObject["first_name"] as! String,
                            "last_name": fbObject["last_name"] as! String,
                            "fb_id": fbObject["id"] as! String,
                            "device_type": "iPhone",
                            ] as [String : AnyObject]
                        if let picutureDict = fbObject["picture"] as? [String: AnyObject],
                        let dataDict = picutureDict["data"] as? [String: AnyObject],
                            let isSilhouetter = dataDict["is_silhouette"] as? Int, isSilhouetter == 0,
                            let url = dataDict["url"] as? String {
                            loginObject["url"] = url as AnyObject
                        }
                        print(loginObject)
                        let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
                        fbLoginManager.logOut()

                        DispatchQueue.main.async {
                            self.checkEmailID(email, fbDict: loginObject)
                        }
                    } else {
                        self.showOopsSimpleAlertWithMessage("An email ID is not associated with this account.".localized)
                    }
                } else {
                    self.showOopsSimpleAlertWithMessage("Failed while trying to login with Facebook".localized)
                }
            }
        })
    }
}

登录后执行操作后,从facebook sdk注销。