iOS - WatchKit 如何将消息/数据从 iPhone 应用程序发送到 WatchKit 应用程序?

标签 ios iphone swift communication watchkit

我正在创建一个 WatchKit 应用程序,想知道如何从 iPhone 向 watch 发送消息/数据?

我知道如何使用“openParentApplication:reply:”和“application:handleWatchKitExtensionRequest:reply:”反过来( watch -> 手机),但是找不到任何关于如何从手机到 watch 进行通信的文档。

简单的设置是 iPhone 应用程序有一个按钮,当按下该按钮时应该更新 Watch 应用程序上的标签。

谁能指出我正确的方向?

最佳答案

首先,您必须为目标启用应用组:

enter image description here

然后你就可以通过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/

相关文章:

swift - 无法使用类型为 (Any) 的参数调用类型 float 的初始值设定项

ios - 有没有办法在 Swift 中从 HealthKit 中提取用户的全名?

ios - 自动将 dSYM 上传到 Crittercism 时构建失败

ios - 如何使用 AFNetworking 对 api 调用进行单元测试

iphone - 显示连续模态视图的正确方法

xcode - 如何在命令行工具中使用 'SQLite'(github 上的包装器)?

ios - 如何在没有对象映射的情况下创建 Restkit .20 响应描述符?

iphone - Flurry Analytics 与移动平台上的 Google Analytics

iphone - 如何将通用 iOS 应用程序转换为独立的 iPhone 应用程序?

ios - 带有关闭按钮的代币