简体   繁体   中英

Facebook Login iOS 10 works until button is pressed for a second time

I've been creating a custom button for Facebook Login on iOS 10 using Swift 3. Unfortunately Facebook login won't show the SafariViewController until I press the UIButton for the second time. Thanks to breakpoints I know it calls loginManager.logIn() but it never enters the callback until the second time I press the UIButton. Although if I use a FacebookLoginButton() alongside my custom everything works. I really have no idea what's the problem. Hope you can help me. Thanks!

Here's my ViewController:

import UIKit
import FacebookCore
import FacebookLogin

class ViewController: UIViewController {

override func viewDidLoad() {

    self.view.backgroundColor = .blue

    // Add a custom login button to your app
    let myLoginButton = UIButton(type: .system)
    myLoginButton.backgroundColor = .red
    myLoginButton.frame = CGRect(x: 0, y: 0, width: 180, height: 40)
    myLoginButton.center = view.center;
    myLoginButton.setTitle("My Button", for: .normal)

    // Handle clicks on the button
    myLoginButton.addTarget(self, action: #selector(self.loginButtonClicked), for: .touchUpInside)

    // Add the button to the view
    view.addSubview(myLoginButton)
}

// Once the button is clicked, show the login dialog
func loginButtonClicked() {
    let loginManager = LoginManager()
    loginManager.logIn([.publicProfile], 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):
            print("Logged in!")
        }
    }
}

And my info.plist (The only changes are the placeholders for my app's name and id:

 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
 <plist version="1.0">
 <dict>
 <key>CFBundleDevelopmentRegion</key>
 <string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
    <string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>fb[A Number]</string>
        </array>
    </dict>
</array>
<key>FacebookAppID</key>
<string>[My Facebook App ID]</string>
<key>FacebookDisplayName</key>
<string>[My App Name]</string>
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>fbapi</string>
    <string>fb-messenger-api</string>
    <string>fbauth2</string>
    <string>fbshareextension</string>
</array>

Update: This has been confirmed as a bug by facebook https://developers.facebook.com/bugs/622990254549819/is

The problem is related to a method that resides in Facebook SDK

- (void)logInWithBehavior:(FBSDKLoginBehavior)loginBehavior  
{  
  __weak __typeof__(self) weakSelf = self;  
  [FBSDKServerConfigurationManager loadServerConfigurationWithCompletionBlock:^(FBSDKServerConfiguration *serverConfiguration, NSError *loadError) {  
    [weakSelf logInWithBehavior:loginBehavior serverConfiguration:serverConfiguration serverConfigurationLoadError:loadError];  
  }];  
}  

you will get no login at view because, with your button callback that include the LoginManager init, the FBSDKServerConfigurationManager is null at the first time you press the login button, but is instead init at the second time.

I have resolved a very similar issue, simply declaring the manager as a global variable and making the init of the LoginManager in viewDidLoad.

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