gpt4 book ai didi

ios - watch app在后台能收到后台信息吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:29:17 26 4
gpt4 key购买 nike

我无法弄清楚如何让 updateApplicationContext 数据在 watch 应用程序前台化之前到达 watch 。它似乎只有在 watch 应用程序处于前台时才有效。

watch 如何在后台接收文件?

这是我一直在努力完成的:

iOS 代码:

func sendDataToWatch() {
if WCSession.isSupported() {
do {
try WCSession.default.updateApplicationContext(["key":value])
} catch {
print("ERROR: \(error)")
}
}
}

观看代码:

func session(_ session: WCSession, didReceiveApplicationContext
applicationContext:[String : Any]) {

//handle data when it arrives

}

我注意到 WatchConnectivity 提供了一个处理函数。这是我应该设置的东西,以便能够在 Watch App 处于后台或什至未启动时处理后台连接吗?

func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {
// Sent when the system needs to launch the application in the background to process tasks. Tasks arrive in a set, so loop through and process each one.
for task in backgroundTasks {
// Use a switch statement to check the task type
switch task {
case let backgroundTask as WKApplicationRefreshBackgroundTask:
// Be sure to complete the background task once you’re done.
backgroundTask.setTaskCompletedWithSnapshot(false)
default:
// make sure to complete unhandled task types
task.setTaskCompletedWithSnapshot(false)
}
}
}

最佳答案

根据 apple 的说法,您可以在 session 可访问时使用 SendMessage 将数据从 iPhone 发送到 Apple Watch。

https://developer.apple.com/documentation/watchconnectivity/wcsession/1615687-sendmessage

Calling this method from your WatchKit extension while it is active and running wakes up the corresponding iOS app in the background and makes it reachable.

您可以使用以下方法将数据从 iPhone 发送到 Apple Watch

swift 2.2

let msg = ["IPrequest":"IsLogin"]

WCSession.defaultSession().sendMessage(msg, replyHandler: { (replyDict) in
print(replyDict)
}, errorHandler: { (error) in
print(error)
})

使用以下方法接收字典

swift 2.2

func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void)
{

dispatch_async(dispatch_get_main_queue()) { () -> Void in

print("Response:\(message)")

}
}

我已经在我的一个项目中实现了上述解决方案。

希望对您有所帮助!

关于ios - watch app在后台能收到后台信息吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49728321/

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