将 Firebase 参考发送到 Apple Watch
Sending Firebase reference to Apple Watch
好吧,我的情况有点复杂,但基本上我有一个 Apple Watch 扩展程序与我的应用程序连接,它使用 Firebase 接收和发送数据。我希望我的手表具有与应用程序相同的功能,唯一的问题是我需要使用相同的 Firebase 参考,因为我使用 Google 身份验证并且我需要保持身份验证数据相同(除非有其它的办法)。所以我让手表做的是最初请求一些数据:所选计时器的索引(它是一个计时器应用程序)和 Firebase 参考变量。但是当 iPhone 应用程序使用 reply() 函数并发回像这样的字典时:
reply(["replyInfoType": "watchInfo", "selectedTimerKey": keySelected, "refFirebase": ref])
ref
变量定义为 var ref = Firebase(url:"https://my-app-url.firebaseio.com/")
。
编译没有错误,但是当我 运行 watch 扩展并附加到应用程序进程时,我的应用程序在这里抛出一个很好的 SIGABRT:
日志中根本没有打印任何内容。在我取消暂停进程后,应用程序崩溃了。我不知道这是从哪里来的。如果我不在字典中包含 ref
变量,它 运行 没问题,除了我的手表应用程序没有读取 Firebase 数据库的权限这一事实。
通过 handleWatchKitRequest 方法在您的手表和 iOS 设备之间移动的所有数据必须是可序列化的。
您可以尝试在您的 Firebase 对象上使用 NSKeyedArchiver 来确定它是否可序列化。
您是否尝试过将 Firebase 引用的 link 作为字符串返回,然后在 Watch 上实例化一个 Firebase 对象? (虽然可能不是在 iOS 和 Watch 应用程序中监听 firebase 节点的最佳解决方案)
另外,我建议你看看 NSUserDefaults 和共享容器,这可能是实现你想要做的事情的好方法,这里是 link:https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/WatchKitProgrammingGuide/DesigningaWatchKitApp.html#//apple_ref/doc/uid/TP40014969-CH3-SW4
有official documentation for Firebase and the Apple Watch on user authentication.
为主机应用和扩展启用应用组。然后在用户登录时使用 NSUserDefaults
将用户的身份验证令牌保存在主机应用程序中。
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()
}
}
在 Watch App Extension 中,从 NSUserDefaults
检索身份验证令牌并在 awakeWithContext
函数中调用 authWithCustomToken
。
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")
}
})
}
好吧,我的情况有点复杂,但基本上我有一个 Apple Watch 扩展程序与我的应用程序连接,它使用 Firebase 接收和发送数据。我希望我的手表具有与应用程序相同的功能,唯一的问题是我需要使用相同的 Firebase 参考,因为我使用 Google 身份验证并且我需要保持身份验证数据相同(除非有其它的办法)。所以我让手表做的是最初请求一些数据:所选计时器的索引(它是一个计时器应用程序)和 Firebase 参考变量。但是当 iPhone 应用程序使用 reply() 函数并发回像这样的字典时:
reply(["replyInfoType": "watchInfo", "selectedTimerKey": keySelected, "refFirebase": ref])
ref
变量定义为 var ref = Firebase(url:"https://my-app-url.firebaseio.com/")
。
编译没有错误,但是当我 运行 watch 扩展并附加到应用程序进程时,我的应用程序在这里抛出一个很好的 SIGABRT:
ref
变量,它 运行 没问题,除了我的手表应用程序没有读取 Firebase 数据库的权限这一事实。
通过 handleWatchKitRequest 方法在您的手表和 iOS 设备之间移动的所有数据必须是可序列化的。
您可以尝试在您的 Firebase 对象上使用 NSKeyedArchiver 来确定它是否可序列化。
您是否尝试过将 Firebase 引用的 link 作为字符串返回,然后在 Watch 上实例化一个 Firebase 对象? (虽然可能不是在 iOS 和 Watch 应用程序中监听 firebase 节点的最佳解决方案)
另外,我建议你看看 NSUserDefaults 和共享容器,这可能是实现你想要做的事情的好方法,这里是 link:https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/WatchKitProgrammingGuide/DesigningaWatchKitApp.html#//apple_ref/doc/uid/TP40014969-CH3-SW4
有official documentation for Firebase and the Apple Watch on user authentication.
为主机应用和扩展启用应用组。然后在用户登录时使用 NSUserDefaults
将用户的身份验证令牌保存在主机应用程序中。
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()
}
}
在 Watch App Extension 中,从 NSUserDefaults
检索身份验证令牌并在 awakeWithContext
函数中调用 authWithCustomToken
。
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")
}
})
}