Apple Watch并发症未得到可靠更新

问题描述 投票:4回答:3

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

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

我的数据存储在字典中,并使用WCSessiontransferCurrentComplicationUserInfo方法发送到手表。 (这种方法可以每天使用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. 从iPhone应用程序调用sendComplication
  2. ExtensionDelegate在Watch应用程序上初始化,设置了WCSession
  3. 复杂化更新(太早 - 这是在WCSession收到新数据之前)
  4. 调用WCSession didReceiveUserInfo委托方法,解析数据,更新数据源(太晚)
  5. 告诉复杂化重新加载,但没有任何反应(可能的预算编制问题?)
watchkit apple-watch watchconnectivity apple-watch-complication clockkit
3个回答
0
投票

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

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

因此,我建议您实施(如果您还没有这样做)WCSessionDelegate函数session(_:activationDidCompleteWith:error:)(请参阅here),并仅在激活会话后传输并发症数据。


0
投票

请尝试以下方法:

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的一个很好的例子,可以帮助你更多:source


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.