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
相关产品推荐
相关产品推荐

