Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
783 views
in Technique[技术] by (71.8m points)

ios - settings bundle not working on watchOS 2

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...