settings bundle not working on watchOS 2
Asked Answered
E

3

8

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.

Expressly answered 17/9, 2015 at 19:39 Comment(2)
Can you please post an example about how you are trying to read settings bundle?Abele
I have tried this on a Watch as well and I can confirm that the problem persists! (Groups enabled, with proper profile and settings)Hock
M
8

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):

targets app group

My settings bundle:

enter image description here enter image description here

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:

enter image description here enter image description here

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.

Mistassini answered 22/9, 2015 at 3:7 Comment(12)
The first block of quoted text is exactly what I'm talking about. "Preferences in watchOS 2 are forwarded from iOS to Apple Watch". Why would they write that if it isn't a flat out lie?Expressly
also check out my revised question, I put a link to a thread where an Apple employee says it is possible.Expressly
This is interesting and I'm trying to work through it with you :) You're referring to this quote, yes? "There is one exception, though, and that is the settings bundle. That will be synced from iPhone to Watch automatically." Seems like the settings bundle are supposed to be pushed to Apple Watch still.Mistassini
Correct. And in one of the other threads I've linked, people are saying it is working for them.Expressly
@Expressly see revised answer.Mistassini
Thanks for the revised answer. The only thing you are doing different than me is including App Groups in the watch app target also. I will test this out and see if it works for me.Expressly
It does not work for me in Simulator. I'm trying a lot of things to figure out why... If you can get that working, please let me know!Mistassini
It worked! The solution was indeed to add the App Groups to all 3 targets. In watchOS 1, you didn't need to add it to the watch app target, and there is no mention about this new requirement in the docs. It only works on the real device, it does not work in the simulator. I have an open bug report at Apple, I will mention this discovery to them. Thanks! Go ahead and make a note about this in your answer and I will accept the answer.Expressly
Revised answer. The docs definitely did not say to add the Watch app as a target. Glad you opened a bug! I opened a bug regarding simulators not talking to each other as expected.Mistassini
There is a bug in the bug report page and it is stuck infinitely loading my ticket.. lolExpressly
Its working, however NSUserDefaultsDidChangeNotification is not arrived when users changes settings. As a result, what if the settings are related with complication, there is no way to update in realtime. I prefer to use in-app settings and WCSession for user experience.Obtain
There is an another restriction. You should not persist NSUserDefault instance. You must re-initiate NSUserDefault when it is required. Existing instance never get synchronized.Obtain
A
0

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

Ahumada answered 21/9, 2015 at 20:31 Comment(2)
Your answer does not apply to this scenario. Read the threads I've referenced in the question.Expressly
Wait, what? My answer was posted when this question had much less information.Ahumada
H
-1

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://mcmap.net/q/472264/-nsuserdefaults-not-working-on-xcode-beta-with-watch-os2 seems to make the whole process a lot more convenient.

Hock answered 23/9, 2015 at 23:39 Comment(1)
Please stop posting answers suggesting to use WatchConnectivity. The point of this thread is to get this process working using shared app groups, without any WatchConnectivity code. Yes, it is possible.Expressly

© 2022 - 2024 — McMap. All rights reserved.