swift - 如何在 SWIFT 的 OS2 中将数据从 Iphone 发送到 Watchkit

标签 swift watchkit xcode7 ios9

我想将字典从 iPhone 发送到 watchOS 2 中的 Watchkit。

在 watchOS 1 中,它适用于我的应用程序组,但在 watchOS 2 中,我知道我们必须使用 WCSession 但我不知道如何使用它。

请帮我找到解决方案。

最佳答案

blog post应该能帮到你。

从那个帖子开始:首先,您将像这样创建并激活一个 WCSession:

if (WCSession.isSupported()) {
    let session = WCSession.defaultSession()
    session.delegate = self
    session.activateSession()
}

用于传输字典:

let applicationDict = // Create a dict of application data
let transfer = WCSession.defaultSession().transferUserInfo(applicationDict)

然后,在接收端,您需要实现 session:didReceiveUserInfo: ( Developer documentation )。请注意,根据 Apple 的“watchOS2 Transition Guide”,

To begin communication, both your Watch app and your iOS app must have an active WCSession object. Typically, each app creates, configures, and activates a session object at launch time and stores a reference to it in a central location. When you want to send data, you retrieve the session object and call its methods.

关于swift - 如何在 SWIFT 的 OS2 中将数据从 Iphone 发送到 Watchkit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30907784/

相关文章:

ios 在 View 执行期间禁用/启用旋转

ios - Swift - 框架中的共享核心数据 - 自动生成的类不是公开的?

ios - 将目标添加到 Apple Watch 上 tableView 行上的按钮

objective-c - 我可以使用 wcsession 让父 iOS 应用程序转到前台吗

xcode - 程序 Xcode 7 和 Receigen 中出现意外的 '@'

ios - 在 iOS9 上运行应用程序时 Xcode 7 停留在 "Validating application"

ios - 修改多张照片时只有一个 iOS 权限对话框

ios - 我如何检查我在 Swift 的 prepareForSegue 函数中为 TableViewController 单击了哪个部分?

swift - 是否可以手动传输 pod 文件而不是运行 `pod install` ?

ios - Storyboard中按钮上的文本和模拟器中未显示的按钮?