无法使用 iOS 设备连接到 Firebase 模拟器

Whi*_*ind 4 ios firebase swift firebase-authentication firebase-realtime-database

我正在尝试将 iOS 设备连接到 Firebase Auth 和实时数据库模拟器。

问题是,我可以在本地计算机上使用 NodeJS 通过 Firebase Admin 连接并使用模拟器(通过 http://localhost:9000?ns=my-project)。

我还可以使用 iOS 设备连接到远程 Firebase 服务器...但在本地它不起作用。它会抛出一堆随机错误,如下所示(当我尝试完成注册/身份验证时):

错误域=NSURLErrorDomain代码=-1004“无法连接到服务器。” NSLocalizedDescription=无法连接到服务器。, NSErrorFailingURLStringKey=http://192.168.1.3:9099/www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=myKeyGoesHere

可选(错误域=com.firebase.core代码=1“无法获取查询FQuerySpec的最新值(路径:/news,参数:{}),客户端离线,没有活动侦听器,也没有匹配的磁盘缓存条目”

这是 firebase.json:

{
  "database": {
    "rules": "database.rules.json"
  },
  "emulators": {
    "auth": {
      "port": 9099
    },
    "database": {
      "port": 9000
    },
    "ui": {
      "enabled": true
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我改变了规则以防万一:

{
  "rules": {
    ".read": true,
    ".write": true
  }
}
Run Code Online (Sandbox Code Playgroud)

但事实并非如此。

以下是我尝试连接到 iOS 应用程序中的数据库(我的 FirebaseManager 类)的方法:

 init(){
      Auth.auth().useEmulator(withHost:"192.168.1.3", port:9099)
 }

  private lazy var newsNodeRef:DatabaseReference? = {
    
    guard let urlString = getBaseURL() else {return nil}
    
    let node = LocalConstants.kNewsRef // this has value of 'news'
    return Database.database(url: urlString).reference(withPath: node)
}()


    
    private func getBaseURL()->String?{
        let environment = Environment()
        guard let connectionProtocol = environment.configuration(PlistKey.firebaseConnectionProtocol), let baseURL = environment.configuration(PlistKey.firebaseDatabaseURL) else {return nil}
        let urlString = "\(connectionProtocol)://\(baseURL)"
        
        return urlString // this produces something like 'http://192.168.1.3:9000?ns=my-project' (its fetched from Configuration Settings file based on selected environment)
    }
Run Code Online (Sandbox Code Playgroud)

问题是,如果我只是更改环境(它会自动更改基本 url),则完全相同的设置可以在远程服务器上运行。

我还允许在 info.plist 中进行不安全的 http 加载,只是为了确定它是否不是这样,但仍然不起作用。

这是我运行模拟器时在控制台中得到的内容:

终端

这里有什么问题?

İsa*_*llı 7

我回复的有点晚了。
\n我看到了你找到的解决方案。它对我不起作用,但我相信它对很多人都有效。

\n

我也找到了解决方案。
\n实际上,我看不到 iOS 15 的问题。我的问题是它在 iOS 14 及更早版本上不起作用。

\n

解决方案;

\n

首先,您需要 MacBook 的 IP 地址。
\n查找IP地址;
\n您可以直接在系统首选项 -> 网络 -> 状态下访问它。

\n

然后我们需要对 firebase.json 文件进行一些更改。

\n

为每个部分添加 \xe2\x80\x9chost\xe2\x80\x9d : \xe2\x80\x9cIP\xe2\x80\x9d 。
\n用 \xe2\x80\x9cport\xe2\x80\x9d 部分覆盖 \xe2\x80\x9chost\xe2\x80\x9d 部分。

\n
"emulators": {\n"auth": {\n  "host": "192.168.1.11\xe2\x80\x9d,\n  "port": 9100\n},\n"functions": {\n  "host": "192.168.1.11\xe2\x80\x9d,\n  "port": 5002\n},\n"firestore": {\n  "host": "192.168.1.11\xe2\x80\x9d,\n  "port": 8081\n},\n"database": {\n  "host": "192.168.1.11",\n  "port": 9001\n},\n"storage": {\n  "host": "192.168.1.11",\n  "port": 9200\n},\n"ui": {\n  "enabled": true\n}\n
Run Code Online (Sandbox Code Playgroud)\n

然后我们需要添加 swift 代码。
\n我们需要在主机部分写入IP地址。
\n更准确地说,我们将用 IP 地址替换 localhost 部分。

\n
let settings = Firestore.firestore().settings\nsettings.host = "192.168.1.11:8081"\nsettings.isPersistenceEnabled = false\nsettings.isSSLEnabled = false\nFirestore.firestore().settings = settings\n    \nStorage.storage().useEmulator(withHost:"192.168.1.11", port:9200)\nAuth.auth().useEmulator(withHost:"192.168.1.11", port:9100)\nlet db = Database.database(url:"http://192.168.1.11:9001?ns=firebaseappname")\nFunctions.functions().useFunctionsEmulator(origin: "http://192.168.1.11:5002")\n
Run Code Online (Sandbox Code Playgroud)\n

我认为这个解决方案适用于 JS、android 和其他语言。

\n

如果您尝试此解决方案并让我知道它是否有效,我将不胜感激。
\n这对我有用。

\n

  • 这应该是正确的方法。iOS 设备需要能够使用主机 IP(通常是 192.168.xy,而不是 localhost 或 127.0.0.1)通过网络连接到模拟器。如果命令 firebase emulators:start 将 127.0.0.1:xxxx 或 localhost:xxxx 打印为 Host:Port,则需要将主机设置为 firebase.json 中的 IP 192.168.xy。您可以通过 iOS 设备的浏览器打开 192.168.xy:<port> 检查它是否正常工作,其中 <port> 是模拟的 Firebase 服务之一。此外,传入的请求可能会被防火墙阻止。 (2认同)