1
votes

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
are you there ? - KKRocks
yes, I am here. - markhorrocks
Do you want to access user info ? - KKRocks
I don't need the user info but I do need the token and the other variables set by login .success. I want to send the token to an API for login there. - markhorrocks
Are you got access token while authenticate ? - KKRocks

4 Answers

2
votes

mb for fb login only better use FacebookLogin

token here:

FBSDKAccessToken.current().tokenString
1
votes

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

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.

0
votes

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.