尚未配置默认应用程序

时间:2016-05-19 21:28:33

标签: ios swift firebase

我正在尝试将我的应用升级到新版本的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.'

我在AppDelegate中有FIRApp.configure(),我将GoogleServices-Info.plist导入到我的项目中。 plist也有所有正确的信息。其他任何人遇到这个或知道如何解决它?

11 个答案:

答案 0 :(得分:62)

以下是您问题的答案:

要配置Firebase,您必须在某处执行 FIRApp.configure()。完成此操作后,您可以使用 let firebaseDatabaseReference = FIRDatabase.database()。reference()来获取对该数据库的引用并开始使用它。问题不在于Firebase"本身"但是Swift的表现如何。

如果您将FIRApp.configure()放入AppDelegate func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool,然后放在MyDatabase class中,则使用所声明函数的<{1}} 有时代码let firebaseDatabaseReference = FIRDatabase.database().reference()在执行FIRDatabase.database().reference()函数之前执行。

基本上,您的类正在尝试获取对Firebase数据库的引用,之后它有机会自行配置,在控制台中生成错误&#34;默认应用尚未配置&#34;

注意:这并不是一直发生的,有时应用程序启动很慢,例如在iOS模拟器中,并且它没有机会在MyDatabase和#34之前完成;让&#34;执行并尝试获取参考。

这就是为什么移动FIRApp.configure()代码以覆盖AppDelegate中的init()的原因,实际上它确保在初始化AppDelegate时执行配置代码(在此情况下,在大多数情况下,在MyDatabase初始化之前)

application didFinishLaunchingWithOptions

另外请确保你super.init()(所以你的超级课程得到了&#34;消息&#34;)所以你覆盖并没有弊大于利。

答案 1 :(得分:16)

我还使用Fabric,在我的情况下,这是FabricFirebase初始化的顺序。我必须先初始化Firebase

如此改变

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    Fabric.with([Crashlytics.self])
    FirebaseApp.configure()
    ...
}

收件人:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    FirebaseApp.configure()
    Fabric.with([Crashlytics.self])
    ...
}

解决了这个问题。

答案 2 :(得分:3)

AppDelegate.m以外的didFinishLaunchingWithOptions

override init() {
   FIRApp.configure()
   FIRDatabase.database().persistenceEnabled = true
}

答案 3 :(得分:1)

iOS 9.2
斯威夫特2.1.1
Xcode 7.2.1
Mac OSX 10.10.5

使用以下代码时出现相同的错误:

<强> 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

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")

    }

我还将文件GoogleService-Info.plist添加到我的项目目录中,如Firebase Getting Started Guide中所述。

我使用以下Rules

公开我的Firebase数据库
{
  "rules": {
    ".read": true,
    ".write": true
  }
}

对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")

    }

在运行我的应用之前,我的Firebase数据库看起来像这样:

myiosdata-abc123: null

运行我的应用后,我的Firebase数据库看起来像这样:

myiosdata-abc123
- key
   |
   +--- subkey: “hello world”

答案 4 :(得分:1)

我在AppDelegate.swift中使用class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { FIRApp.configure() return true } 代码进行了几个正常的工作项目:

Swift 3

很长一段时间以来一切都很顺利,但是昨天和今天我在Xcode 7.3.1内开了一个Swift 2项目(我仍然在Swift 3工作,但是打开了Swift 2项目,看看有什么变化),突然在我仍在努力的所有override init() { FIRApp.configure() FIRDatabase.database().persistenceEnabled = true } 应用程序和项目中,得到了同样的错误:

  

尚未配置默认应用

现在我在XCode中打开每个项目,得到同样的错误,我不知道该怎么做,但在实现了@MichaelWilliams的代码后,一切正常。

我已经调试了我的Xcode(清除并重新加载控制台),但Michael没有采用这种新方法。

这个解决了我的问题:

{{1}}

这个新代码能否以某种方式使我的应用程序不稳定,我现在可能会害怕看到连接/使用Firebase数据库时出现问题?

答案 5 :(得分:1)

尝试从您的控制台重新下载 GoogleService-Info.plist 并将其添加到您的项目中,这对我有用!

答案 6 :(得分:0)

确保DATABASE_URL

中有GoogleService-Info.plist个密钥

答案 7 :(得分:0)

如果您使用的是Xcode 9,Swift 4和Firebase 4,请执行以下操作:

override init() {
    FirebaseApp.configure()
    Database.database().isPersistenceEnabled = true
}

答案 8 :(得分:0)

对于我来说,最干净的解决方案是具有惰性属性,以防您希望将数据库放在文件顶部。因此,假设您有一个FirebaseService类,希望在其中拥有Firestore.firestore() db常量以在该类的所有函数中使用它:

private lazy var db = Firestore.firestore()

或者如果您使用的是Firebase Storage:

private lazy var storage = Storage.storage().reference()

还要记住,如果您在类的init()中引用数据库/存储,您仍然可能会遇到相同的问题,因此请避免这种情况。

答案 9 :(得分:0)

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
}

快乐编码

答案 10 :(得分:0)

雨燕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
}

快乐编码

相关问题