Facebook login always comes back as cancelled. (iOS Swift)
I had the some problem but I found a workaround. You can set the login behaviour of the Login Manager to use the Facebook details on the phone. The default behaviour is FBSDKLoginBehaviorSystemNative and that tries to use the Facebook app first and then if its not there, it uses a web modal.
Instead of doing it that way and passing around urls that don't seem to work you can set the login behaviour as FBSDKLoginBehaviorSystemAccount.
Long story short, try:
let fbLoginManager = FBSDKLoginManager();
fbLoginManager.loginBehavior = FBSDKLoginBehaviorSystemAccount;
// call login method of choice here
With Facebook SDK version 4 I only had to add this to my application delegate on iOS 10 & Swift 3 to make the authentication work. Before that I also only had cancelled logins.
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
return SDKApplicationDelegate.shared.application(app, open: url, options: options)
}
I have same issue login method always return canacelled then I add below things in info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>akamaihd.net</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
<key>facebook.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
<key>fbcdn.net</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
</dict>
</dict>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fb-messenger-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>
and in AppDelegate update didFinishLaunching method and add new method like below
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
}
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool
{
return SDKApplicationDelegate.shared.application(app, open: url, options: options)
}
Login method is given below:
@objc func loginButtonClicked() {
let loginManager = LoginManager()
loginManager.loginBehavior = .systemAccount
loginManager.logIn([ .publicProfile,.userFriends,.email ], viewController: 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("Logged in!")
//Do further code...
}
}
}
it works for me in Swift 3.0 and SDK 4.17.0, I hope it works for you Thanks.
Did you add function to AppDelegate?
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}