Synching user settings on watchOS 2
02 Oct 2015In my last post I mentioned that watchOS 2 doesn’t have access to NSUserSettings
,
which isn’t entirely correct. The watch can have access to user settings, it’s just it can’t directly synchronise
them when they are backed by iCloud.
This means if we change the user settings on the phone we need to push the changes to the watch ourselves.
This isn’t too tricky to do if you follow the instructions on the Apple Developer site
about using WCSession
to set up a sharing session between the phone app and your watch extension.
As I wanted to continue sharing code between the two apps, I’ve had to add a slightly hacky onWatch
property to the
settings class so it:
- Only uses iCloud synchronisation on the phone
- If a setting is changed on the phone, it sets up a
WCSession
and uses it to send aDictionary
containing the changes viatransferUserInfo()
- The watch will receive those changes in
didReceiveUserInfo()
which it can then use to update it’s local settings
There’s a small “code smell” of adding in this onWatch flag just so I can reuse some code in this way, so I may come back and revisit this code later.
According to the docs
transferUserInfo()
queues any transfers up to send to the watch when a connection is available.
The solution seems to work pretty well - in the watch simulator at least! On the watch itself the app does seem to run rather slow, but TBH that seems a problem with most watchOS apps I’ve used. More investigation needed!
Next steps: Start tidying up the UI in the existing app, and see if we can improve the performance on the actual watch.
N.B. All code can be seen on GitHub