Swift集成Firebase后Facebook登录提示App ID未找到求助
问题:集成Firebase后Facebook登录崩溃,提示无法找到App ID
近期在Swift项目中集成了Firebase,此前Facebook登录功能正常,但现在App能正常启动,点击登录按钮却崩溃,提示无法找到Facebook App ID,尽管该ID已配置在Info.plist中。已按照文档将FirebaseAppDelegateProxyEnabled设为NO以禁用方法混编,但问题仍未解决。调试后发现Facebook的App Delegate从未被调用,尝试了多种网上的解决方案均无效,恳请各位提供帮助。
以下是相关配置及代码:
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>CFBundleTypeRole</key> <string>Editor</string> <key>CFBundleURLSchemes</key> <array> <string>com.googleusercontent.apps.###</string> </array> </dict> <dict> <key>CFBundleTypeRole</key> <string>Editor</string> <key>CFBundleURLSchemes</key> <array> <string>fb###</string> </array> </dict> </array> <key>FacebookAppID</key> <string>####</string> <key>FacebookClientToken</key> <string>#####</string> <key>FacebookDisplayName</key> <string>AlmaAmor</string> <key>FirebaseAppDelegateProxyEnabled</key> <false/> <key>GIDClientID</key> <string>#########.apps.googleusercontent.com</string> <key>LSApplicationQueriesSchemes</key> <array> <string>fbapi</string> <string>fb-messenger-share-api</string> </array> </dict> </plist>
App Delegate代码
class AppDelegate: NSObject, UIApplicationDelegate { func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { FirebaseApp.configure() // 初始化Facebook SDK FBSDKCoreKit.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, options: options) return true } } class GoogleAppDelegate: NSObject, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { ApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions) } func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool { return GIDSignIn.sharedInstance.handle(url) } func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { // 将设备令牌传递给认证服务 Auth.auth().setAPNSToken(deviceToken, type: .prod) // 根据App需求进一步处理设备令牌 // ... } }
登录视图相关代码
struct FBLog: UIViewRepresentable { @Binding var logged: Bool @Binding var email: String func makeCoordinator() -> FBLog.Coordinator { return FBLog.Coordinator(parent1: self) } func makeUIView(context: Context) -> FBLoginButton{ let button = FBLoginButton() button.permissions = ["public_profile", "email"] button.delegate = context.coordinator return button } func updateUIView(_ uiView: FBLoginButton, context: Context){ } class Coordinator : NSObject, LoginButtonDelegate{ var parent : FBLog init(parent1: FBLog){ parent = parent1 } func loginButtonDidLogOut(_ loginButton: FBLoginButton) { parent.logged = false parent.email = "" } func loginButton(_ loginButton: FBLoginButton, didCompleteWith result: LoginManagerLoginResult?, error: Error?){ if error != nil { print(error!.localizedDescription) return } if !(result!.isCancelled) { // 自定义业务代码 } } } }
FBLog的调用方式
struct SignUpView: View { @State var logged = false @State var creatingAccount = false @State var email: String = "" @State var creationError = false @State var logged_in_with = "" @StateObject private var store = UserStore() let dummyError = CreationError.basic var body: some View { VStack { if logged { if creatingAccount { CreateAccountView() } else { MainPage() } } else { GeometryReader { geometry in VStack { Spacer() Text("Get Started").bold() Spacer() HStack { Spacer() GoogleSignInButton(action: handleSignInButton) .frame(width: geometry.size.width * 0.77, height: geometry.size.height * 0.025) .padding(EdgeInsets(top: geometry.size.width * 0.04, leading: geometry.size.width * 0.04, bottom: geometry.size.width * 0.04, trailing: geometry.size.width * 0.04)) .background(Color.white.opacity(0.20)) .cornerRadius(geometry.size.width * 0.5) .overlay( VStack(spacing: geometry.size.width * 0.04) { // Google登录按钮内部内容,垂直居中 } ) Spacer() } HStack { Spacer() FBLog(logged: $logged, email: $email) .frame(width: geometry.size.width * 0.7, height: geometry.size.height * 0.025) .padding(EdgeInsets(top: geometry.size.width * 0.04, leading: geometry.size.width * 0.04, bottom: geometry.size.width * 0.04, trailing: geometry.size.width * 0.04)) .background(Color.blue) // 替换为自定义背景色 .cornerRadius(geometry.size.width * 0.04) .overlay( VStack(spacing: geometry.size.width * 0.04) { // FBLog内部内容,垂直居中 } ) Spacer() } Spacer() } .background(LinearGradient(gradient: Gradient(colors: [.yellow, .green, .red]), startPoint: .top, endPoint: .bottom)) .alert(isPresented: $creationError, error: dummyError) { error in // 如需自定义按钮可在此添加 } message: { error in if let message = error.errorMessage { Text(message) } } } } } } } import SwiftUI import FBSDKCoreKit import FirebaseAuth @main struct BlahApp: App { @StateObject private var store = UserStore() @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate @UIApplicationDelegateAdaptor(GoogleAppDelegate.self) var googleAppDelegate @UIApplicationDelegateAdaptor(FirebaseAppDelegate.self) var firebaseAppDelegate var body: some Scene { WindowGroup { Landing() } } }
内容的提问来源于stack exchange,提问作者Shehzaad Daureeawoo
相关产品推荐
相关产品推荐

