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

SwiftUI应用模拟器无法连接Firestore本地模拟器问题求助

SwiftUI应用无法连接Firestore本地模拟器

问题背景

在SwiftUI应用模拟器中尝试连接Firestore本地模拟器,已试过127.0.0.1、局域网IP(如192.168.x.x)以及localhost配置,参考过相关方案但无效。核心问题是无法连接模拟器,已确认端口与IP匹配、模拟器启动正常,也修改过firebase.json中的IP配置。

初始配置代码

class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        FirebaseApp.configure()
        
        Auth.auth().useEmulator(withHost: IP_ADDRESS, port: 9099)
        
        let storeSettings = Firestore.firestore().settings
        storeSettings.host = IP_ADDRESS+":8080"
        storeSettings.isPersistenceEnabled = false
        storeSettings.isSSLEnabled = false
        Firestore.firestore().settings = storeSettings
        
        FirebaseStorage.Storage.storage().useEmulator(withHost: IP_ADDRESS, port: 9199)
        
        FirebaseFunctions.Functions.functions().useEmulator(withHost: IP_ADDRESS, port: 5001)
        
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
            UNUserNotificationCenter.current().delegate = self
        }
        
        return true
    }
}

初始错误信息

error when getting profile: Optional(Error Domain=FIRFirestoreErrorDomain Code=14 "Failed to get document because the client is offline." UserInfo={NSLocalizedDescription=Failed to get document because the client is offline.})

2023-07-19 18:13:44.586226-0400 Pronto[25712:2998528] 10.9.0 - [FirebaseFirestore][I-FST000001] WatchStream (7fadc150eba8) Stream error: 'Unknown: An internal error has occurred, print and inspect the error details for more information.'

2023-07-19 18:13:44.587878-0400 Pronto[25712:2998528] 10.9.0 - [FirebaseFirestore][I-FST000001] Could not reach Cloud Firestore backend. Connection failed 1 times. Most recent error: An internal error has occurred, print and inspect the error details for more information.

This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.

2023-07-19 18:13:44.592171-0400 Pronto[25712:2998536] 10.9.0 - [FirebaseFirestore][I-FST000001] WatchStream (7fadc150eba8) Stream error: 'Unknown: An internal error has occurred, print and inspect the error details for more information.'

注:第一个错误源于检查Firestore文档是否存在,核心问题为无法连接模拟器

修改后的配置代码

class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        FirebaseApp.configure()
        
        // 试过"localhost"和"127.0.0.1",上方是"127.0.0.1"的错误,下方是"localhost"的错误
        Auth.auth().useEmulator(withHost: "localhost", port: 9099)
        
        let storeSettings = Firestore.firestore().settings
        storeSettings.host = "localhost:8080"
        storeSettings.isPersistenceEnabled = false
        storeSettings.isSSLEnabled = false
        Firestore.firestore().settings = storeSettings
        
        FirebaseStorage.Storage.storage().useEmulator(withHost: "127.0.0.1", port: 9199)
        
        FirebaseFunctions.Functions.functions().useEmulator(withHost: "127.0.0.1", port: 5001)
        
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
            UNUserNotificationCenter.current().delegate = self
        }
        
        return true
    }
}

firebase.json配置

{
  "functions": [
    {
      "source": "functions",
      "codebase": "default",
      "ignore": [
        "node_modules",
        ".git",
        "firebase-debug.log",
        "firebase-debug.*.log"
      ],
      "predeploy": []
    }
  ],
  "firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  },
  "storage": {
    "rules": "storage.rules"
  },
  "emulators": {
    "auth": {
      "port": 9099
    },
    "functions": {
      "port": 5001
    },
    "firestore": {
      "port": 8080
    },
    "storage": {
      "port": 9199
    },
    "ui": {
      "enabled": true
    },
    "singleProjectMode": true
  }
}

修改后的错误信息

2023-07-19 20:29:57.524333-0400 Pronto[32513:3147732] [connection] nw_socket_handle_socket_event [C1.1.1:2] Socket SO_ERROR [61: Connection refused]

error when getting profile: Optional(Error Domain=FIRFirestoreErrorDomain Code=14 "Failed to get document because the client is offline." UserInfo={NSLocalizedDescription=Failed to get document because the client is offline.})

2023-07-19 20:29:57.575475-0400 Pronto[32513:3147727] [connection] nw_socket_handle_socket_event [C2.1.1:2] Socket SO_ERROR [61: Connection refused]

2023-07-19 20:29:57.612341-0400 Pronto[32513:3147732] [connection] nw_socket_handle_socket_event [C3.1.1:2] Socket SO_ERROR [61: Connection refused]

2023-07-19 20:30:00.147268-0400 Pronto[32513:3147733] 10.9.0 - [FirebaseFirestore][I-FST000001] WatchStream (7f8fd1a0a638) Stream error: 'Unknown: An internal error has occurred, print and inspect the error details for more information.'

2023-07-19 20:30:00.599262-0400 Pronto[32513:3147733] 10.9.0 - [FirebaseFirestore][I-FST000001] Could not reach Cloud Firestore backend. Connection failed 1 times. Most recent error: An internal error has occurred, print and inspect the error details for more information.

This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.
2023-07-19 20:30:00.894489-0400 Pronto[32513:3147733] 10.9.0 - [FirebaseFirestore][I-FST000001] WatchStream (7f8fd1a0a638) Stream error: 'Unknown: An internal error has occurred, print and inspect the error details for more information.'

求助

请问哪里操作出错了?希望能找到解决方法。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 15:24:53