Reputation: 187
I'm making a login app that tracks whether someone is logged in or not. I'm using firebase as a way to do this. if a person is not in firebase, they cannot be logged in.
PROBLEM
I keep getting the console error
<FIRInstanceID/WARNING> Failed to fetch default token Error Domain=com.firebase.iid Code=6 "(null)"
when I simulate the app. when I try to log in as Ash Dreyer
, it does not log me in. I click it several times so even though I am logged in in firebase, it should log me out and log me back in again.
FIREBASE
{
"Ash Dreyer" : {
"activity" : "None",
"current_status" : "IN",
"num_of_logins" : 0,
"total_hours" : 0,
"type" : "mentor"
},
"Bryton Moeller" : {
"activity" : "None",
"current_status" : "OUT",
"num_of_logins" : 0,
"total_hours" : 0,
"type" : "student"
}
}
APP DELEGATE
import UIKit
import Firebase
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
override init() {
FIRApp.configure()
}
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
FIRApp.configure()
return true
}
}
LOGINVIEWCONTROLLER
import Firebase
import UIKit
class LoginViewController: UIViewController {
@IBOutlet weak var activity_label: UILabel!
@IBOutlet weak var activity_picker: UIPickerView!
@IBOutlet weak var name_field: UITextField!
@IBOutlet weak var login_button: UIButton!
@IBOutlet weak var location_switch: UISwitch!
var root_ref = FIRDatabase.database().reference()
// Color scheme
let grey = UIColor(red: 0.933, green: 0.929, blue: 0.922, alpha: 1.0)
let dark_orange = UIColor(red: 0.769, green: 0.396, blue: 0.176, alpha: 1.0)
let dark_blue = UIColor(red: 0.188, green: 0.463, blue: 0.541, alpha: 1.0)
let light_blue = UIColor(red: 0.412, green: 0.663, blue: 0.686, alpha: 1.0)
let light_orange = UIColor(red: 0.871, green: 0.612, blue: 0.451, alpha: 1.0)
var user: Person!
func update_UI() {
if let _ = user {
if user!.current_status == "IN" {
login_button.setTitleColor(dark_blue, for: UIControlState.normal)
login_button.setTitle("LOG OUT", for: UIControlState.normal)
} else {
login_button.setTitleColor(dark_orange, for: UIControlState.normal)
login_button.setTitle("LOG IN", for: UIControlState.normal)
}
}
}
func login(member: inout Person) {
if member.current_status == "IN" {
member.current_status = "OUT"
} else {
member.current_status = "IN"
}
root_ref.child(byAppendingPath: member.key).updateChildValues(["current_status": member.current_status]);
update_UI()
}
func login_new_user(member_name: String) {
root_ref.observe(FIRDataEventType.value, with: { (snapshot) -> Void in
for member in snapshot.children {
if member.key == member_name {
self.user = Person(snapshot: member as! FIRDataSnapshot)
}
}
})
if let _ = user {
login(member: &user!)
}
}
@IBAction func login_user(_ sender: UIButton) {
if let _ = user, let name_text = name_field.text, name_text != "" {
if user.key != name_text {
login_new_user(member_name: name_text)
}
} else if let name_text = name_field.text, name_text != "" {
login_new_user(member_name: name_text)
} else if let _ = user {
login(member: &user!)
} else {
print("Please enter text into the text field")
}
}
@IBAction func toggle_location(_ sender: UISwitch) {
}
@IBAction func other_login(_ sender: UIButton) {
}
}
NOTES
FIRApp.configure()
in func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
, it gave me a SIGABRT error.Upvotes: 2
Views: 3976
Reputation: 1541
Just to update, these errors should no longer appear in the latest version of Firebase when using the Xcode 8.2+ version of the simulator. There was an issue (in fact a couple) with the simulator, and some overly aggressive logging in InstanceID.
Upvotes: 1