skip to Main Content

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.

4

Answers


  1. Chosen as BEST ANSWER

    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)
    }
    

  2. 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.

    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
  4. mb for fb login only better use FacebookLogin

    token here:

    FBSDKAccessToken.current().tokenString
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search