swift - 结合 WatchConnectivity 和并发症

标签 swift watchos-2 watchconnectivity apple-watch-complication wcsession

我希望我的复杂功能通过 Watch Connectivity 从 iPhone 获取数据。我正在使用 sendMessage 即时消息技术。

我不想在尝试获取数据时打开我的 iPhone 应用程序,因此这需要在后台运行。

在我 iPhone 上的 ViewController 中:

import UIKit
import WatchConnectivity

class ViewController: UIViewController, WCSessionDelegate {

var session: WCSession!

override func viewDidLoad() {
    super.viewDidLoad()
    if WCSession.isSupported() {
        self.session = WCSession.defaultSession()
        self.session.delegate = self
        self.session.activateSession()
    }
}

func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
    if message.count != 1 { return }

    if message["request"] != nil {
        replyHandler(["response" : "data"])
    }
}

在我的 ComplicationController 中

var session: WCSession!

func getCurrentTimelineEntryForComplication(complication: CLKComplication, withHandler handler: ((CLKComplicationTimelineEntry?) -> Void)) {
    if complication.family != .ModularSmall {
        handler(nil)
    }

    if WCSession.isSupported() {
        self.session = WCSession.defaultSession()
        self.session.delegate = self
        self.session.activateSession()
    }

    var respondedString = "not"

    session.sendMessage(["request" : ""], replyHandler: {
        (resp) -> Void in
        respondedString = resp["response"]
    }, errorHandler: nil)

    let circularTemplate = CLKComplicationTemplateModularSmallSimpleText()
    circularTemplate.textProvider = CLKSimpleTextProvider(text: respondedString)
    let timelineEntry = CLKComplicationTimelineEntry(date: NSDate(), complicationTemplate: circularTemplate)
    handler(timelineEntry)
}

我在 watch 上唯一能看到的是“不”。为什么复杂功能不显示接收到的数据?

最佳答案

主要问题是您正试图在您的并发症 Controller 中进行异步调用。

sendMessage: 调用之后的代码将在您的回复处理程序收到响应之前执行。这就是为什么您的并发症显示为“不”,因为在您收到回复之前已设置模板的文本。

一段时间后,在 getCurrentTimelineEntryForComplication 返回后,sendMessage 将收到一个响应并调用回复处理程序,它只会设置 respondedString,然后退出那个街区。

您应该避免做的事情:

你应该考虑Apple's recommendations ,而不是尝试在并发症 Controller 中获取任何数据。

The job of your data source class is to provide ClockKit with any requested data as quickly as possible. The implementations of your data source methods should be minimal. Do not use your data source methods to fetch data from the network, compute values, or do anything that might delay the delivery of that data. If you need to fetch or compute the data for your complication, do it in your iOS app or in other parts of your WatchKit extension, and cache the data in a place where your complication data source can access it. The only thing your data source methods should do is take the cached data and put it into the format that ClockKit requires.

此外,您在数据源中执行的任何事件都将不必要地用完分配给您的复杂功能的每日执行时间预算。

您如何为您的并发症提供数据?

Apple 提供了一个 Watch Connectivity transferCurrentComplicationUserInfo 方法,它将 immediately将(字典)复杂功能信息从手机传输到 watch 。

When your iOS app receives updated data intended for your complication, it can use the Watch Connectivity framework to update your complication right away. The transferCurrentComplicationUserInfo: method of WCSession sends a high priority message to your WatchKit extension, waking it up as needed to deliver the data. Upon receiving the data, extend or reload your timeline as needed to force ClockKit to request the new data from your data source.

在 watch 端,您有 WCSessionDelegate 句柄 didReceiveUserInfo 并使用您收到的数据来更新您的复杂功能:

func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {
    if let ... { // Retrieve values from dictionary

        // Update complication
        let complicationServer = CLKComplicationServer.sharedInstance()
        guard let activeComplications = complicationServer.activeComplications else { // watchOS 2.2
            return
        }

        for complication in activeComplications {
            complicationServer.reloadTimelineForComplication(complication)
        }
    }
}

Apple 工程师通常建议设置一个数据管理器来保存数据。在您的并发症 Controller 中,您将从数据管理器中检索最新信息以用于您的时间线。

GitHub 上有几个现有项目使用这种方法。

如果您仍然更喜欢从 watch 端请求数据:

您希望将 WCSession 代码从并发症 Controller 中移出,放入 watch 扩展中,并将其作为 WKExtension 初始化的一部分激活。

关键是让回复处理程序在收到数据后手动更新并发症。

当您的 session 委托(delegate)的回复处理程序被调用时,您可以使用我之前提供的更新复杂功能代码来重新加载您的复杂功能的时间线。

如果您使用预定的复杂功能更新来触发此操作,那么该特定方法的缺点是您将执行两次更新。第一次更新会发起数据请求,但没有任何新数据可供使用。第二次(手动)更新发生在接收到数据之后,此时新数据将出现在时间线上。

这就是为什么在后台从手机提供数据的方法效果更好,因为它只需要一次更新。

关于swift - 结合 WatchConnectivity 和并发症,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36856436/

相关文章:

arrays - 如何使用路径中文件中的数组序列化 JSON 数组?

ios - 刷新静态 View Controller

ios - 将 Parse.com 1.11.0 添加到 watchOS 2

ios - 如何连接和调试自定义 ClockKit Complications Controller ?

ios - 在 AppDelegate 中调用 func session didReceiveMessage

ios - 将核心数据对象发送到 "read only"Apple Watch App

swift - 照片编辑扩展问题 (iOS 8)

ios - WCSession 发送消息在实际设备上不起作用,但在模拟器上起作用

ios - 保持核心数据与多个 watch 同步

ios - Swift:Map 返回 4 个值作为 2 点之间的距离,为什么?