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

How to send data from Iphone to Watchkit in OS2 in SWIFT

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

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

请帮我找到解决办法。

这个 blog post 应该能帮到你。

由此 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). Note, according to Apple's "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.