settings bundle not working on watchOS 2

2019-01-26 11:38发布

问题:

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.

回答1:

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.



回答2:

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



回答3:

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.