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

前端之家收集整理的这篇文章主要介绍了如何在SWIFT中将数据从Iphone发送到OS2中的Watchkit前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在watchOS 2中从iPhone发送一个字典到Watchkit.

在watchOS 1中,它适用于appgroups,但在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.

原文链接:https://www.f2er.com/swift/318632.html

猜你在找的Swift相关文章