【问题标题】:Sending Firebase reference to Apple Watch将 Firebase 引用发送到 Apple Watch
【发布时间】:2015-09-13 20:57:35
【问题描述】:

好吧,我的情况有点复杂,但基本上我有一个 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 数据库的权限。

【问题讨论】:

    标签: objective-c swift firebase watchkit apple-watch


    【解决方案1】:

    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")
        }
      })
    }
    

    【讨论】:

    • 2019 年与 WatchConnectivity 有什么推荐?我有一个 iOS 聊天应用程序,它已经在处理从 Firebase 获取的所有聊天、用户、帖子等。我添加了一个带有共享 WCSessionManager 的手表扩展,并将工作请求从手表发送到 iPhone。手机正在回复来自 plist 的当前日期和 iPhone 版本,仅用于测试目的。现在我准备好将聊天消息文本从 iOS 发送到 watchOS。看来我的手表不需要 Firebase 授权。
    【解决方案2】:

    通过 handleWatchKitRequest 方法在手表和 iOS 设备之间移动的所有数据都必须是可序列化的。

    您可以尝试在 Firebase 对象上使用 NSKeyedArchiver 来确定它是否可序列化。

    您是否尝试过将 Firebase 引用的链接作为字符串返回,然后在 Watch 上实例化一个 Firebase 对象? (虽然可能不是在 iOS 和 Watch 应用中监听 firebase 节点的最佳解决方案)

    另外,我建议你看看 NSUserDefaults 和共享容器,这可能是实现你想要做的事情的好方法,这里是链接:https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/WatchKitProgrammingGuide/DesigningaWatchKitApp.html#//apple_ref/doc/uid/TP40014969-CH3-SW4

    【讨论】:

    • 我做了你说的第二个选项,只是返回链接,这对我来说很好。我不认为它会因为 Firebase 引用不会经过身份验证,但不知何故它是。而且我在两个节点上都运行了 Firebase 节点,它似乎工作正常。
    • @Slash705 关于 NSUserDefaults 是正确的。链接是有关如何使用 Firebase 和 Apple Watch firebase.com/docs/ios/libraries/watchkit/…验证用户身份的文档@
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多