如何通过Swift连接到本地Firebase服务器?
我真的想通过利用本地安装firebase-server来编写一些集成测试,它将模仿实时数据库服务器。如何通过Swift连接到本地Firebase服务器?
我有installed并从CLI开始吧: node_modules/.bin/firebase-server -p 5000
但我通过SWIFT连接到它的麻烦。 按照Firebase文档,需要下载项目中的利用GoogleService-Info.plist
。这显然必须改变,因为我想连接到本地服务器。
这就是我现在所拥有的在AppDelegate.swift
:
var firebaseConfig: String!
if isRunningTests() {
// unit test server (idealy local)
firebaseConfig = Bundle.main.path(forResource: "GoogleService-Info-tests", ofType: "plist")
} else {
#if DEBUG
// staging server
firebaseConfig = Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist")
#else
// production server
firebaseConfig = Bundle.main.path(forResource: "GoogleService-Info-prod", ofType: "plist")
#endif
}
guard let options = FIROptions(contentsOfFile: firebaseConfig) else {
fatalError("Invalid Firebase configuration file.")
}
FIRApp.configure(with: options)
FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
FIRDatabase.database().persistenceEnabled = true
这意味着单元测试我加载GoogleService-Info-tests
plist,以连接到远程测试数据库。我怎么能连接到ws://test.firebase.localhost:5000
?我不清楚options
中的哪些变量是强制性的,需要提供。
您可以使用FIROptions init!(googleAppID: String!, bundleID: String!, gcmSenderID GCMSenderID: String!, apiKey APIKey: String!, clientID: String!, trackingID: String!, androidClientID: String!, databaseURL: String!, storageBucket: String!, deepLinkURLScheme: String!)
方法,并从AppId,bundleId,SenderID传入一个真实应用程序,然后传递databaseURL,它可以是本地URL。你应该能够通过nil
到其他人,并罚款(虽然你可能不得不沉默编译器警告,因为他们似乎被标记为不可空的Swift)。
或者您可以更改GoogleService-Info-tests.plist
中的一个字段以指向新的数据库。
github上的相关问题:https://github.com/urish/firebase-server/issues/85 – urish