IOS Facebook SDK: login doesn't return email despite permissions granted

后端 未结 8 2080
[愿得一人]
[愿得一人] 2020-12-10 12:46

I\'m trying to get information through the facebook sdk but so far I\'m getting only the id and the name of the user, although I\'m sure there is an email available, as it i

8条回答
  •  自闭症患者
    2020-12-10 13:35

    Tested in Swift 2.2

    func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
            print("User Logged In")
    if ((error) != nil)
            {
                // Process error
            }
            else if result.isCancelled {
                // Handle cancellations
            }
            else {
                // If you ask for multiple permissions at once, you
                // should check if specific permissions missing
                if result.grantedPermissions.contains("email")
                {
                    // Do work
                    getUserData()
                }
            }}
    
    func getUserData()
    {
        if ((FBSDKAccessToken.currentAccessToken()) != nil)
        {
            let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "email, name"])
            graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
    
                if ((error) != nil)
                {
                    // Process error
                    print("Error: \(error)")
                }
                else
                {
                    print("fetched user: \(result)")
                    let userName : NSString = result.valueForKey("name") as! NSString
                    print("User Name is: \(userName)")
                    let userEmail : NSString = result.valueForKey("email") as! NSString
                    print("User Email is: \(userEmail)")
    
                }
            })
        }
    }
    

提交回复
热议问题