Is it possible to read or write steps in Watchkit extension? - ios

I have read documents that said : All the data Apple Watch collects about your heart rate is then transferred automatically to the Health app on your iPhone. I have not found any Information about steps and calories. Whether it store in apple watch local storage or in Health App. If anyone knows about it then guide me.

It isn't possible to read or write steps data from a WatchKit extension. Extensions do not have HealthKit access. However, you could hand off to your iPhone app using openParentApplication:reply: and grab the data from the host app directly. It worth noting, however, that HealthKit data isn't necessarily delivered to the phone in real time.

Related

Share AsyncStorage from iOS react native app with Apple Watch

I'm developing the main app with react native and the Apple Watch with Swift.
The main app (react native) uses AsyncStorage to store some values, that then are used as params for a fetch that shows a list.
After running the simulator, I found manifest.json inside
~/Library/Developer/CoreSimulator/Devices/D10E869B-040B-446F-9B8B-754F111442EC/data/Containers/Data/Application/AC837AFE-312B-4861-906D-EC9EEE7D029B/Document
s/RCTAsyncLocalStorage_V1
with the data inside.
I need to access those values in the Apple Watch in Xcode to do the same job as the iOS app. How do I access that data?
Since Apple Watch apps run independently from iOS app since watchOS 2, there’s really no way to directly access the data stored locally on iPhone — you’ll need to maintain some form of communication between your iOS and watchOS apps.
The framework intended for this is called WatchConnectivity. I see two more-or-less suitable solutions for your case:
Using transferFile to transfer your file from an iOS device to an Apple Watch whenever it got updated. The viability of this option highly depends on how big your file is.
Or you can just use updateApplicationContext to pass an already serialized data instead of transferring a raw JSON file — again, highly depends on your specific needs.
Unfortunately, I don’t know how WatchConnectivity is supposed to work with ReactNative. If you want a deeper introduction to this framework — I highly recommend watching this amazing video from WWDC 2015.

How often and how much iPhone share NSUserDefault data to Apple Watch?

Look at this Apple Docs
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.
Is there anyone know how to access iOS app's preferences from Apple Watch and how often it is updated on the Apple Watch.
I did try to search for document, but could not found any.
AFAIK, NSUserDefaults can contain upto much memory. It would be bad if iPhone try to save all this user config in NSUserDefault onto the watch.
They don't. You should use the WatchConnectivity framework. Here is an extended answer: Reading NSUserDefaults in watchOS 2 (I know App Groups doesn't work)

Did Apple change NSUserDefaults sharing from iOS app to watchOS app

I want to ask about how to use NSUserDefaults on the watchOS app.
Is its data different from the iOS app's NSUserDefaults's data?
There are a lot of stackoverflow questions about this topic and all of them have same answers. That said, for example
Watch apps that shared data with their iOS apps using a shared group
container must be redesigned to handle data differently. In watchOS 2,
each process must manage its own copy of any shared data in the local
container directory. For data that is actually shared and updated by
both apps, this requires using the Watch Connectivity framework to
move that data between them.
However, all the quoted text disappeared from the web page referred, see this accepted answer.
Instead, in current Apple Docs. There is
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.
I have 2 questions:
Which one is correct: all of StackOverflow questions' answers I mentioned above, or, the Apple Docs
Which mechanism iOS use to forward NSUserDefaults object to watchOS app? Is it reliable to be relied on for future development and how recent the data is up to date? Can this feature be deprecated in the near future?
Many thanks
To answer your first question, both answers you quoted from StackOverflow and Apple are correct. Apple forwards the iOS app's NSUserDefaults as read-only values, but the watch has it's own NSUserDefaults for its preferences. The main takeaway from the documentation is for watch apps to move away from shared container groups that use NSUserDefaults, (as this was how WatchKit apps were implemented). In watchOS, Apple has added WatchConnectivity which is the standard for sharing data between the iOS and watch apps.
To answer your second question, rely on NSUserDefaults as you would normally to store preferences related to each app separately and use WatchConnectivity for sharing data between apps.

If you are using a Apple Watch, do you first need to open the Health App to sync data and then open your own healthkit app to get the latest data?

The issue is my App doesnt get the latest steps data unless I go inside the Health App first and then goto my App. Does the Apple watch not sync with Apple Health automatically or is their a workaround in Healthkit API which forces a sync?
HealthKit data syncs to your phone from Apple Watch periodically in the background. Additionally, launching the Health app, Activity app, or any app with a live HKHealthStore will also trigger a sync. There is no programmatic way to trigger a sync, though.

Persistent storage on Apple watch

I am looking for a way to store something on the Apple watch app that I can read later on enabling me to identify it uniquely.
I don't see any API at the moment that would let me do this. The closest I have got is to save an image in cache but since its just a cache, its not reliable.
My goal is to uniquely identify an Apple Watch.
Any pointers would be much appreciated.
You can save information to be accessible from both watch and iPhone in NSUserDefaults and App Groups like this:
let defaults = NSUserDefaults(suiteName: "group.com.your-bundle-id.app-group-name")
where "group.com.your-bundle-id.app-group-name" is your app group identifier.
For more details about NSUserDefaults and other ways to share data between Watch and iPhone see
Architecting Your App
for the Apple Watch
User App Groups OR NSUserDefaults to share data between your iOS apps.
Easily share small amounts of data between your iOS app and your
WatchKit extension with App Groups and NSUserDefaults. For access to
other resources, such as a Core Data store, use a shared container
between your iOS app and your WatchKit extension to simplify data
access and provide up to date information.
I think that this would be very useful, not only for identifying the watch, but also to load the UI faster (without synchronizing lots of data via Bluetooth first).
However, there seems to be no solution at the moment to do this.
It also seems to be impossible to get the hardware identifier of the watch.
Maybe this will be possible once normal third-party developers are allowed to write real watch apps (not just extensions of iPhone apps). The WWDC is in June.

Resources