首页 > 解决方案 > 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

标签: swiftxcodeapple-watch

解决方案


推荐阅读