Apple Watch 和 iPhone 同步, 表盘开发
2020-05-25 本文已影响0人
幸运者_Lucky
iPhone 端同步
remainingComplicationUserInfoTransfers
//If the complication is on the active watch face, you are given 50 transfers a day. If the complication is not active, this property defaults to 0.
iPhone 每天可以唤醒 Watch 50 次, WCSession.default.remainingComplicationUserInfoTransfers
, 可以直接知道当前剩余次数, 如果使用者在 iPhone 上关闭了复杂功能
, 那直接返回0, 如果剩余次数为零的时候, 就会使用transferUserInfo(_:), 替换transferCurrentComplicationUserInfo(_:), transferUserInfo(_:) 只有当 watch app 进入前台之后才会唤醒, 官方说会在适当的时机唤醒, 肯定是不会立即唤醒.
Watch 端接收
//The background watch connectivity task informs you that your app has been given background time. You must use your [WCSessionDelegate](https://developer.apple.com/documentation/watchconnectivity/wcsessiondelegate) methods to receive this data. Because of the asynchronous nature of these tasks, you must defer calling your tasks’s `Completed()` method until after you have activated your session and received all the pending data. Use the `hasContentPending` method to determine whether you still have any pending data.
Watch 在后台收到 iPhone 推送数据的时候, 会先在 handle
方法中返回一个 WKWatchConnectivityRefreshBackgroundTask
后台任务, 如果这个任务 Completed()
, Watch 将会挂起, 所以在这之前一定要保证所有的同步完成.
表盘数据更新
只说一下 getCurrentTimelineEntry
方法, 这个方法是为表盘提供数据的,
表盘控件有不同模板, 需要提供不同的数据, 这里就不一一介绍可以参阅CLKComplicationTemplate, 主要说一下更新表盘数据, 每次获取新数据, 都需要 reloadTimeline, 这样就会从新走 getCurrentTimelineEntry
回调方法.
let server = CLKComplicationServer.sharedInstance()
for complication in server.activeComplications ?? [] {
server.reloadTimeline(for: complication)
}
这里最大的问题就是每天只能唤醒50次, 超过50次, 就没法立即唤醒 APP, 那么就没法及时更新表盘数据.