我已将正确的 Facebook sdk 值添加到 info.plist 中,如快速入门指南所示,并使用包管理器添加 SDK,但我仍然收到此错误
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:]
.
应用程序 ID 100% 正确,位于我的 info.plist 中。如果我在应用程序委托中设置名称和应用程序 ID,我只会收到此错误:
Thread 1: fbappid is not registered as a URL scheme. Please add it in your Info.plist
Facebook SDK 似乎在我的 info.plist 中找不到任何值?
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fb823445029398455</string>
</array>
</dict>
</array>
<key>FacebookAppID</key>
<string>823445029398455</string>
<key>FacebookDisplayName</key>
<string>vertoo-dev</string>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fbapi20130214</string>
<string>fbapi20130410</string>
<string>fbapi20130702</string>
<string>fbapi20131010</string>
<string>fbapi20131219</string>
<string>fbapi20140410</string>
<string>fbapi20140116</string>
<string>fbapi20150313</string>
<string>fbapi20150629</string>
<string>fbapi20160328</string>
<string>fbauth</string>
<string>fb-messenger-share-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>
将其添加到新文件中
class AppDelegate: NSObject, UIApplicationDelegate {
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
// Initialize Facebook SDK
FBSDKCoreKit.ApplicationDelegate.shared.application(
application,
didFinishLaunchingWithOptions: launchOptions
)
return true
}
}
在 Xcode 12.5 中测试
创建一个 AppDelegate.swift 并在下面添加此代码。
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)
}
}
接下来在您的@main App 文件中添加此行
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
您应该在启动时初始化 sdk,如此处所述
https://github.com/facebook/facebook-ios-sdk/issues/1745
其他答案可以做到这一点,但它们让您使用 UIKit 中的 UIApplicationDelegate。 如果您只想从 swiftui 执行此操作,只需在应用程序文件中的 contentview 上添加 onappear 即可:
var body: some Scene {
WindowGroup {
ContentView()
.onAppear(){
ApplicationDelegate.shared.application(UIApplication.shared, didFinishLaunchingWithOptions: nil)
}
}
在 SwiftUI 中
1- 确保文件名是 Info.plist - 大写“I”。
2-如果您遇到错误,提示“多个命令产生....Info.plist”,
解决方案:
打开目标 -> 构建阶段 > 复制捆绑资源并从其中删除 Info.plist。
确保以下键值对
<key>FacebookAppID</key>
<string>823445029398455</string>
<key>FacebookDisplayName</key>
<string>vertoo-dev</string>
放置在 plist 文件的顶层,而不是嵌套在任何其他键值对中。