7

我有一个 iPhone 应用程序,它将数据从 iPhone 应用程序直接发送到表盘,以显示为一个复杂功能。

我使用WatchConnectivity框架创建了一个WCSession将数据从手机发送到手表。

WCSession我的数据存储在字典中,并使用'stransferCurrentComplicationUserInfo方法发送到手表。(这种方法每天可以使用 50 次,我知道这一点 - 这不是问题。)

transferCurrentComplicationUserInfo方法似乎在我第一次尝试发送数据时起作用。

我的问题是我的 iPhone 应用程序打算在会话中多次调用此函数,并且它只在第一次可靠地工作。

当我发送第二组数据时,第一组仍然存在于并发症中。通常,当我发送第三组时,会出现第二组。有时第二组永久显示,有时在显示第三组之前只出现一秒钟。

这是不一致的,这就是我遇到的问题。

有什么我设置不正确的吗?

代码:

//iPhone code to send data to Apple Watch:

func sendComplication(complication: Complication) {
        guard let session = session else {
            delegate?.failedToSendComplication(reason: "Could not connect to your Apple Watch.")
            return
        }
        guard let context = convertComplicationToDictionary(complication: complication) else {
            delegate?.failedToSendComplication(reason: "Couldn't cast complication to a dictionary.")
            return
        }
        if session.remainingComplicationUserInfoTransfers > 0 {
            session.transferCurrentComplicationUserInfo(context)
            delegate?.didSendComplication()
        } else {
            delegate?.failedToSendComplication(reason: "Due to hardware limitations, you can only send a certain amount of complications in a day. You have exceeded that limit for today. You can still set complications from the Apple Watch app.")
        }
    }

// WatchKit Extension Delegate to receive and handle data sent from iPhone app

import WatchKit
import WatchConnectivity

class ExtensionDelegate: NSObject, WKExtensionDelegate {

    var session: WCSession?

    override init() {
        super.init()
        self.session = newWatchConnectivitySession()
    }

    func newWatchConnectivitySession() -> WCSession? {
        if WCSession.isSupported() {
            let session = WCSession.default
            session.delegate = self
            session.activate()
            return session
        }
        return nil
    }

    func reloadComplicationTimeline() {
        let server = CLKComplicationServer.sharedInstance()
        guard let activeComplicationFamilies = server.activeComplications else { return }
        for comp in activeComplicationFamilies {
            server.reloadTimeline(for: comp)
        }
    }

}

extension ExtensionDelegate: WCSessionDelegate {

    func sessionReachabilityDidChange(_ session: WCSession) {
        if session.activationState != .activated {
            self.session = newWatchConnectivitySession()
        }
    }

    // Receive info from iPhone app
    func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any] = [:]) {
        // Parse dictionary and update data source
        reloadComplicationTimeline()
    }

    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
        guard let error = error else { return }
        print(error.localizedDescription)
    }
}

// 更新 //

经过进一步检查,我现在看到这些步骤发生了故障。

这是事件的顺序:

  1. sendComplication从 iPhone 应用程序调用
  2. ExtensionDelegate在 Watch 应用上初始化,设置WCSession
  3. 并发症已更新(为时过早 - 这是在WCSession接收新数据之前)
  4. 调用委托方法,WCSession didReceiveUserInfo解析数据,更新数据源(来不及)
  5. 并发症被告知重新加载,但没有任何反应(可能的预算问题?)
4

3 回答 3

1

尝试以下操作:

func reloadComplicationTimeline() {
    #if os(watchOS)
    let server = CLKComplicationServer.sharedInstance()
    if let activeComplicationFamilies = server.activeComplications {
    for comp in activeComplicationFamilies {
        server.reloadTimeline(for: comp)
    }
   #endif
}



func sendComplication(complication: Complication) {
        guard WCSession.default.activationState == .activated else {
            delegate?.failedToSendComplication(reason: "Could not connect to your Apple Watch.")
            return
        }
        guard let context = convertComplicationToDictionary(complication: complication) else {
            delegate?.failedToSendComplication(reason: "Couldn't cast complication to a dictionary.")
            return
        }
        #if os(iOS)
        if WCSession.default.isComplicationEnabled {
            let userInfoTranser = WCSession.default.transferCurrentComplicationUserInfo(context)
            delegate?.didSendComplication()
        } else {
            delegate?.failedToSendComplication(reason: "Due to hardware limitations, you can only send a certain amount of complications in a day. You have exceeded that limit for today. You can still set complications from the Apple Watch app.")
        }
        #endif
    }

这是来自 Apple 的一个很好的例子,可以为您提供更多帮助:来源

于 2019-02-27T15:33:47.003 回答
0

正如您在更新中所描述的那样,您的 iPhone 应用程序session.transferCurrentComplicationUserInfo(context)会在手表设置其WCSession. 但是文档说:

[transferCurrentComplicationUserInfo(_:)] 只能在会话处于活动状态时调用——也就是说,activationState 属性设置为 WCSessionActivationState.activated。为非活动或停用的会话调用此方法是程序员错误。

因此,我建议您实现(如果您还没有这样做的话)该WCSessionDelegate功能session(_:activationDidCompleteWith:error:)(请参阅此处),并且仅在会话被激活后才传输复杂数据。

于 2019-02-25T09:53:19.873 回答
0

重置 iPhone、Apple Watch 和 Mac 解决了这个问题。

于 2019-03-01T21:03:08.120 回答