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

妖精的绣舞 提交于 2019-12-06 01:02:35

问题


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

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.


回答1:


This blog post should help you out.

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()
}

For transferring a dictionary:

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

Then, on the receiving end, you'll need to implement 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.



来源:https://stackoverflow.com/questions/30907784/how-to-send-data-from-iphone-to-watchkit-in-os2-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!