I wanted to try following some online tutorials for making apps connecting to Firebase, but they are outdated as they are still using FIRApp.configure()
, etc. Therefore I have to change the code correspondingly and not able to have a complete workable version of such app.
By now, I have already:
Read through the documentation from Firebase
How to define the "ref" variable in AppDelegate (How to fix Firebase SIGABRT crash iOS),
How to set that variable in AppDelegate to be a global variable and readable in other ViewControllers (How do I get a reference to the app delegate in Swift?),
Checked if I have included the correct pod files, and set the value of the variable inside viewDidLoad section (Where do I add the database reference from Firebase on iOS?),
Noticed that pod files might need to be updated, performed pod disintegrate and pod update (Get a database reference in the new Firebase 4.0 release).
For podfile, I have included:
pod 'Firebase/Core'
pod 'Firebase/Database'
For AppDelegate, (parts not shown are as default)
import UIKit
import CoreData
import Firebase
import FirebaseDatabase //tried not having this line, still the same
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var ref: DatabaseReference!
func application(_application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
self.ref = Database.database().reference()
return true
}
For ViewController, (parts not shown are as default or either certainly not related)
import UIKit
import Firebase
import FirebaseDatabase //again, tried with/without this line
class ViewController: UIViewController {
@IBOutlet weak var emailfield: UITextField!
@IBOutlet weak var usrnamefield: UITextField!
@IBOutlet weak var pwdfield: UITextField!
@IBOutlet weak var loginbtn: UIButton!
@IBOutlet weak var createnewaccbtn: UIButton!
@IBOutlet weak var forgetpwdbtn: UIButton!
//......
@IBAction func createnewAcc(_ sender: UIButton) {
let username = usrnamefield.text
let email = emailfield.text
let password = pwdfield.text
if username != "" && email != "" && password != "" {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.ref.child("users").setValue(["username": username]) //this line is highlighted with the error below
//Cannot even update one single field to the database
}
}
}
But when I run the Simulator, the error says
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
Reference:
Xcode 9.2, CocoaPods 1.4.0, Deployment Target: 9.2, 10.3
Update (2018 Feb 27, GMT 18:00):
Tried defining databaseReference
variable in the ViewController
directly, and assigned value using databaseReference = Database.database().reference()
inside viewDidLoad
.
It turns out to receive signal SIGABRT
in AppDelegate (class AppDelegate
line). Disabling the above value assignment code will solve the signal SIGABRT
problem, but still cannot connect to Firebase Database...