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

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

In watchOS 1 it works great for me with application groups, but in watchOS 2 I know that we should use WCSession , but I don't know how to use it.

Please help me find a solution.

+6
source share
1 answer

This post should help you.

From this post: first you create and activate WCSession like this:

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

To translate a dictionary:

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

Then on the receiving side you will need to implement session:didReceiveUserInfo: ( Developer Documentation ). Please note, according to Apple’s watchOS2 Transition Guide ,

To start chatting, your Watch app and your iOS app must have an active WCSession object. Typically, each application creates, configures, and activates a session object at startup and stores a link to it in a central location. When you want to send data, you retrieve the session object and call its methods.

+11
source

Source: https://habr.com/ru/post/989318/


All Articles