This was NOT a problem on watchOS 1, but now on watchOS 2 I cannot read the values on the watch extension.
According to Apple docs, it is possible.
According to some people on this thread, it is possible.
According to an Apple employee on this thread, it is possible.
I'm setting everything up correctly as far as I can tell:
I enabled App Groups on both iOS app and watch extension with the
same identifier.
I added Settings-Watch.bundle to the iOS app
and added the ApplicationGroupContainerIdentifier with the same
identifier to the plist
When I initialize an NSUserDefaults object with the identifier as the suite name, I cannot read values on the watch extension. I can read them on the iOS app. This happens in simulator and real device.
Please DON'T post an answer about how to do this with Watch Connectivity. It is possible to do this with just Shared App Groups on watchOS 2 without Watch Connectivity, people are able to do it, and here it is straight out of the docs:
In watchOS 2, your WatchKit extension may read the values of preferences, but you cannot write new values. Preferences in watchOS 2 are forwarded from iOS to Apple Watch, but any modifications you make are not sent back to iOS.
I've edited my answer. Previously, it spoke about the inability to use App Groups to sync data in watchOS 2, but your specific question is regarding the Settings Bundle, which still syncs from iOS to Apple Watch in watchOS 2.
I am unable to get this to work in Xcode 7.1 / 7.2 in Simulator, but it does work on a real device. From the docs:
Preferences in watchOS 2 are forwarded from iOS to Apple Watch, but any modifications you make are not sent back to iOS. In watchOS 1, WatchKit extensions have direct access to the defaults database and may read and write values.
All 3 targets should have the same App Group configured (the Watch App target here seems to be the missing component in OPs question):
My settings bundle:
Some simple interface code in InterfaceController.swift:
#IBOutlet var label: WKInterfaceLabel!
#IBAction func buttonAction() {
let sharedDefaults = NSUserDefaults.init(suiteName: "group.testSettings")
let name_preference = String(sharedDefaults?.objectForKey("name_preference"))
self.label.setText(name_preference)
}
and the final outcome:
So, it does work as expected, only not in Simulator. It seems that there is some isolation occurring between the 2 devices in Simulator, and it's a little frustrating trying to put my finger on exactly what's happening there.
This is because the apps now run native on the Apple Watch, which means they are not able to get settings from the iPhone, because the settings are not stored on the same device anymore. More info here: Unable to get values from settings bundle in watchOS 2
The new process of handling using `WCSessions be see in the sample Apple app - Lister in the below mentioned file.
https://developer.apple.com/library/ios/samplecode/Lister/Listings/Objective_C_ListerKit__WatchOS__AAPLConnectivityListsController_m.html#//apple_ref/doc/uid/TP40014701-Objective_C_ListerKit__WatchOS__AAPLConnectivityListsController_m-DontLinkElementID_57
Also, the answer mentioned in https://stackoverflow.com/a/32628105/1640786 seems to make the whole process a lot more convenient.
Related
I am making an independent Apple Watch app (but with a companion iPhone app which is not necessary for the watch app to function.)
The app uses WatchConnectivity to sync data between devices if iPhone companion app is installed. I am able to call session.updateApplicationContext() on the iOS app and receive session(didReceiveApplicationContext) on the Watch app. But going the other way is not possible. I always get an NSError code of 7018 which means the iOS companion app is not installed.
I have made sure both watchOS and iOS app are installed in simulator (and tested on my actual devices, iPhone 11 & Watch series 5.) But getting the same failed results from watchOS -> iOS.
I noticed there is a new WCSession instance variable available for watchOS 6.0+ isCompanionAppInstalled. When I read this variable after my WCSession is activated on my watch app, it always return false.
Is there anything else I need to look out for? I am thinking maybe there is something I did wrong in all the different info.plist. But I have checked them multiple times.
my info.plist files:
In my watch app:
WKWatchKitApp is YES
WKCompanionAppBundleIdentifier is com.abc.myapp
In my watch extension:
App can run independently of companion iPhone app is YES
NSExtension
WKAppBundleIdentifier is com.abc.myapp.watchkitapp
NSExtensionPointIdentifier is com.apple.watchkit
iOS app bundle identifier is com.abc.myapp
watchOS app is com.abc.myapp.watchkitapp
watchOS app extension is com.abc.myapp.watchkitapp.watchkitextension
Is there anything else I might be overlooking?
Thank you very much for any help/insight you can offer into this.
Ok. I found a way to get rid of the error and have the watch connectivity work from watch to iPhone.
Originally I wanted the watch app to be independent so in the watch extension target in Xcode, the check box "Supports Running Without iOS App Installation" is checked. But as soon as I uncheck this box, WCSession.isCompanionAppInstalled returns true and error goes away. Syncing from watch to iPhone starts working.
So hopefully this is not intended behavior and will soon be fixed by Apple. (I filed a bug report.) But for now, I am just going to leave the independent watchOS app unchecked because I do want WatchConnectivity in case iPhone app is installed by user.
Thank you for reading and good luck with your programming.
If you have "App is only available as a standalone watchOS app" in Info.plist, delete it even if it is false. Leave only "App can run independently of companion iPhone app". Also don't forget to set "WKCompanionAppBundleIdentifier" for Watch App (not extension). At least it works on real device for me with these parameters.
This seems to be a bug. Sometimes when I start my WatchOS app, it gets WCSession.isCompanionAppInstalled = false all the time, even though the companion app on the iPhone is running, and other apps are able to communicate with the phone.
This seems to go away when I force-restart the app on the watch (press side button, then swipe the app to the left and press the big red button, then start the app again). After the restart, everything seems to work fine.
This has happened only on Testflight builds so far. If it happens in production builds, I'm going to file a bug.
My iOS app will not launch in the Simulator if I uncheck "Supports Running Without iOS App Installation".
I have an iOS app where I have some settings in the iOS app related to the watch. I only want to show them if the user has an Apple Watch that's compatible with my app.
In WatchConnectivity I can query WCSession's isPaired property to see if the user has a watch but I can't figure out how to determine the watchOS version (it needs to be >5.0 to use my app).
Is there a way to determine the watchOS version from the iOS app?
If the settings only apply if the user has your watch app installed, you probably just want to check that directly using WCSessions isWatchAppInstalled property.
Things get much trickier if you want to get the paired watch's OS version regardless of whether your app is installed. If the user runs the watch app, you could just grab the version on the watch side and send it over (with updateApplicationContext(_:)). Otherwise, there's no way that I know of to get the watchOS version -- and that kind of makes sense. From a platform security perspective, if the user hasn't installed your app then the details of their watch are kind of none of your business.
Here are two possible solutions. Number 1 checks for the OS which if I understand your question you already know, but then 2 checks for the OS version which is WatchOS 5.0 and later. This is what I have done so I hope it helps you.
1.
#if os(watchOS)
...your code
#endif
if #available(watchOS 5.0, *) {
...your code
}
I believe I have everything set up right, and everything works for CloudKit in the iOS Simulator, but I can't get CloudKit to work in the Watch Simulator: "Couldn't send a valid signature"
Does anyone know why this would be?
I know CloudKit works in watchOS 3 per Apple "iCloud interactions.
Starting with watchOS 3, the WatchKit extension can communicate
directly with CloudKit and other iCloud technologies."
I already did Settings > iCloud and then log in using your Apple ID
on the iOS simulator per enter link description here
In the Watch Extension, I'm calling CloudKit using CKContainer(identifier:"iCloud.com.xx.xx") per enter link description here
I double-checked Apple's doc to make sure I didn't miss something:
enter link description here
I'm accessing the same CloudKit container on the Watch that I am on
iOS, so nothing is wrong with accessibility to the data in CloudKit
The CloudKit capability with the container identifier is set
correctly in the Watch Target
According to Apple:
CloudKit usage is blocked on watchOS Simulators. Running any test will
throw a “Not Authenticated” error even though you are signed in via
the paired iOS Simulator. Workaround: Use CloudKit on paired devices
with watchOS 3 and iOS 10.
This is from the watchOS 3 release notes, but does not seem to be fixed yet. Just test on a real watch and everything will work fine.
If I'm not wrong, the App Watch can't take locations itself and needs to request it to the iPhone, so the current location is got in the WatchKit extension at iOS side... right?
I think this is a quite common thing WatchKit developers would do, but I don't find a clear tutorial/example of it, could somebody tell me one or post me some code? In addition, do you know if the way to deal with locations changes in watchOS 2?
EDIT: I've found that in some posts it is said that CoreLocation can be accessed from the WatchKit extension, and in others I've read that only the iOS app can... which is the correct?
You can access CoreLocation and all its methods directly from WatchKit itself. There's no need to employ any sort of additional logic, such as opening the parent app.
In watchOS 1, your WatchKit app is technically running on the iPhone, and you can therefore use (almost) any frameworks available to you on the iPhone (such as CoreLocation).
In watchOS 2, the WatchKit app is running on the Watch itself. I have done some research and I've not found anything that indicates you will have to make any changes to your usage of CoreLocation, and I will therefore assume the frameworks automatically handle the communication between the iPhone and the Watch.
I'll update this answer if I find anything indicating that you will have to employ some other logic to make this work in watchOS 2.
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.