0:00
[MUSIC]
Okay, cool.
So let's go ahead and take a look at how do we get
our hobbies over to the My Hobbies view controller once I've signed in?
To do that I actually need to go back to my iPhone app.
So, here is my iPhone folder, and I'm
obviously working on a different project than I showed you in the samples so
that you could follow along with me, but I've reorganized my files into iPhone,
0:37
iPhone Watch, TV, etc sections Well let's get your iPhone app.
And I want to look at HoBshareViewController.
And you'll notice that I did a couple of things in this view controller.
I added this extension called WCSessionDelegate.
This allows me to create a session so that I can establish communication
between the iPhone and the watch, and pump some data over to the watch.
To do all of this, I need to import the watchConnectivity framework.
1:12
Now this works for watchOS Two and
later versions for watch os one there's a different API that you use and
the watch connectivity app is not available.
So you may decide to watch os one app on your watch still
1:44
That will allow us to turn this class view controller and
extend it to conform to the WC session and delegate protocol.
All right, so now I can come down here and show you that once we've signed in,
okay sorry for the scrolling, okay we sign in over on the me view controller, so
once we've signed in GetAccountForUser it says self.currentUser = the returnedUser.
If we go look at self.currentUser it has a property observer on it,
didSet that says if our current user is not nil,
2:21
then let's use a NSKeyedArchiver to turn that User into data.
And then we can set that data into some user default's object.
This time instead of using the NS user defaults, not standard user defaults,
I've created my own user defaults area called shared user defaults.
That's more for supportive though watch OS1 technique, so
I just didn't change that code.
But if you use, for example, custom UserDefaults area like I am here,
make sure you use it everywhere just to keep your code consistent.
3:03
Now I take that user and I turn it into data and
put that data into my UserDefaults and I synchronize to save it.
And now I call this function called self dot send user to watch,
which is a function I implemented so let's go do that.
Now I saved my user into the current user default,
excuse me, into the shared user defaults before I called send user to watch.
So it should be in user defaults now.
So I can go and unpackage that data from UserDefaults.
We're not going to deserialize it yet.
We're not going to actually turn it back into userInstance yet.
I just want to grab that data out of this shared UserDefaults and send it over to
the watch and ask the watch to deserialize it so that it can show it on screen.
So I can come here and say, If wcsession is supported
which only works with an iPhone and your watch.
3:58
Then let watch section equals wcsession.defaultsession.
Is set the extensions delegate to be self, right, this hobby share view controller.
And I put it in the superclass hobby share view controller.
So that any of my children view controllers like the mean view controller,
or the edit view controller, the edit hobbies view controller,
the neighbors view controller, should be able to call, send user to watch as well.
And take advantage of this session.
So now, and also I don't have to write all this session management code and all
the subclasses, I just do it one time in the super class hobby share V controlling.
Now I can activate the session and I can check if the watch is connected and,
my watch app is installed, then, I am going to so this instead of a Do catch.
So I'm going to say try to update the watch sessions application
context, and I'm going to pass along a dictionary.
So I'm going to say update application context, and I'm going to pass
5:15
Okay, so that's how I can send my hobbies over to the phone,
excuse me to the watch once I've logged in.
Now, let's go over to the watch and look at how we catch that data and
turn it back into you know habits that we can put on screen.
So, here is the my habits interface controller.
Notice that The superclass is wk interface controller not a ui view controller.
So it's a little something different there but it kind of works with the same mvc
concepts right, you can think of it as essentially it's a ui view controller
with Different functionality, different available properties on functions.
All right, so some of the things you might be expecting to do.
6:03
With your UI view controller, you can't deal with WK interface controller.
And if we go look at the definition of that class, we can see We have things
like view didAppear or view willDisappear, right?
But it's not view willDisappear, it's just didAppear or willDisappear.
The whole interface is going to appear or disappear.
That's the paradigm.
6:31
And instead of things like view didLoad or Will get,
the interface we're talking about will activate, or the interface did deactivate.
So there are similar things for what we're used to with UI view controller, but
just a little bit different and more limited, at least at this point.
Okay so here is my My Hobbies interface controller that we're looking at, and
here I wrote an extension to that again That inherits from WC Session Delegate.
And I can implement this function now that's part of that protocol session.
Did receive application context, and so
now I can see, I can check the dictionary that my context is getting sent.
So here in another part of the app we're sending over Our current user,
here's where we're sending over my hobbies.
So we go and we look for, look for the data that's in the,
received hobby data that we got out of the application context or
if we're doing it through the user object, then we go and we get the user data out.
Of the application context.
And then we call our unarchiver method.
And this is the same thing that we did in the iPhone app using NSKeyedArchived and
NSKeyedUnarchiver.
So I just read whatever dictionary I'm being passed in my application context
by looking at the keys.
And I have to again make sure that the Key name is spelled and
capitalized the same way as it was, over here in my IOS app when I
was sending the information over inside of that key rate.
8:20
All right, so the spelling and capitalization counts.
And now I can go ahead and And package all that data and
I use NSKeyedUnarchivers to go ahead and unarchive that data
back into an instance of an array of hobbies or an instance of a user.
And now that I have that array of hobbies as Like actual,
a list of actual hobby type instances.
Then I can do things like put them into my table view, my hobbies table view.
I have here some code for the rows in that table.
here I have my IB outlet that 's pointing out
to my hobbies table view I have my hobbies property that
9:06
is an array of hobby types that will drive that table view.
And I have a property observer on there called "didset".
And so I can go and enumerate through each of my hobbies and
then I can say The Hobbies table view row at the corresponding index path,
look at it's Hobbie, row label that I have put into my storyboard and
set it's text as, 'the current Hobbie', which we got, again,
by looking at the index, not an index path but an index.
And we say, set text to Hobbie dot Hobbie name So now I've unpackaged
the hobbies that my iPhone sent to me and I put them into my table view.
Now, they should appear in this list of hobbies
inside of this table view and that will be dynamically generated.
That's what you want to look at to start that kind of
communication with the watch and the app.
And then being able to present data, package and unpackage it on both sides,
and then present it on the screen as you need too.
[MUSIC]