iOS13中Swift实现的Facebook登录点击后总是取消(iOS12及以下正常)
I see you're running into an issue where Facebook login works flawlessly on iOS versions below 12.0, but throws a "User cancelled login" error on iOS 13.0. Let's break down the most likely causes and actionable fixes based on your code, config, and pod versions:
1. Outdated AppDelegate URL Handling (iOS 13+ Compatibility)
iOS 13 deprecated the application(_:open:sourceApplication:annotation:) method you're using. The newer application(_:open:options:) method is required to handle URL redirects properly for iOS 13+. Here's how to update your AppDelegate.swift:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions) } // Replace your old open method with this updated version func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any]) -> Bool { return FBSDKApplicationDelegate.sharedInstance().application( application, open: url, sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String, annotation: options[UIApplication.OpenURLOptionsKey.annotation] ) }
If your app uses SceneDelegate (standard for iOS 13+ projects), add URL handling there too:
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) { guard let url = URLContexts.first?.url else { return } FBSDKApplicationDelegate.sharedInstance().application( UIApplication.shared, open: url, sourceApplication: nil, annotation: [UIApplication.OpenURLOptionsKey.annotation] ) }
2. Upgrade Your Facebook SDK Versions
Your current pod versions (FBSDKCoreKit 4.44.1 and FacebookLogin 0.6.0) are outdated and lack full iOS 13 support. Facebook released specific updates to fix iOS 13 compatibility bugs. Update your Podfile to use newer, stable versions:
# Replace your existing Facebook pods with these (or use the latest stable releases) pod 'FBSDKCoreKit', '~> 17.0' pod 'FBSDKLoginKit', '~> 17.0'
After updating, run pod install --repo-update to fetch the latest SDKs.
3. Fix LoginManager Presentation Context (iOS 13 Modality Changes)
iOS 13 altered how view controllers are presented modally, which can cause the Facebook login sheet to fail silently (triggering the "cancelled" error). To resolve this, set a presentation context provider for your LoginManager:
First, make your view controller conform to LoginPresentationContextProviding:
extension YourViewController: LoginPresentationContextProviding { func presentationAnchor(for loginManager: LoginManager) -> ASPresentationAnchor { return self.view.window ?? UIWindow() } }
Then update your loginWithFacebook function to attach the provider:
class func loginWithFacebook(controller: UIViewController, completionHandler: @escaping (_ result: Bool) -> Void){ let defaults = UserDefaults.standard if defaults.value(forKey: "FBToken") != nil { let jsonData : [String : Any] = defaults.value(forKey: "FBDict") as? [String : Any] ?? [String: Any]() print(jsonData) completionHandler(true) } else{ let loginManager = LoginManager() // Add this line to set the presentation context if let presentingVC = controller as? LoginPresentationContextProviding { loginManager.presentationContextProvider = presentingVC } loginManager.logIn(readPermissions: [.publicProfile, .email, .userGender, .userBirthday], viewController: controller) { 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(grantedPermissions) print(declinedPermissions) print(accessToken) Utility.getFacebookUserData(completionHandler: { (success) in completionHandler(success) }) } } } }
4. Verify Full Info.plist Configuration
Double-check that you have all required Facebook entries in your Info.plist (you might be missing these even with ATS configured):
<!-- Facebook URL Scheme (replace YOUR_APP_ID with your actual ID) --> <key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLSchemes</key> <array> <string>fbYOUR_APP_ID</string> </array> </dict> </array> <!-- Allow queries to Facebook apps --> <key>LSApplicationQueriesSchemes</key> <array> <string>fbapi</string> <string>fb-messenger-share-api</string> <string>fbauth2</string> <string>fbshareextension</string> </array> <!-- Facebook App ID and Display Name --> <key>FacebookAppID</key> <string>YOUR_APP_ID</string> <key>FacebookDisplayName</key> <string>YOUR_APP_DISPLAY_NAME</string>
Recommended Troubleshooting Order
- Upgrade your Facebook SDKs first (this fixes the majority of iOS 13 compatibility bugs)
- Update your AppDelegate/SceneDelegate URL handling
- Add the presentation context provider
- Verify your full Info.plist entries
内容的提问来源于stack exchange,提问作者bhargav K

