You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xcode13下SwiftUI实现Firebase认证时无法找到FacebookAppID问题咨询

Xcode13 SwiftUI项目Facebook认证正确配置步骤

1. 修正Info.plist配置(解决Xcode13默认配置不生效问题)

Xcode13新建项目默认开启Generate Info.plist File选项,配置项存储在项目设置中,第三方SDK无法正常读取,需要手动生成独立Info.plist:

  • 打开项目Targets面板,选择Info标签,右键任意配置项,选择「Open As -> Source Code」,全选复制所有内容
  • 在项目根目录右键选择「New File...」,搜索Property List,命名为Info.plist后保存
  • 打开新建的Info.plist,右键选择「Open As -> Source Code」,粘贴之前复制的内容,再添加如下配置(替换所有占位符为你Facebook后台的真实值):
<key>CFBundleURLTypes</key>
<array>
  <dict>
  <key>CFBundleURLSchemes</key>
  <array>
    <string>fb[你的真实App ID]</string>
  </array>
  </dict>
</array>
<key>FacebookAppID</key>
<string>[你的真实App ID]</string>
<key>FacebookClientToken</key>
<string>[你的真实Client Token]</string>
<key>FacebookDisplayName</key>
<string>[你的App名称]</string>
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>fbapi</string>
    <string>fb-messenger-share-api</string>
    <string>fbauth2</string>
    <string>fbshareextension</string>
</array>
  • 打开Targets -> Build Settings面板,搜索Generate Info.plist File,将值改为NO
  • 搜索Info.plist File,将值改为你新建的Info.plist的路径,通常为[你的项目名]/Info.plist

2. 初始化Facebook SDK

SwiftUI App生命周期需要手动添加初始化逻辑,处理启动和回调:

import SwiftUI
import FBSDKCoreKit

@main
struct YourAppName: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    var body: some Scene {
        WindowGroup {
            // 替换为你的入口视图
            CoverView()
                .onOpenURL { url in
                    ApplicationDelegate.shared.application(
                        UIApplication.shared,
                        open: url,
                        sourceApplication: nil,
                        annotation: [UIApplication.OpenURLOptionsKey.annotation]
                    )
                }
        }
    }
}

class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        // 兜底配置Facebook参数,避免Info.plist读取失败
        Settings.shared.appID = "[你的真实App ID]"
        Settings.shared.clientToken = "[你的真实Client Token]"
        Settings.shared.displayName = "[你的App名称]"
        
        return ApplicationDelegate.shared.application(
            application,
            didFinishLaunchingWithOptions: launchOptions
        )
    }
}

3. 生效配置

  • 按Shift+Cmd+K清空Xcode构建缓存
  • 卸载模拟器/真机上已安装的旧版应用,重新运行项目

你提供的登录视图代码无需修改,配置完成后即可正常触发Facebook登录流程。

内容的提问来源于stack exchange,提问作者PipEvangelist

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 07:39:01