Xcode 8, swift 3, Facebook SDK login, using LoginManager, double-click to open the FB login form

I use the function below to login to Facebook, but I always need to double-click the "Login" button to open the Facebook login page.

I already tested on the simulator and device. The first time I press the login button, the application goes to the loginManager.logIn (...) function, but never gets into the completion handler until the second click.

Does anyone have this problem and solution? I am using xCode 8 and Swift 3

private func loginWithFB(){
    let loginManager = LoginManager()
    loginManager.logIn([ .publicProfile, .userFriends, .email ], viewController: self) { loginResult in
        switch loginResult {
        case .failed(let error):
            print(error)
        case .cancelled:
            print("User cancelled login.")
        case .success(let grantedPermissions, let declinedPermissions, let accessToken):

        }
    }
}
+4
source share
4 answers

This code works for me. The completion handler is called each time the button is clicked.

Xcode - 8.0

FBSDK - 4.15.1

@IBAction func loginTest(_ sender: UIButton) {
        let loginManager = FBSDKLoginManager()
        loginManager.logIn(withReadPermissions:["public_profile","user_friends","email"], from: self) {
            loginResult,error in
            print("completion handler called")
        }
    }
+2

appDelegate?

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool
{

    return SDKApplicationDelegate.shared.application(app, open: url, options: options)

}
+2

I have the same problem, I just initialize Facebooklogin in the didFinishLaunching file.

Here is the code that works for me:

func application(_ application: UIApplication,  didFinishLaunchingWithOptions launchOptions:    [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
   FBSDKLoginManager.initialize()  // Initialize facebook login
   return true
}
+1
source

just use:

let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
        fbLoginManager.logIn(withReadPermissions: ["email"], from: self) { (result, error) in
            if (error == nil){
                let fbloginresult : FBSDKLoginManagerLoginResult = result!
                if fbloginresult.grantedPermissions != nil {
                    if(fbloginresult.grantedPermissions.contains("email"))
                    {
                        self.getFBUserData()
                        fbLoginManager.logOut()
                    }
                }
            }
        }

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){
                    self.dict = result as! [String : AnyObject]
                    print(result!)
                    print(self.dict)
                }
            })
        }
    }
0
source

Source: https://habr.com/ru/post/1657122/


All Articles