我正在创建一个 WatchKit 应用程序,想知道如何从 iPhone 向 watch 发送消息/数据?
我知道如何使用“openParentApplication:reply:”和“application:handleWatchKitExtensionRequest:reply:”反过来( watch -> 手机),但是找不到任何关于如何从手机到 watch 进行通信的文档。
简单的设置是 iPhone 应用程序有一个按钮,当按下该按钮时应该更新 Watch 应用程序上的标签。
谁能指出我正确的方向?
最佳答案
首先,您必须为目标启用应用组:
然后你就可以通过NSUserDefaults
开始写入和读取对象了:
// write
let sharedDefaults = NSUserDefaults(suiteName: appGroupName)
sharedDefaults?.setInteger(1, forKey: "myIntKey")
// read
let sharedDefaults = NSUserDefaults(suiteName: appGroupName)
let myIntValue = sharedDefaults?.integerForKey("myIntKey")
请参阅 Apple Watch Programming Guide: Developing for Apple Watch 中的与包含的 iOS 应用程序共享数据一章
关于iOS - WatchKit 如何将消息/数据从 iPhone 应用程序发送到 WatchKit 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29523253/