Xcode 13 在 SwiftUI 中实现 Firebase 身份验证时找不到 FacebookAppID

Xcode 13 cannot find FacebookAppID when implementing Firebase auth in SwiftUI

Xcode 13 的主要变化是 info.plist 不再可见。根据 Facebook 的文档,我需要将以下内容添加到 info.plist:

<key>CFBundleURLTypes</key>
<array>
  <dict>
  <key>CFBundleURLSchemes</key>
  <array>
    <string>fbAPP-ID</string>
  </array>
  </dict>
</array>
<key>FacebookAppID</key>
<string>APP-ID</string>
<key>FacebookClientToken</key>
<string>CLIENT-TOKEN</string>
<key>FacebookDisplayName</key>
<string>APP-NAME</string>

我试图在项目的 Targets -> Info 设置下手动添加这些。

但是,每当我点击我的应用程序上的登录按钮时,我都会收到一个运行时错误

Thread 1: "App ID not found. Add a string value with your app ID for the key FacebookAppID to the Info.plist or call [FBSDKSettings setAppID:]."

其他一切似乎都正常(我可以在屏幕上看到 Facebook 默认按钮)。

我的 Facebook 登录视图

import SwiftUI
import FacebookLogin

struct CoverView: View {
    @AppStorage("isLoggedIn") var isLoggedIn = false
    @AppStorage("email") var email = ""
    
    var body: some View {
        FBLog(isLoggedIn: $isLoggedIn, email: $email)
            .frame(height: 50)
            .padding(.horizontal, 35)
            .clipShape(Capsule())
    }
}

struct FBLog: UIViewRepresentable {
    
    func makeCoordinator() -> Coordinator {
        return FBLog.Coordinator(parent1: self)
    }
    
    
    @Binding var isLoggedIn: Bool
    @Binding var email: String
    
    func makeUIView(context: Context) -> FBLoginButton {
        let button = FBLoginButton()
        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 loginButton(_ loginButton: FBLoginButton, didCompleteWith result: LoginManagerLoginResult?, error: Error?) {
            if error != nil {
                print(error!.localizedDescription)
                return
            }
            
            // check if user cancelled the flow.
            
            if !result!.isCancelled {
                parent.isLoggedIn = true
                let request = GraphRequest(graphPath: "me", parameters: ["fields": "email"])
                request.start() { [self] (_, res, _) in
                    
                    guard let profileData = res as? [String: Any] else {return}
                    
                    // save email
                    parent.email = profileData["email"] as! String
                    
                }
            }
        }
        
        func loginButtonDidLogOut(_ loginButton: FBLoginButton) {
            // logging out
            parent.isLoggedIn = false
            parent.email = ""
        }
              
    }
}

问题

如何使用 Xcode 13 在 SwiftUI 中正确实施 Facebook 身份验证?

刚刚找到适合我的答案:

Tested in Xcode 12.5

make an AppDelegate.swift and add this code below.

import SwiftUI import FacebookCore

class AppDelegate: 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 ApplicationDelegate.shared.application(app, open: url, options: options)
    } } 

next in your @main App file add this line

@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate