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
I needed to add this function in my AppDelegate.swift. It seems completely undocumented.
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 thatYou can also change this in Settings:
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.
If you have already added to CFBundleURLSchemes in your Info.plist to LSApplicationQueriesSchemes.
mb for fb login only better use FacebookLogin
token here: