1

I'm trying to test Facebook SDK login in my app but keep getting this error. I see a dialog which accepts my email and password to login and then tells me I have already accepted the app. I should see a message "Logged in but I only see the error below. How can I test the Facebook SDK in the Xcode iOS simulator. I tested this on an iPhone with an alert to display the access token but got the same result. It will always present "User cancelled login".

-canOpenURL: failed for URL: "fbauth2:/" - error: The operation couldn’t be completed. (OSStatus error -10814.) User cancelled login.

Here is my code.

func signInWithFacebook(button: UIButton) {

    let loginManager = LoginManager()

    loginManager.logIn([ .publicProfile ], viewController: self) { loginResult in

        switch loginResult {

        case .failed(let error):

            let alertController: UIAlertController = UIAlertController(title: "Login error", message: error as? String, preferredStyle: .alert)

            let okAction: UIAlertAction = UIAlertAction(title: "OK", style: .default) { action -> Void in }

            alertController.addAction(okAction)

            self.present(alertController, animated: true, completion: nil)

            print(error)

        case .cancelled:

            let alertController: UIAlertController = UIAlertController(title: "Login cancelled", message: "User cancelled login", preferredStyle: .alert)

            let okAction: UIAlertAction = UIAlertAction(title: "OK", style: .default) { action -> Void in }

            alertController.addAction(okAction)

            self.present(alertController, animated: true, completion: nil)

            print("User cancelled login.")

        case .success(let grantedPermissions, let declinedPermissions, let accessToken):

            print(grantedPermissions)

            print(declinedPermissions)

            print(accessToken)

            let alertController: UIAlertController = UIAlertController(title: "Login success", message: String(describing: accessToken), preferredStyle: .alert)

            let okAction: UIAlertAction = UIAlertAction(title: "OK", style: .default) { action -> Void in }

            alertController.addAction(okAction)

            self.present(alertController, animated: true, completion: nil)

            print("Logged in")
        }
    }
}

On the first pass, I got the Facebook login dialog but thereafter I just see a dialog saying I have already authorized my app with an OK button.

let accessToken = AccessToken.current

in viewDidLoad() returns nil always. I need to get an accessToken so i can send it to an API and test the response.

markhorrocks
  • 1,199
  • 19
  • 82
  • 151

4 Answers4

2

mb for fb login only better use FacebookLogin

token here:

FBSDKAccessToken.current().tokenString
  • actually `let token = accessToken.authenticationToken` – markhorrocks Jun 07 '17 at 09:14
  • hm. interesting, but on one my project i see: ` func loginButtonDidCompleteLogin(_ loginButton: LoginButton, result: LoginResult){ switch result { case .failed(let error): print(error) case .cancelled: print("Cancelled") case .success(let grantedPermissions, let declinedPermissions, let accessToken): print("Logged In", FBSDKAccessToken.current().tokenString) ... } } }` and its works! :) – Andrew DevNir Jun 07 '17 at 11:16
1

I needed to add this function in my AppDelegate.swift. It seems completely undocumented.

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

        return FBSDKApplicationDelegate.sharedInstance().application(application,
                                                                     open: url,
                                                                     sourceApplication: sourceApplication,
                                                                     annotation: annotation)
}
markhorrocks
  • 1,199
  • 19
  • 82
  • 151
0

I think Error status 10814 is related to cantOpenUrl, which is used when Facebook call the URL using the arguments fbauth2:/. Like suggested here. Printing happens inside this function so you can't do anything much with that

You can also change this in Settings:

Targets > Capabilities > Enable Keychain Sharing

It doesn't matter because you are testing in the Simulator and it will not be coming when you will test in a real device.

Anurag Sharma
  • 4,276
  • 2
  • 28
  • 44
0

If you have already added to CFBundleURLSchemes in your Info.plist to LSApplicationQueriesSchemes.

If you are trying to resolve "canOpenURL: failed" warnings, those only indicate that the Facebook app is not installed on your device or simulator and can be ignored.

KKRocks
  • 8,222
  • 1
  • 18
  • 84