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.
Related
I have an iOS app, and I would like the watchOS app to have synchronized data. Currently, the data is from Firestore, since it's also synced from the web, but I'm open to re-architecting the app.
Fundamentally, what I need is the ability to synchronize user data across all three platforms: watchOS, iOS, and the web.
Currently I have the iOS sending the data to watchOS, but on the Watch if I kill the app I lose all the state until I sync again. Ideally, the state would persist until there is a chance to sync again. So, there needs to be some persistence on watchOS. It seems there's a few options:
AppStorage / UserDefaults seems like it might work, although I've had problems with it not liking arrays and not finding expected values in Info.plist
CoreData seems like the most viable option from what I've seen so far, but a lot of code to synchronize data between watchOS and iOS and especially if I'm also trying to synchronize with Firestore
JSON seems like it's technically an option but a lot of code to serialize/deserialize
Both the watchOS and the iOS apps are SwiftUI, and I'm trying to determine how to keep them on the same page. I have apps on my Watch that seem to do this ok, but I'm having trouble figuring out how to do it myself.
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)
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.
I am working on an app using core data as I thought I could use sync functionality provided by core data for iCloud for syncing data between iPad and iPhone, now it is deprecated.
I came across CloudKit but it is mainly to create and manage data that can be consumed by community of end users but not just one like chat apps. My app is only for single user so that user can sync their data between their apple eco system devices.
I am new to iOS development so it would be helpful to know which technology will meet my requirements.
In short, can you please suggest what should I use
If have to store data on phone for offline use
Then sync it across devices if user have say iPhone, iPad, and iWatch
P.S. Can you please suggest something that doesn't need the user to be signed into iCloud while saving the data.
The CoreData/iCloud integration is deprecated in iOS 10 / macOS Sierra, and there is no replacement (at least so far).
Way forward so far :
Use third party framework such as Ensembles : http://www.ensembles.io
Use CloudKit, which can be used for that. See this for a help : https://nickharris.wordpress.com/2016/02/09/cloudkit-core-data-nsoperations-introduction/
Note that CoreData/iCloud integration will still be working for some time in the future. Apps that are using it will not suddenly stop working.
Apple just released a new container for Core Data that can sync across devices with iCloud.
Get started here: https://developer.apple.com/documentation/coredata/mirroring_a_core_data_store_with_cloudkit/setting_up_core_data_with_cloudkit
Note, this requires iOS 13.
You should take a look at the videos from the latest WWDC.
Here is something about CloudKit Best Practices and whats new in cloud kit
CloudKit really have some nice out of the box solutions and you dont have to bother about security. Also the newest functionality enables you to use notifications for synchronising apps using the same apple id and even beyond, as the user can now opt to share certain data.
Depending how much data you are syncing, NSUbiquitousKeyValueStore.default() is also an option. But only for very less data
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.