简体   繁体   中英

iOS Facebook login button with swift

Just wondering what i'm doing wrong with the code below, the code inside the block facebookLogin.login(withReadPermissions:) never gets called.

  @IBAction func fbBtnPressed(sender: UIButton!) {
    let facebookLogin =  FBSDKLoginManager()

    facebookLogin.logIn(withReadPermissions: [ "email" ], from: self) { (facebookResult: FBSDKLoginManagerLoginResult?, facebookError:NSError?) in

        if facebookError != nil {
            print("Facebook login failed. Error \(facebookError)")
        } else {
            let accessToken = FBSDKAccessToken.current().tokenString
            print("Successfully logged in with facebook. \(accessToken)")
        }
    }

在此处输入图片说明

在此处输入图片说明

It's possible that facebookLogin is being deallocated when it goes out of scope at the end of the function call, before it has had time to complete.

Try move let facebookLogin = FBSDKLoginManager() into the containing (class) scope:

let facebookLogin =  FBSDKLoginManager()

@IBAction func fbBtnPressed(sender: UIButton!) {

    facebookLogin.logIn(withReadPermissions: [ "email" ], from: self) { (facebookResult: FBSDKLoginManagerLoginResult?, facebookError:NSError?) in

        // ...    
    }
}

Are you sure that the token existed when you ask for the permission? As stated in Facebook Login SDK ,

You should check the for availability of an existing token before you call the loginManager.

Else you would need to set the permission to your login button with self.facebookLogInButton.readPermissions = ["email"] as stated at the permission page I linked

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM