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

SwiftUI iOS应用对接Firebase Authentication Emulator配置问题

问题原因

你仅完成了Firestore服务的模拟器指向配置,其余Firebase服务需单独配置本地模拟器端点,才会将请求转发到本地模拟器而非线上环境。

各服务模拟器配置方法

所有配置需放置在FirebaseApp.configure()执行之后、对应服务的第一次业务调用之前。

  • Authentication模拟器配置
    Auth模拟器默认端口为9099,添加如下代码:
    Auth.auth().useEmulator(withHost: "localhost", port: 9099)
    
  • Storage模拟器配置
    Storage模拟器默认端口为9199,后续接入时添加如下代码:
    Storage.storage().useEmulator(withHost: "localhost", port: 9199)
    
  • Functions模拟器配置
    Functions模拟器默认端口为5001,后续接入时添加如下代码:
    Functions.functions().useEmulator(withHost: "localhost", port: 5001)
    
完整初始化示例代码
init() {
    FirebaseApp.configure()
    
    // Firestore模拟器配置
    let settings = Firestore.firestore().settings
    settings.host = "localhost:8080"
    settings.isPersistenceEnabled = false
    settings.isSSLEnabled = false
    Firestore.firestore().settings = settings
    
    // Authentication模拟器配置
    Auth.auth().useEmulator(withHost: "localhost", port: 9099)
    
    // 后续接入Storage时取消注释
    // Storage.storage().useEmulator(withHost: "localhost", port: 9199)
    
    // 后续接入Functions时取消注释
    // Functions.functions().useEmulator(withHost: "localhost", port: 5001)
}
注意事项
  • 配置前核对模拟器启动时终端输出的各服务端口,如果你自定义过模拟器端口,需将代码中的端口替换为你实际使用的端口
  • iOS 14及以上系统默认拦截HTTP请求,需在项目Info.plist中添加localhost域的HTTP请求权限,避免模拟器请求被系统拦截
  • 配置更新后建议卸载设备/模拟器上的原有应用再重新安装运行,避免旧配置缓存导致请求仍走线上环境

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 02:48:03