Facebook iOS SDK v14.1.0登录成功时回调函数未触发问题求助
Facebook iOS SDK v14.1.0登录成功时回调函数未触发问题求助
我现在在尝试通过iOS SDK实现Facebook登录功能,跟着官方教程做的,但遇到了一个棘手的问题:登录成功时回调函数完全没触发,只有用户取消登录的时候才会调用回调。
我的环境配置是:
- Facebook SDK版本:v14.1.0
- iOS系统版本:17.6
我能正常看到Facebook登录按钮,点击后会跳转到Facebook App,也能看到“Continue as ...”的授权页面,点击Continue后会跳转回我的App,但就是不触发回调。下面是我的代码和配置信息:
核心实现代码
import SwiftUI import FBSDKLoginKit @main struct myapp: App { @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate var body: some Scene { WindowGroup { FacebookLoginView() .frame(width: 200, height: 50) } } } struct FacebookLoginView: UIViewRepresentable { typealias UIViewType = FBLoginButton func makeUIView(context: Context) -> UIViewType { let button = FBLoginButton() button.permissions = ["public_profile", "email"] button.delegate = context.coordinator return button } func updateUIView(_ uiView: FBLoginButton, context: Context) { } func makeCoordinator() -> Coordinator { return Coordinator() } class Coordinator: NSObject, LoginButtonDelegate { func loginButton( _ loginButton: FBLoginButton, didCompleteWith result: LoginManagerLoginResult?, error: Error? ) { print("callback triggered") if result?.token != nil { print("Logged in") } else { print("Failed to login") } if let error = error { print("Login failed with error: \(error.localizedDescription)") return } if let result = result, !result.isCancelled { if let token = result.token?.tokenString { print("Access Token: \(token)") } } } func loginButtonDidLogOut(_ loginButton: FBLoginButton) { print("User logged out") } } } class AppDelegate: UIResponder, UIApplicationDelegate { func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { ApplicationDelegate.shared.application( application, didFinishLaunchingWithOptions: launchOptions ) return true } func application( _ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:] ) -> Bool { ApplicationDelegate.shared.application( app, open: url, sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String, annotation: options[UIApplication.OpenURLOptionsKey.annotation] ) } }
Info.plist配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLSchemes</key> <array> <string>fb<myappid></string> </array> </dict> </array> <key>FacebookAppID</key> <string><myappid></string> <key>FacebookClientToken</key> <string><myclienttoken></string> <key>FacebookDisplayName</key> <string><mydisplayname></string> <key>LSApplicationQueriesSchemes</key> <array> <string>fbapi</string> <string>fb-messenger-share-api</string> </array> <key>LSApplicationQueriesSchemes</key> <array> <string>fbapi</string> <string>fb-messenger-share-api</string> <string>fbauth2</string> <string>fbshareextension</string> </array> </dict> </plist>
登录流程截图
- App内显示Facebook登录按钮

- 跳转至Facebook授权页面

- 授权确认弹窗

- 授权完成后跳转回App

- 回到App但无回调触发

尝试过的无效方案
我尝试添加了SceneDelegate来处理URL跳转回调,但问题依旧存在:
class SceneDelegate: UIResponder, UIWindowSceneDelegate { var window: UIWindow? func scene( _ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext> ) { print("scene delegate openURLContexts called") guard let url = URLContexts.first?.url else { return } ApplicationDelegate.shared.application( UIApplication.shared, open: url, sourceApplication: nil, annotation: [UIApplication.OpenURLOptionsKey.annotation] ) } func scene( _ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions ) { guard let windowScene = scene as? UIWindowScene else { return } let window = UIWindow(windowScene: windowScene) window.rootViewController = UIHostingController(rootView: FacebookLoginView()) self.window = window window.makeKeyAndVisible() } }
备注:内容来源于stack exchange,提问作者Leo




