ios 尚未配置默认应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37334323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The default app has not been configured yet
提问by Michael Williams
I'm trying to upgrade my app to the new version of Firebase. I went through the setup guide and edited all of my code to match the new syntax. However, when I run the app, I get these two errors.
我正在尝试将我的应用升级到新版本的 Firebase。我浏览了设置指南并编辑了我的所有代码以匹配新语法。但是,当我运行该应用程序时,出现了这两个错误。
The default app has not been configured yet.
Terminating app due to uncaught exception 'MissingDatabaseURL', reason: 'Failed to get FIRDatabase instance: FIRApp object has no databaseURL in its FirebaseOptions object.'
I have FIRApp.configure()
in the AppDelegate and I have the GoogleServices-Info.plist imported into my project. The plist has all of the correct info as well. Anyone else running into this or know how to fix it?
我 FIRApp.configure()
在 AppDelegate 中有 GoogleServices-Info.plist 导入到我的项目中。plist 也包含所有正确的信息。还有其他人遇到过这个问题或知道如何解决吗?
回答by unom
Here's the answer to your problem:
这是您问题的答案:
To configure Firebase you have to execute FIRApp.configure()somewhere. After this is done you can use let firebaseDatabaseReference = FIRDatabase.database().reference()to get a reference to that database and start using it. The problem isn't with Firebase "per se" but with how Swift behaves.
要配置 Firebase,您必须在某处执行FIRApp.configure()。完成此操作后,您可以使用let firebaseDatabaseReference = FIRDatabase.database().reference()获取对该数据库的引用并开始使用它。问题不在于 Firebase“本身”,而在于 Swift 的行为方式。
If you put FIRApp.configure()
in your AppDelegate func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
and then in the MyDatabase class
you use let firebaseDatabaseReference = FIRDatabase.database().reference()
outsideof your declared functions sometimesthe code FIRDatabase.database().reference()
executes before the application didFinishLaunchingWithOptions
function is executed.
如果你放入FIRApp.configure()
你的 AppDelegatefunc application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
然后MyDatabase class
你let firebaseDatabaseReference = FIRDatabase.database().reference()
在你声明的函数之外使用,有时代码会在函数FIRDatabase.database().reference()
执行之前application didFinishLaunchingWithOptions
执行。
Essentially your class is trying to get a reference to the Firebase database beforeit has a chance to configure itself, generating the error in the console "The default app has not been configured yet."
本质上,您的类试图在有机会自行配置之前获取对 Firebase 数据库的引用,从而在控制台中生成错误“尚未配置默认应用程序”。
Note: This doesn't happen all the time, sometimes the application is slow to start, in iOS Simulator for example, and it doesn't have a chance to finish before MyDatabase "let" executes and tries to get a reference.
注意:这不会一直发生,有时应用程序启动很慢,例如在 iOS 模拟器中,并且它没有机会在 MyDatabase “让”执行并尝试获取引用之前完成。
That is why moving the FIRApp.configure() code to override init() in AppDelegate works, essentially it makes sure the configure code gets executed when AppDelegate is initialised (in this and most cases, before MyDatabase is initialised)
这就是为什么移动 FIRApp.configure() 代码以覆盖 AppDelegate 中的 init() 工作的原因,本质上它确保在 AppDelegate 初始化时执行配置代码(在这种情况下和大多数情况下,在 MyDatabase 初始化之前)
override init() {
super.init()
FIRApp.configure()
// not really needed unless you really need it FIRDatabase.database().persistenceEnabled = true
}
Also make sure you super.init() (so you super classes get the "message") so you override doesn't do more harm than good.
还要确保你 super.init() (所以你的超级类得到“消息”)所以你覆盖不会弊大于利。
回答by algrid
I'm also using Fabric
and in my case it was the order of Fabric
and Firebase
initializations. I had to initialize Firebase
first.
我还使用Fabric
在我的情况下,它是秩序Fabric
和Firebase
初始化。我必须先初始化Firebase
。
So changing
如此变化
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Fabric.with([Crashlytics.self])
FirebaseApp.configure()
...
}
to:
到:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
Fabric.with([Crashlytics.self])
...
}
fixed the problem.
解决了这个问题。
回答by Michael Williams
In AppDelegate.m
, outside of didFinishLaunchingWithOptions
,
在AppDelegate.m
,之外didFinishLaunchingWithOptions
,
override init() {
FIRApp.configure()
FIRDatabase.database().persistenceEnabled = true
}
回答by 7stud
iOS 9.2
Swift 2.1.1
Xcode 7.2.1
Mac OSX 10.10.5
iOS 9.2
Swift 2.1.1
Xcode 7.2.1
Mac OSX 10.10.5
Same error here using the following code:
使用以下代码此处出现相同错误:
AppDelegate.swift:
AppDelegate.swift:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
FIRApp.configure()
return true
}
ViewController.swift:
ViewController.swift:
import UIKit
import Firebase
class ViewController: UIViewController {
var db = FIRDatabase.database().reference()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//Create some data in Firebase db:
db.child("key").child("subkey").setValue("hello world")
}
I also added the file GoogleService-Info.plist
to my project directory as described in the Firebase Getting Started Guide.
我还GoogleService-Info.plist
按照Firebase 入门指南中的说明将该文件添加到了我的项目目录中。
And I made my Firebase db public with the following Rules:
我使用以下规则公开了我的 Firebase 数据库:
{
"rules": {
".read": true,
".write": true
}
}
Making the following changes to ViewController.swift is what worked for me:
对 ViewController.swift 进行以下更改对我有用:
class ViewController: UIViewController {
var db: FIRDatabaseReference!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
db = FIRDatabase.database().reference()
db.child("key").child("subkey").setValue("hello world")
}
Prior to running my app, my Firebase db looked like this:
在运行我的应用程序之前,我的 Firebase 数据库如下所示:
myiosdata-abc123: null
After running my app, my Firebase db looked like this:
运行我的应用程序后,我的 Firebase 数据库如下所示:
myiosdata-abc123
- key
|
+--- subkey: “hello world”
回答by Pejo Zmaj
I had several normal working projects with FIRApp.configure ()
code in AppDelegate.swift
:
我有几个正常的工作项目,FIRApp.configure ()
代码在AppDelegate.swift
:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
FIRApp.configure()
return true
}
Everything worked great for quite some time, but yesterday and today I opened a Swift 3
project inside my Xcode 7.3.1
(I am still working in Swift 2
, but opened Swift 3
project to see what is changed), and suddenly in all my Swift 2
apps and projects that I am still working on, got the same error:
在相当长的一段时间内一切都很好,但是昨天和今天我Swift 3
在我的内部打开了一个项目Xcode 7.3.1
(我仍在工作Swift 2
,但打开了Swift 3
项目以查看发生了什么变化),突然在我Swift 2
仍在开发的所有应用程序和项目中,得到了同样的错误:
The default app has not been configured yet
尚未配置默认应用程序
Every project now when I open in XCode, getting same error, I didn't know what to do, but after implementing @MichaelWilliams' code, everything works fine again.
现在,当我在 XCode 中打开每个项目时,都会遇到同样的错误,我不知道该怎么办,但是在实施 @MichaelWilliams 的代码后,一切又正常了。
I have debug my Xcode (clear and reload console), but nothing works beside this new approach by Michael.
我已经调试了我的 Xcode(清除并重新加载控制台),但除了 Michael 的这种新方法之外没有任何工作。
This one resolved my problem:
这个解决了我的问题:
override init() {
FIRApp.configure()
FIRDatabase.database().persistenceEnabled = true
}
Can this new code somehow make my app's unstable and can I be afraid to see problems with connecting/working with Firebase database now?
这个新代码会以某种方式使我的应用程序不稳定吗,我现在是否会害怕看到连接/使用 Firebase 数据库的问题?
回答by Zaid Pathan
Make sure you are having DATABASE_URL
key in your GoogleService-Info.plist
确保你有DATABASE_URL
钥匙GoogleService-Info.plist
回答by CommittedEel
If you are using Xcode 9, Swift 4 and Firebase 4 please do the following:
如果您使用的是 Xcode 9、Swift 4 和 Firebase 4,请执行以下操作:
override init() {
FirebaseApp.configure()
Database.database().isPersistenceEnabled = true
}
回答by Vasil Garov
The cleanest solution to me here is to have lazy propertiesin case you want to have the db on top of your file. So let's say you have a FirebaseService class where you want to have Firestore.firestore()
db constant to use it in all of the functions in that class:
对我来说,这里最干净的解决方案是使用惰性属性,以防您想将数据库放在文件顶部。因此,假设您有一个 FirebaseService 类,您希望在该类中Firestore.firestore()
使用 db 常量以在该类中的所有函数中使用它:
private lazy var db = Firestore.firestore()
Or if you are using Firebase Storage:
或者,如果您使用的是 Firebase 存储:
private lazy var storage = Storage.storage().reference()
Also keep in mind that if you are referencing the Database/Storage in init()
of your classes, you still might have the same problem so avoid that.
还要记住,如果您在类中引用数据库/存储init()
,您仍然可能遇到相同的问题,因此请避免这种情况。
回答by Shakeel Ahmed
Swift 5 - Easy Solution
Swift 5 - 简单的解决方案
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
return true
}
//MARK:- This function will auto run and firebase will configure successfully
override init() {
super.init()
FirebaseApp.configure()
// not really needed unless you really need it
FIRDatabase.database().persistenceEnabled = true
}
Happy Coding
快乐编码
回答by Shakeel Ahmed
Swift 5
斯威夫特 5
pod 'Firebase/Analytics'Swift 5 - Easy Solution
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
return true
}
//MARK:- This function will auto run and firebase will configure successfully
override init() {
super.init()
FirebaseApp.configure()
// not really needed unless you really need it
FIRDatabase.database().persistenceEnabled = true
}
Happy Coding
快乐编码