Accessing User Defaults on Apple Watch app Extension with watchOS 5 - swift

I am building a workout app for Apple Watch with watchOS 5.
I am storing workout settings in UserDefaults plist. When the watch extension is launched I try to read the UserDefaults and have the workout settings restored for the user.
According to Apple Documentation
Additionally, iOS automatically forwards a read-only copy of your iOS
app’s preferences to Apple Watch. Your WatchKit extension can read
those preferences using an NSUserDefaults object, but it cannot make
changes directly to the defaults database.
If your Watch app needs to change the values stored in the defaults
database, use the Watch Connectivity framework to send the values back
to your iOS app, and save the values there.
Since UserDefaults is just read only for watch Extension, If user changes any setting on the watch, I send that settings to iPhone with WatchConnectivity and store it into UserDefaults on iPhone. So the next time user launches watch Extension he should get updated userDefaults.
Now the problem is watch App also has a complication, If an Apple Watch App has complication it stays in the background in order to launch the app quickly. So when will the iOS app sync the read only copy of userDefaults?
What can be the best practice to follow if a user wants to restore its settings every time the watch app extension is launched?

Related

How can I make an Apple WatchOS 6 independent app auto-launch on watch startup

On the new watchOS 6 I am building a watch app that is independent and I want it to auto-launch when the watch turns on. Are there specific developer permissions I need to allow this OR can I make the app a designated type (e.g. workout app, health app) OR any other way anyone knows to do this.
Right now I have the app running but the user has to tap a complication to turn the app on which they only remember to do sometimes.
Thanks
This is not possible, the user has to launch the app themselves.

Setting up HealthKit on Independent Apple Watch app

I am creating an independent Apple Watch app that is not linked to an iPhone app. How can I request access to HealthKit data directly from the Apple Watch without requesting the HealthKit data via an iPhone app.
Apple recently announced that developers could now create independent Apple Watch apps in WWDC 2019, but I'm not finding documentation on how to use HealthKit on independent apps. I started following the instructions on this page here
https://developer.apple.com/documentation/healthkit/setting_up_healthkit
but the problem that I'm running into is that I can't figure out how to set the necessary custom messages for NSHealthShareUsageDescription and NSHealthUpdateUsageDescription. Apples documentation says to do this through the Info.plist, but I can't find these options anywhere for the just the Apple Watch.
Can anyone show how to use HealthKit for an independent Apple Watch app?
In Xcode's project navigator on the left, your WatchKit extension group contains the Info.plist where you'll add your NSHealthShareUsageDescription and NSHealthUpdateUsageDescription messages. Add the HealthKit setup and authorization code from the article you linked in say, your extension delegate, and you're good to go.

Communicate data between WatchOS & Today Extension widget

Standard set up for Watch OS > 2.
WCSessionDelegate used to coordinte data between main application and Watch.
An App Group "group.***********.TodayExtensionWidget" used to coordinate data between main application and its Today Extension widget via UserDefaults(suiteName: "group.***********.TodayExtensionWidget")
When I make a change from the watch it communicates that change with the main application. Then the main application (once launched) communicates this on to the Today Extension.
What I would like to do is communicate the change in the watch app to the Today extension without needing to launch the main app first.
Is there a best practice to communicate between a watch app and a Today Extension widget?
At the moment there is no way to achieve this using any built-in frameworks. Since the introduction of watchOS2, WatchKit apps are considered to be independent apps and not just an extension of the iOS app, hence you cannot use AppGroups to share data between the two and especially cannot use it to share data between the WatchKit app and an iOS extension.
As you already experienced, WatchConnectivity cannot be used in a Today extension, so that is out of the picture as well.
Your only option is a suboptimal one, since you will need to upload your data to a server from your watchOS app and download it in your iOS Today extension. This of course generates unnecessary data usage, but with the current frameworks provided by Apple you cannot share the data offline.
However, if you wait until watchOS4 is released (or give the beta version a try), you might be able to leverage the CoreBluetooth framework, which is becoming available for WatchKit in watchOS4 to communicate between a WatchKit app and an iOS extension offline, using BLE. I am not 100% sure if CoreBluetooth can be used in iOS Extensions, but you should give this a try if you can use watchOS4 beta.

Firebase not working on Apple Watch Extension

I have been attempting to make an Apple Watch extension and WatchKit app with my firebase application, but every time I make a connection in the extension code, the watch crashes. By connection I mean adding an observer to my Firebase reference. Is firebase not compatible on Apple Watch extensions? Or is there something in firebase that Apple prevents on watches (something like web sockets) that firebase won't be able to work around?

iPhone app's Core Data, after upgrade to App Store?

I managed to finish upload my first iPhone app to Apple's App Store couple of days ago. My app is based on Core Data so it contains users' data to their devices.
What I want to know is that what happens when I upgrade the app with or without altering Core Data? (altering means add new entity or property, etc)
From my experience, I downloaded my own app from Apple's App Store and saved some data and then I install the same app from my computer using XCode with slight change to interface that has nothing to do with Core Data. When I turned on the app, the data is all there.
Is it safe to upload an upgraded version of iPhone's app to Apple's App Store assuming that as long as we don't touch Core Data, user's data would not be influenced?
If you change Core Data model, it may need to migrate/update its scheme. Apple has the appropriate documentation. The work necessary to perform a migration depends on what you change.