Where to add FirebaseApp.configure() in SpriteKit - ios

So I am attempting to integrate Google's Firebase into my SpriteKit game and was having a small issue.
The instruction from Firebase is..."Configure a FIRApp shared instance, typically in your application's application:didFinishLaunchingWithOptions: method:FirebaseApp.configure()".
Now I've located the file called AppDelegate.swift in my Xcode project but when I place it into the first function, which matches the named one in the instructions I get the following error Use of unresolved Identifier FirebaseApp.
And I remembered to import Firebase at the top of document. Any suggestions?

It is FIRApp, not FirebaseApp.
FIRApp.configure()
Note: Don't forget to import the Firebase module.
import Firebase

Your app delegate needs to look something like this:
import UIKit
import Firebase
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
FIRApp.configure()
return true
}
}
Make sure you call FIRApp.configure() before the return true and remember to import Firebase.

Related

Connecting to Firebase in IOS

Following various tutorials most of which seem to be from a couple years ago, I am trying to connect to Firebase from an IOS app and having trouble with how and where to import the libraries and, in turn, connect to the database.
In my app delegate, I am able to establish a connection--I think---inside the difFinishLaunching method (I say I think because Firebase does not recognize the app in the console but this seems to be a frequent occurrence so I'm ignoring that and just trying to get the app to build in Xcode for now) as follows:
import UIKit
import FirebaseAnalytics
import FirebaseDatabase
import FirebaseCore
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
let myDB = Database.database().reference().child("items")
}
return true
}
That seems okay in that it builds.
When I try to put a reference to the database in a viewcontroller, however, it throws an error on Database:
import UIKit
import FirebaseCore
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let ref = Database.database().reference(withPath: "items") //THIS LINE THROWS ERROR
What do I need to do to create a property for the Database in a view controller?
Edit: Looking at some other answers on SO, it seems I may have to create a singleton reference to the db connection. If so, where would I put this and how would I reference it?
Thanks for any suggestions
First, your AppDelegate should look something like this
import UIKit
import Firebase
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
return true
}
then, in your first viewController like this:
import UIKit
import Firebase
class ViewController: UIViewController {
var db: Firestore!
override func viewDidLoad() {
super.viewDidLoad()
self.db = Firestore.firestore()
//do something with Firestore
var ref: DocumentReference? = nil
ref = db.collection("users").addDocument(data: [
"first": "Ada",
"last": "Lovelace",
"born": 1815
]) { err in
if let err = err {
print("Error adding document: \(err)")
} else {
print("Document added with ID: \(ref!.documentID)")
}
}
}
}
Keep in mind I have set my Firestore rules to allow anyone to read/write, which is dangerous so please start working through the Authentication guide once you get this working.
Note that the above code pretty much came straight from the Getting Started guides Add Firebase To An App and Installation and setup

How to use Firebase Remote Config in AppDelegate.swift?

Based on advice, I would like to manage API keys using Firebase Remote Config to avoid hard-coding API keys like google_maps_flutter suggests. It has an AppDelegate.swift like:
import UIKit
import Flutter
import GoogleMaps
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GMSServices.provideAPIKey("YOUR KEY HERE")
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
How can the above be modified to fetch the API key from Firebase Remote Config and then pass it to GMSServices?
Based on this article, I came up with:
import UIKit
import Firebase
import Flutter
import GoogleMaps
//import os.log
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
FirebaseApp.configure()
RemoteConfig.remoteConfig().fetchAndActivate() { status, error in
let apiKey : String = RemoteConfig.remoteConfig()["Google_Maps_SDK_for_iOS_API_KEY"].stringValue ?? "MISSING";
// os_log("Google_Maps_SDK_for_iOS_API_KEY = '%#'", apiKey)
GMSServices.provideAPIKey(apiKey)
}
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
Is there a better way?
I was thinking of making this as a comment, but decided to make it as my answer as it was too long.
I think it's one way of getting keys from remoteConfig, but again failure can happen where fetch config was not retrieved. One way to solve it is by having a force refresh when it fails, but then again you are relying on firebase for those keys and if for some reason (experienced it before firebase was acquired by google) it went down your app then will be unuseable (so as for many apps).
For me, I still put my API keys bundled with the app just to make sure all the important functionality works.
Another option will be having your API Keys bundled and then having a WebService call to check for new keys once the current keys you have has expired/change. That way you have the capability to immediately expire your keys and change it to another one.

UIApplication delegate must be used from main thread only

I have a standard AppDelegate file in my Xcode project, I'm experimenting with Firebase and as soon as I imported it & added
FirebaseApp.configure()
I was flagged with purple warnings: Click me
The whole AppDelegate looks as follows
import UIKit
import Firebase
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
return true
}
These warnings never appeared whilst using Xcode 8, as a novice I am unsure on how to fix this, I have read that adding a DispatchQueue.main.async can fix it but I'm not sure where to add this.
Thanks!

Firebase configration is crashing in swift

App is crashing while configure firebase,Unable to resolve
Error:libc++abi.dylib: terminating with uncaught exception of type NSException
Code:
import UIKit
import Firebase
import FirebaseDatabase
import CoreData
import IQKeyboardManagerSwift
let mainUrl = "https://gopolly.com/well.php/"
#UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate
{
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
FirebaseApp.configure()
IQKeyboardManager.sharedManager().enable=true
return true
}
Check if the application works without Firebase. Delete or comment out the lines of connection to the Firebase.
If the application is working without errors, then go back to the initial code and check the GoogleService-Info.plist. The BUNDLE_ID must be the same as the Bundle Identifier of the application.

Firebase: FIRApp.configure() keeps wanting me to change it to FirebaseApp.configure()

So I made a project that included lots of extra code so I could try out different things, and I am slowly copy-pasting the files from textedit into my new project.
I created a new Firebase account, added the google plist file, initialized and installed the pods needed (also changed the iOS version to 10.0 on the pod file)... yet it's still wanting me to use FirebaseApp.configure() instead of FIRApp.configure().
Should I just delete the whole thing and try again? I just created a new Xcode project, so if I had to delete it right now that would be fine.
import UIKit
import Firebase
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
override init() {
super.init()
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FIRApp.configure()
return true
}
There hasn't been much code written, but I can't figure out what could have made this happen... especially since I just started the new project.
FirebaseApp.configure() is correct. I checked out the documentation just now and FIRApp.configure() has been renamed FirebaseApp.configure(). I also ran pod update on one of my projects just to confirm, and my project had me change to using FirebaseApp.configure(). So no need to rebuild!
I can confirm that FIRApp.configure() can be changed to FirebaseApp.configure()
In firebase it suggest to add the following to app delegate:
"
import UIKit import Firebase
#UIApplicationMain class AppDelegate: UIResponder,
UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?)
-> Bool {
FirebaseApp.configure()
return true } }
"
This includes the FirebaseApp.configure() command

Resources