WKWatchConnectivityRefreshBackgroundTask示例 [英] WKWatchConnectivityRefreshBackgroundTask example
问题描述
我想使用WKWatchConnectivityRefreshBackgroundTask将数据从iOS应用传递到watchOS 3应用
I want to pass data from my iOS App to my watchOS 3 app using WKWatchConnectivityRefreshBackgroundTask
如何在watchOS应用中设置代码以处理正在传输的数据?
How do I set up code in my watchOS App to handle the data being transferred?
例如,在过去,我使用此iOS代码从iOS应用发送消息,如果没有连接,则发送上下文:
For example in the past I used this iOS code to send a message from the iOS App and if there was no connection send a context:
func sendTable()
{
let tableInfo: WatchWorkout = PhoneData().buildWatchTableData(Foundation.Date().refDays())
let archivedTable: Data = NSKeyedArchiver.archivedData(withRootObject: tableInfo)
if validSession
{
sendMessage([Keys.UpdateType : PhoneUpdateType.TableInfo.rawValue, Keys.Workout: archivedTable])
}
else
{
do
{
try updateApplicationContext([Keys.UpdateType : PhoneUpdateType.TableInfo.rawValue, Keys.Workout: archivedTable])
}
catch
{
print("Phone Session - error sending info: \(error)")
}
}
}
func sendMessage(_ message: [String : AnyObject], replyHandler: (([String : AnyObject]) -> Void)? = nil, errorHandler: ((NSError) -> Void)? = nil)
{
print("Phone Session - phone sent message")
session!.sendMessage(message,
replyHandler:
nil,
errorHandler:
{
(error) -> Void in
print("Phone Session - Error Message during transfer to Watch: \(error)")
}
)
}
func updateApplicationContext(_ applicationContext: [String : AnyObject]) throws
{
print("Phone Session - phone sent context")
if ((session) != nil)
{
do
{
try session!.updateApplicationContext(applicationContext)
}
catch let error
{
print("Phone Session - OPPS something wrong - context send failed")
throw error
}
}
}
我不确定如何将接收到的数据编码为手表上的后台任务.
I'm not sure how to code the receipt of this data as a background task on the watch.
有人可以提供一些示例代码或发布链接吗?唯一的Apple示例代码不是很有帮助: https://developer.apple.com/library/prerelease/content/samplecode/WatchBackgroundRefresh/Introduction/Intro.html
Can someone provide some example code or post a link? The only Apple example code is not very helpful: https://developer.apple.com/library/prerelease/content/samplecode/WatchBackgroundRefresh/Introduction/Intro.html
谢谢
格雷格
推荐答案
The Quick Switch sample code was updated together with the release of watchOS 3 to include an example of handling the WatchConnectivity background refresh task.
这篇关于WKWatchConnectivityRefreshBackgroundTask示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!