iOS Firebase app not configured after calling FIRApp.configure() - ios

I have an iOS (Swift) app with the following code in the AppDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FIRApp.configure()
FIRDatabase.database().persistenceEnabled = true
return true
}
The app crashes on the persistenceEnabled = true line, with exception FIRAppNotConfigured, and the message "Failed to get default FIRDatabase instance. Must call FIRApp.configure() before using FIRDatabase."
Obviously, I've called FIRApp.configure() immediately before this, so the suggested solution is incorrect. The log output even shows "Configuring the default app" when that is called.
What might the problem be, and how could I resolve it so I can use the FIRDatabase?

Try:
override init() {
// Firebase Init
FIRApp.configure()
}

Related

Firebase Crashlytics is not working in iOS

Implemented all steps from firebase doc :
Create App on firebase
add google plist in project
POD installed
Add FirebaseApp.configure() in didFinishLaunchingWithOptions
Added run script ${PODS_ROOT}/Fabric/run
update Debug information format
Please make sure Firebase configuration should be first line in the didFinishLaunchingWithOptions method of AppDelegate file.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
Fabric.sharedSDK().debug = true
return true
}

Why beforeInAppMessageDisplayed is never called?

I want to use Braze to send in-app message notification and show customized UI to replace the UI created by Braze. But beforeInAppMessageDisplayed is never called. Below is how I done.
I added
Appboy.start(withApiKey: apiKey, in: application, withLaunchOptions: launchOptions, withAppboyOptions: [ ABKInAppMessageControllerDelegateKey: self ]) in func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
and also added
func beforeInAppMessageDisplayed(inAppMessage: ABKInAppMessage!) -> ABKInAppMessageDisplayChoice
in AppDelegate class.
I resolved this issue by putting func beforeInAppMessageDisplayed(inAppMessage: ABKInAppMessage!) -> ABKInAppMessageDisplayChoice in ABKInAppMessageControllerDelegate.

Local storage in iphone using firebase

Is there a way where I can use firebase to store data on the iphone device so that even after the user turns off and then turns on the device, the data remains in the device?
Put this in your AppDelegate file
Swift 3
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FIRApp.configure()
FIRDatabase.database().persistenceEnabled = true
return true
}

Private func didFinishLaunchingWithOptions not being called? (Swift 3)

Isn't didFinishLaunchingWithOptions supposed to be called when the app starts running for the first time? I set a breakpoint at this method and when I run the app in the simulator the breakpoint doesn't get hit, which means the method doesn't get called. I'm trying to load some data from UserDefaults whenever the app launches, but it's being completely ignored. One thing I noticed is that it's by default a private func instead of a func. If I get rid of the private, I receive a warning that "there's an almost similar optional requirement in the UIApplicationDelegate". Can someone explain to me what this means and whether or not the private func has anything to do with the method being ignored? Is that method even supposed to be called when I run my app in the simulator? If not, how can I test if data is being retrieved after my app launches? All the other methods in the AppDelegate do get called normally (for example, the applicationDidEnterBackground method works perfectly fine).
Remove your method signature and have Xcode autocomplete it
I also had the problem that my didFinishLaunchingWithOptions method in AppDelegate would not be called. My function was also marked private and looked like this
private func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
The problem is that this is the old syntax! Apparently for me when I converted my project from Swift 2.x to Swift 3 Xcode did not convert the methods in AppDelegate. The new syntax looks like this
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool
Swift 4.2:
func application( _ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
for Swift ~3.0 Replace didFinishLaunchingWithOptions with
follwing signature
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
}
Have you implemented the didFinishLaunchingWithOptions in one of your ViewControllers? One will not get a call to the custom implementation of this method. This method is defined in the ApplicationDelegate and it will always be called once the app is launched. If you haven't defined the method again in any ViewController and the one in AppDelegate is not being called, then try resetting the simulator. From the simulator menu Simulator -> Reset content and settings.
If compiler prompts to make the didFinishLaunchingWithOptions method private then the parameter of the method might be causing the error.
The parameter of the application(_:didFinishLaunchingWithOptions:) delegate method is now bridged to Swift as a [UIApplicationLaunchOptionsKey: Any]?, rather than an [NSObject : AnyObject]?. So modify the method signature as shown.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// ...
}
This method is defined in the ApplicationDelegate and it will always be called once the app is launched. If you have not defined the method again in any ViewController and the one in AppDelegate is not being called, then try resetting the simulator.
Open simulator - > menu Simulator -> Reset content and settings.
-(BOOL)application(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//..
}
Update for Swift 4.2:
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool
Delete app from device and restart Xcode worked for me here

The default app has not been configured yet

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.
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?
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.
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() outside of your declared functions sometimes the code FIRDatabase.database().reference() executes before the application didFinishLaunchingWithOptions function is executed.
Essentially your class is trying to get a reference to the Firebase database before it has a chance to configure itself, generating the error in the console "The default app has not been configured yet."
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.
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)
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.
I'm also using Fabric and in my case it was the order of Fabric and Firebase initializations. I had to initialize Firebase first.
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.
In AppDelegate.m, outside of didFinishLaunchingWithOptions,
override init() {
FIRApp.configure()
FIRDatabase.database().persistenceEnabled = true
}
Make sure you are having DATABASE_URL key in your GoogleService-Info.plist
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
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:
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")
}
I also added the file GoogleService-Info.plist to my project directory as described in the Firebase Getting Started Guide.
And I made my Firebase db public with the following Rules:
{
"rules": {
".read": true,
".write": true
}
}
Making the following changes to ViewController.swift is what worked for me:
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:
myiosdata-abc123: null
After running my app, my Firebase db looked like this:
myiosdata-abc123
- key
|
+--- subkey: “hello world”
I had several normal working projects with FIRApp.configure () code in 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:
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.
I have debug my Xcode (clear and reload console), but nothing works beside this new approach by 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?
Try re-download GoogleService-Info.plist from your console and add it to your project, That worked for me!
If you are using Xcode 9, Swift 4 and Firebase 4 please do the following:
override init() {
FirebaseApp.configure()
Database.database().isPersistenceEnabled = true
}
The cleanest solution to me here is to have lazy properties in 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:
private lazy var db = Firestore.firestore()
Or if you are using Firebase Storage:
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.

Resources