Reputation: 123
Alright so my situation here is a little complicated, but basically I have a an Apple Watch extension connected with my app, which uses Firebase to receive and send data. I want the same functionality in my watch as I do the app, the only problem is that I need to use the same Firebase reference because I use Google Authentication and I need to keep the auth data the same (unless there is another way). So what I have the watch do is request some data initially: the index of the selected timer (it's a timer app) and the Firebase reference variable. But when the iPhone app uses the reply() function and sends back a dictionary like so:
reply(["replyInfoType": "watchInfo", "selectedTimerKey": keySelected, "refFirebase": ref])
The ref
variable is defined as var ref = Firebase(url:"https://my-app-url.firebaseio.com/")
.
There are no errors compiling but when I run the watch extension and attach to the application process as well, my application throws a nice SIGABRT here:
Nothing is printed in the log at all. The app just crashes after I unpause the process. I have no leads on where this is coming from. If I don't include the
ref
variable in the dictionary it runs fine except for the fact that my watch app doesn't have permission to read the Firebase Database.
Upvotes: 2
Views: 1330
Reputation: 32604
There is official documentation for Firebase and the Apple Watch on user authentication.
Enable App Groups for both the host app and the extension. Then use NSUserDefaults
to save the user's authentication token in the host app when they log in.
let defaults = NSUserDefaults(suiteName: "group.username.SuiteName")!
ref.observeAuthEventWithBlock { [unowned self] (authData: FAuthData!) in
if authData != nil {
defaults.setObject(authData.token, forKey: "FAuthDataToken")
defaults.synchronize()
}
}
In the Watch App Extension, retrieve the auth token from NSUserDefaults
and call authWithCustomToken
in the awakeWithContext
function.
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
ref = Firebase(url: "https://<your-firebase-app>.firebaseio.com/updates")
updates = [FDataSnapshot]()
// Use the same suiteName as used in the host app
let defaults = NSUserDefaults(suiteName: "group.username.SuiteName")!
// Grab the auth token
let authToken = defaults.objectForKey("FAuthDataToken") as? String
// Authenticate with the token from the NSUserDefaults object
ref.authWithCustomToken(authToken, withCompletionBlock: { [unowned self] (error: NSError!, authData: FAuthData!) in
if authData != nil {
println("Authenticated inside of the Watch App!")
} else {
println("Not authenticated")
}
})
}
Upvotes: 3
Reputation: 56
All datas moving between your watch and your iOS device through handleWatchKitRequest method must be serializable.
You can probably try to use NSKeyedArchiver on your Firebase object to find out if it's serializable or not.
Have you tried returning the link of your Firebase reference as a string and then instantiate a Firebase object on the Watch? (might not be the best solution to listen on firebase nodes in both your iOS and Watch app, though)
Also, I suggest you to have a look at NSUserDefaults and shared containers, might be a good way to achieve what you want to do, here is the link : https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/WatchKitProgrammingGuide/DesigningaWatchKitApp.html#//apple_ref/doc/uid/TP40014969-CH3-SW4
Upvotes: 1