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

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)

Related

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.

Why is ubiquityIdentityToken always nil?

I am developing an iOS app that uses CloudKit. Apple's documentation says that I should use NSFileManager's ubiquityIdentityToken property to detect whether the service is available. But the property is always nil. How can I do to solve it? I am testing my app on an iPhone 5 simulator with iOS 8.2. And I have also logged into an iCloud account. I have also turned on iCloud Drive. (Someone says that turning on iCloud Drive may solve this problem, but this doesn't work for me.)
Do I need to call NSFileManager's method URLForUbiquityContainerIdentifier:? But Apple's documentation says that it is needed for document storage. It doesn't say that it is needed for CloudKit.
The CKContainer accountStatusWithCompletionHandler method is how you should check whether the user is logged into iCloud or not (supported since iOS 8.0). The CloudKit Quick Start shows an example of how to use it. fetchUserRecordIDWithCompletionHandler is another option that might make sense for your app.
In iOS 9.0, you'll also have CKAccountChangeNotification, which will notify your app when the iCloud status on the device changes.

Can I access healthkit data from iWatch extension?

I am making a iWatch app where I need to show data from healthkit. is it possible to access healthkit APIs from iWatch extension?
No. It's mentioned specifically in the HealthKit Framework Reference:
"You cannot access HealthKit from extensions (like the Today view) or from a WatchKit app."
What you can do is call openParentApplication:reply: to talk to the iPhone app itself and retrieve that data. Search around for that method name and you'll find some examples on how to call it and get data back to the Watch from it.
UPDATE: As others have mentioned below, this has changed for watchOS 2. The documentation has not been updated yet, but check out the WWDC 2015 videos for HealthKit and the Watch for some snippets of code you can use with the Xcode 7 beta.
Yes, in watchOS 2 you will be able to.
Yes, it's possible on watchOS 2.
However, it's a bit confusing, because it's still mentioned in the HealthKit Framework Reference for watchOS
You cannot access HealthKit from extensions (like the Today view) or from a WatchKit app.
Do not care about this.
Follow a few steps below.
Follow Setting Up HealthKit in the reference, you are now ready to access
Follow Accessing HealthKit Data, then you will be able to access
Make sure your application is running on watchOS 2.
Please see the reference for more detail.

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

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.

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