swift - Xcode Settings-Watch.bundle 两种方式通信
问题描述
我很想帮助一个 Xcode 项目,该项目有一个 iPhone 应用程序和一个手表应用程序,它获取 Settings-Watch.bundle 以在设置页面和手表应用程序之间进行两种通信。目前它只有一种方式工作。我在设置页面上对值所做的任何更改都会立即反映在应用程序中,但它不会以另一种方式恢复。如果我更改手表应用程序中的值,设置页面不受影响。
我创建了一个 Settings-Watch.bundle 并使用了一些我在整个手表应用程序中使用的用户定义的默认值。其中一个默认值被称为customGoalValue
包含一个数字,我希望从 iPhone 的设置页面或手表应用程序中进行设置。因此,在设置包的 root.plist 中,我为文本字段创建了首选项并将默认项目设置为 30,将标识符设置为 customGoalValue。我在手表应用中设置并注册了一次默认值applicationDidFinishLaunching()
在手表应用程序中,我可以通过调用加载默认值没问题
let defaults = UserDefaults.init(suiteName: "group.my-name.my-app-name")
showCustomGoalValue = defaults.integer(forKey: "customGoalValue")
print(showCustomGoalValue)
如果我在 iPhone 的设置页面上更改数字,它会立即在手表应用程序中更改。但是,如果我尝试更改手表应用程序中的值,设置页面不会更改。我通过这样做设置默认值
let newValue = 25
let defaults = UserDefaults.init(suiteName: "group.my-name.my-app-name")
defaults.set(newValue, forKey: "customGoalValue") //Updates value no problem
defaults.synchronize() //Tried with and without this
let showCustomGoalValue = defaults.integer(forKey: "customGoalValue")
print(showCustomGoalValue) //Prints new value no problem
但是我回头看一下 iPhone 设置应用程序,它仍然显示 30,而它应该更改为 25。
沟通只有一种方式吗?我认为我正在使用 Xcode 版本 11.4.1 (11E503a) 和 Swift 5.1
解决方案
推荐阅读
- node.js - SyntaxError:打字稿上的意外保留字
- php - 当重复条目不起作用时显示消息
- c++ - reinterpret_cast - 同一个指针可能有多个整数表示
- d3.js - D3强制布局/避免重叠节点(forcecollide,radius)
- jenkins - 为詹金斯安装建议的插件
- python - Python parser.parse 结合 if 语句
- eclipse - 防止 Eclipse (RAD) 自动生成代码
- r - 使 R 中的一个数据框列未更改为过滤器
- javascript - 我有 2 个数组 - users 和 onlineUsers,如果两个数组中都存在该用户,我将如何使用 Vue.js 循环并显示所有用户并应用类?
- python - 从 Pandas 列中删除不需要的字符串