Eliasz Sawicki

Eliasz Sawicki

iOS developer from Gdansk

Data transfer between Apple Watch and iPhone

- 1 min

Today’s post will cover basic data transfer between your iPhone app and Apple Watch app. Let’s assume that you have already created an Apple Watch extension in your project and you want to transfer some data to your watch. As an example, we will be sending Event object to our watch, so let’s have a look at Event class!

The most important thing here is to implement NSCoding protocol. We will need it, because we won’t be able to send pure Event object to our watch app, however we can easily send NSData. Another important thing is to make our class visible to both iPhone and Apple Watch, so remember about linking this class to both targets.

Ok! We’ve got our object. Next thing we want to do is to establish our connection. In our case, we will get the data after tapping on the button displayed on the watch’s screen. Here is the button action code:

Don’t get mislead! Method openParentApplication will not open an app on your iPhone. It will quietly run it in the background and let you quickly do some actions. Here, we are sending a dictionary to our iPhone app, and we expect to get a dictionary back as well. Let’s see the code on iPhone’s side. This method is placed in our AppDelegate:

We can easily get message that we previouly sent from the watch. Then we create our event, archive it and send it as NSData packed inside the dictionary.

It’s not the only way of transfering the data, so feel encouraged to look for another solutions!

This article is cross-posted with my my company blog

comments powered by Disqus
rss facebook twitter github youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora