gpt4 book ai didi

watchkit - Apple Watch 复杂功能无法可靠更新

转载 作者:行者123 更新时间:2023-12-04 16:03:59 26 4
gpt4 key购买 nike

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

我使用 WatchConnectivity创建 WCSession 的框架将数据从手机发送到 watch 。

我的数据存储在字典中,并使用 WCSession 发送到 watch 的transferCurrentComplicationUserInfo方法。 (这种方法每天可以使用 50 次,我知道这一点 - 这不是问题。)
transferCurrentComplicationUserInfo方法似乎在我第一次尝试发送数据时起作用。

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

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

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

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

代码:

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

//更新//

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

这是事件的顺序:
  • sendComplication从 iPhone 应用程序调用
  • ExtensionDelegate在 Watch 应用上初始化,设置 WCSession
  • 并发症已更新(太早 - 这是在 WCSession 接收新数据之前)
  • WCSession didReceiveUserInfo调用委托(delegate)方法,解析数据,更新数据源(来不及)
  • 并发症被告知重新加载,但没有任何 react (可能的预算问题?)
  • 最佳答案

    尝试以下操作:

    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

    关于watchkit - Apple Watch 复杂功能无法可靠更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54750119/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com