简体   繁体   中英

Blank browser page when using Facebook login on swift ios 10

I'm using swift 3, ios 10 and Facebook's swift sdk v0.2.0.

I just implemented login with facebook in my application, pressing the "continue with facebook" button will open safari (I'm running on the simulator which does not have the facebook app installed) but about half of the time the browser will turn completely blank, no navbar or anything and the console will output this error:

2017-08-03 12:40:05.276987-0500 Foster[30486:726822] [ViewService] Failed to get remote view controller with error: Error: domain=NSCocoaErrorDomain, code=0

But sometimes the browser does manage to load and display the facebook login page and I'm able to login to facebook normally and continue using my app. I have not found what triggers the error above, seems to be random.

I've seen a lot of issues regarding this blank page on facebook login, but they are usually related to delegation after login, which works fine when safari manages to load the login page, I'm getting the blank page right after pressing the "login with facebook" button. Also most other similar errors get code=4099 .

I have followed all the steps in Facebook's swift documentation ( https://developers.facebook.com/docs/swift/login ) and ios documentation ( https://developers.facebook.com/docs/facebook-login/ios/ ).

I installed FacebookLogin and FacebookCore via cocoaPods.

I have updated my plist file:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>fb...</string>
        </array>
    </dict>
</array>
<key>FacebookAppID</key>
<string>...</string>
<key>FacebookDisplayName</key>
<string>...</string>
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>fbapi</string>
    <string>fb-messenger-api</string>
    <string>fbauth2</string>
    <string>fbshareextension</string>
</array>

My app delegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    registerForPushNotifications()
    return SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
}

public func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool{
    return SDKApplicationDelegate.shared.application(app, open: url, options: options)
}

My ViewController:

@IBAction func loginButtonClicked(_ sender: UIButton) {
    let loginManager = LoginManager()
    loginManager.logOut()
    loginManager.logIn([ .publicProfile, .userFriends ], viewController: self) { [unowned 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("Facebook Access Token: " + accessToken.authenticationToken)

        }
    }
}

func loginButtonDidCompleteLogin(_ loginButton: LoginButton, result: LoginResult) {}

func loginButtonDidLogOut(_ loginButton: LoginButton) {
    print("Facebook LogOut")
}

Keychain sharing is on.

I have seen a lot of pots of people with similar problems but none of the solutions listed there work.

Update

After more testing I have only been able to replicate the bug in the simulator, not on a physical device

Remove this code-

public func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool{
    return SDKApplicationDelegate.shared.application(app, open: url, options: options)
}

Add this code in your appdelegate-

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
       return FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
}

I think your problem in one of the appDelegate functions , try to change your second function in the appDelegate to the following :

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

    return FBSDKApplicationDelegate.sharedInstance().application(
      app,
      open: url as URL!,
      sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String,
      annotation: options[UIApplicationOpenURLOptionsKey.annotation]
    )

}

you missed to return sourceApplication and this is the reason of the blank screen.

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