如何在SWIFT的OS2中将数据从Iphone发送到Watchkit [英] How to send data from Iphone to Watchkit in OS2 in SWIFT

查看:72
本文介绍了如何在SWIFT的OS2中将数据从Iphone发送到Watchkit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to send a dictionary from iPhone to Watchkit in watchOS 2.

在watchOS 1中,它对我来说适合应用组,但在watchOS 2中,我知道我们必须使用WCSession,但我不知道如何使用它.

In watchOS 1 it works fine for me with appgroups but in watchOS 2 I know that we have to use WCSession but I don't know how to use it.

请帮助我找到解决方法.

Please help me find the solution.

推荐答案

在那篇文章中:首先,您将像这样创建并激活WCSession:

From that post: First, you'll create and activate a WCSession like so:

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:(

Then, on the receiving end, you'll need to implement session:didReceiveUserInfo: (Developer documentation). Note, according to Apple's "watchOS2 Transition Guide,"

要开始通信,Watch应用程序和iOS应用程序都必须具有活动的WCSession对象.通常,每个应用在启动时都会创建,配置和激活会话对象,并将对它的引用存储在中央位置.当您要发送数据时,可以检索会话对象并调用其方法.

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的OS2中将数据从Iphone发送到Watchkit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆