简体   繁体   中英

Setting a value for WatchKit app from iOS app when WatchKit app isn't running

I have already know how to use WCSession's updateApplicationContext method, but it only works when my WatchKit app is running.

I need to transfer currency exchange rates from the iOS app to the watch, to update my watch data and complication.

I have a API to get the exchange rate for USD-CNY and USD-EUR, and want to share that with the watch (similar to how NSUserDefaults persists data), so it could show that "1 USD = 6.3 CNY" in my complication.

So, how can I accomplish that?

For watchOS 2

There is only one WCSession method which would let you update your complication data while the watch app was not active.

Assuming your complication is active, and you have not exceeded the (iOS 10) number of transfers per day , transferCurrentComplicationUserInfo will wake up your watch extension in the background, and immediately transfer the user info to your extension.

Your extension could then persist that data on the watch (for the watch app to use when it becomes active), before updating your complication.

func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {
    if let ... { // Retrieve exchange rate data from dictionary
        // Add code here to update your cache/backing store with exchange rate
        // ...

        // Now update the complication
        let complicationServer = CLKComplicationServer.sharedInstance()
        guard let activeComplications = complicationServer.activeComplications else { // watchOS 2.2
            return
        }

        for complication in activeComplications {
            complicationServer.reloadTimelineForComplication(complication)
        }
    }
}

For watchOS 3

watchOS 3 supports background refresh app tasks. This means it is now possible for your watch app to update itself, its dock snapshot, and its complication in the background.

I've already covered this information in an existing answer , but will sum up the key takeaway here:

Apple recommends that you use each opportunity you are given regardless of whether your app is in the foreground or background to keep your complication, app, and dock snapshot up to date.

This is a radical change from merely updating the complication when the app isn't active, to now updating everything -- complication, app, and dock snapshot -- regardless of whether your watch app is in the foreground or the background.

A note on limits to updating frequently changing data

Regardless of which approach you use, keep in mind that you will be limited in the number of updates you can perform per day or per hour.

As Apple recommends, you should consider scheduling your exchange rate updates around the times when they would be needed. These two sessions cover when (as well as how) to update your watch app:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM