设置包不能在watchOS 2上运行 [英] settings bundle not working on watchOS 2

查看:123
本文介绍了设置包不能在watchOS 2上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这在watchOS 1上不是问题,但是现在在watchOS 2上我无法读取手表扩展上的值。



根据



我的设置包:






一些简单INT 中的erface代码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)
}

以及最终结果:






因此,它确实按预期工作,但不能在模拟器中工作。似乎模拟器中的两个设备之间存在一些隔离,并且试图准确地指出那里正在发生的事情,这有点令人沮丧。


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.

这篇关于设置包不能在watchOS 2上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆