Peter Pik Peter Pik - 1 month ago 14
Swift Question

Custom Facebook loginButton functions Swift 3

I'm trying to create a custom loginButton, which I've found a lot of guides for. However, none of them were updated for Swift 3, so I've tried myself to create the functions. however, I keep getting errors.

The first I'm getting

ambiguous reference to member login(_:completion:)


@IBAction func LoginPressed(_ sender: AnyObject) {
let fbLoginManager : LoginManager = LoginManager()
fbLoginManager.logIn(["public_profile", "email", "user_friends"], viewController: self, completion: { (result, error) -> Void in

if (error == nil){
let fbloginresult : FBSDKLoginManagerLoginResult = result!
if fbloginresult.grantedPermissions != nil {
if(fbloginresult.grantedPermissions.contains("email"))
{
self.getFBUserData()
fbLoginManager.logOut()
}
}
}

})

}


The second I'm getting
Cannot convert value of type (_, _, _) -> Void to expected argument type (HTTPURLResponse?, GraphRequestResult<GraphRequest> -> Void)


func getFBUserData(){
if((AccessToken.current) != nil){
GraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start({ (connection, result, error) -> Void in

if (error == nil){
//everything works print the user data
print(result)
}
})
}
}

Answer

The solution to your first problem is the following: No need to add the completion: word to the end of the function. Your function should look like this:

fbLoginManager.logIn(["public_profile", "email", "user_friends"], viewController: self) { result in
    // handle the result
}

Make sure you delete the extra ) from the end of the call.

The second problem has two parts. You need to update the completion handler syntax, plus one of the parameters has been removed. Your function should look like the following:

GraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start { (urlResponse, requestResult) in    
     if (error == nil) {
       //everything works print the user data
       print(result)
     }
}

Also, this tutorial by Facebook seems very promising!

Also, here is the link for the SDK on github, where you can take a look on the Samples project for examples.

Comments