Everyone welcome back.
Joining me here with slopestyle and we are still working on our shortcut app in this Target topic.
All right.
Pretty exciting stuff so far.
We are able to click on our app and get these nifty shortcut quick actions to pop up.
But when we click on them they're not doing what we want them to do are they.
So go ahead and work on that.
So the quick actions they are actually handled in the app delegate file.
Right.
And so we can actually get rid of get rid of most of these.
And the function that we want is called perform action for short cut items so you just start typing
perform action shortcut.
You should get the auto completion right here.
OK.
So let's take a look at this function we are passed into it.
Application the shortcut item which is of type application shortcut item and a completion handler.
All right so we are expected to actually call the completion handler and pass it a boolean true or false.
All right so but just for starters just go ahead and just print out say print and print out the short
cut item.
If you remember what are the keys and the properties of shortcut item we are able to do the types.
Let's print out the type here.
All right we're going to run this and this should print out the item type when we click on it.
OK.
So bring up the simulator here and go home and oh I should tell you that if so what I'm using here I'm
forced touching with my Magic Trackpad nifty thing here you might find it difficult to do this unless
you have a newer either a MacBook Pro or you have this trackpad.
So you might need to run it on your device to be able to to really test this out all right.
But anyways I'm going to cook here on the mountains and I should have pulled it over here sure enough.
Here we go.
Come out Johnny B does short cut that mountains.
All right so this is the bundle Id dropped and then our whole thing is our type but we got our bundle
I.D. and then dot mountains and if we click on the other one let's see here.
That was regular quick force touch.
We have space printed out and forced touch and ocean.
Have it go all right.
So pretty cool.
So we know that's working.
So we're going to need some way of determining which one that we are clicking on and what to do something
where you're in it.
We're going to need an enumerator here.
That would be helpful for any use in the numerator to be able to use a switch statement and just have
different cases and handle them on a case by case basis.
All right so we're going to say enough and we're going to name this short cut type probably don't need
a capital C to shortcut type.
There we go.
And our first cases.
Oh this is going to be of type String we're going to have raw values of string.
OK.
We went to a mountain
and then there should be an equal equal to
mountains.
And so these are going to be exactly need to accidentally match what we have in our infant up here just
at the end of our shortcut.
Item type Ok so Mountains space lower lowercase and ocean.
OK.
So we have mountains and then we have case is space and that equal to space.
And let's we have the ocean ocean to ocean.
All right on the scene is just so that we don't have a bunch of magic strings floating around.
All right.
So I just kind of make it a little bit more safe.
All right so now what we're going to do is we are going to use our shortcut right here and we're going
to extract the type from it.
So we're going to do it we're going to say if that type is equal to short cut item which is passed into
us this is the short cut item of whichever one that we click on.
And what we're going to do is we are going to say we're going to access the type and so this right here
is going to be what we have here.
All right.
And what we're going to do is we're going to divide it up by components split up by the period.
OK.
So it divided up into an array of string components.
So it's going to be calm.
JOHNNY B.
Short cut and mountains and then we're just going to get the last one which is this values mountains
or space or ocean.
And that's how we're going to be able to do a switch statement on our short cut type right here.
The same numerator I guess we're going to say short item type dot and then we're going to say components
right here separated by character set.
And as you can see that it will return a an array of strings can and will we want to divide it by or
separate it by is of course a period and then we're going to grab the last one of it because that's
the one that we want.
Mountain space and ocean.
All right so we're going to grab that if let it.
So we have it safely unwrapped and now we can do a switch statement on that type so we can see a switch
on the type and we can say case short cut type dot mountains that are all value and just for now we're
going to say print these are out this then for the second case we're going to say case short cut type
dot.
Let's see what was on Space space that raw value and we're going to save print.
This is space.
And last week we have our third case which is actually no colon here yet.
Short cut type that ocean that raw value and we're going to see here print.
Love the beach.
And actually it's going to say which must be exhaustive.
So we need a default.
So let's go ahead and make our first Visi the default.
And if you click on here it's going to ask us if we want to add a default clause I say sure and fix
that.
And what we're going to do is we're just going to take this line right here and paste it right here
because we're going to make and we can just delete the Surekha typed on mountains that Rob you will
make the first Visi be the default the case that Google is going to do a little bit of clean up here.
All right let's go ahead and try that again.
Going to run it.
And just to recap while we're coming up here we created an enumerator called shortcut type with three
cases mountain space and ocean.
Each of its raw values corresponds exactly to the value that we entered here and the input up the list
right here and come back here and then we have a switch statement.
Well first we are extracting from our shortcut item when we click we are grabbing out of the full string
including our bundle identifier and just getting that last value.
And then we're doing a switch statement and we will handle the shortcut actions on a case by case basis.
So we couldn't test this and click here and force touch and when it's like the mountains and we are
printing these other mountains that's good and a force for touch again and say space is space.
And lastly love the beach.
All right perfect.
So now we are actually handling each individual quick action.
So just think about what we want to have happen when we click on the shortcut items.
So when we click on this ocean for example we want to go directly to the ocean view.
All right.
But what if we have launched it for the first time and if we launch it for the first time and we open
it.
The only view that has loaded that's been instantiated is this first one right.
And if you check now I mean that scoreboard storyboard we have three controllers.
So if we launched the first time only this one has been instantiated so we can't just say you know Segway
directly over here to this third one.
So what we're going to do is rush to go into on did finish launching with options so once we have finished
launching we're actually just going to instantiate all three and store them in a controller array because
one nice thing about the one we have are all of our views embedded in a navigation controller is that
we can use some of the nice methods that it has and just pop to different view controllers as long as
the view controls are part of the stack and we can add any we can add an array of controllers to a navigation
controller stack.
OK.
So we're going to instantiate our three view controllers and put them inside of an array and then we
are going to add that array of controllers to the route controller and navigation stack.
So out here under our window a variable is going create a variable for that someone to say VCs array.
I'm going to instantiate this right here so it's going to be an array of UI view controller and instantiate
that.
And then here and finish launching with options we're going to need a reference to our storyboards.
When I say let storyboard equal to you I story that's how you story board and its name is main as you
can see right here main storyboard and the bundle we can just put in.
OK there we go.
So now we have a reference for storyboard we can instantiate a view controller from it and say let And
it is going to be mountain VCM is equal to storyboard that instantiate controller.
And this is why we had our identifiers here.
If you remember we added mountains we see here our storyboard ID mountains Visi kit.
And so when you get to delegate and our identifier here
is my own patterns we see that we go and we're going to cast it as a mountains of visi.
All right then we're gonna do that for our other two.
When I say let the space seem to storyboard it instantiate the view controller with identifier.
And this one's identifier is space Visi And we cast it as a space receive controller and clothes a left
pane here and give us a little bit more space.
OK need our exclamation point there.
Right I'm going to say that ocean we see equal storyboard dot instantiated controller with identifier
and the identifier is of course ocean.
And we are casting it as an ocean.
There you go.
So now we have our three controllers instantiated.
And now let's go ahead and add them to our A B C's array.
So I want to say VCs array is equal to.
And then I'm just going to put them in right here from the same mountain VCAT space.
Space Fisi and ocean.
We see that.
So now we have our array of view controllers so we can actually use them down here in our perform action.
So when we click on the our own we tap on the space for an item we want to navigate to the space one.
So what we're going to need to do is we're going to need to get access to our route.
Controller came and how we're going to do that.
And we are going to say let nav Visi and we're going to use the window right here to get to it.
We're going to see that and have the sea equal window dot route view controller and we're going to cast
that has a UI navigation controller.
OK.
And so what we're doing is we're saying we're saying this guy is and let me open up my main storyboard
board is this guy right here.
This is what we are calling Nav. the sea.
OK.
So let VCP do route view controller and then we need to set its array of controllers so we can say now
Visi that set the controllers and we are going to pass in this array of controllers that we just created.
OK.
So let's see here.
This is VCs array animated false.
There you go.
So now our navigation controller has a full stack of controllers that we can navigate to.
All right so now that we have instantiated them added them now we can tell it to pop to any of those
controllers at any point and it will maintain the order that we would expect if we pop to the third
one then we can go back and we'll back through to the second and then to the first.
If we take if we pop to the second one we just pop to the second one and we can continue on to the third
one or go back to the first one.
All right it's going to be pretty cool.
So here where we shortcut to space let's go and remove this print statement and what we're going to
do is we're going to say now have these dot pop to view controller and you can see all these options
you can pop to your controller and that will just go directly to a specific controller.
If you say pop the controller then it removes the top few controller from the stack and goes to the
one below it.
You can pop to the root view controller.
So no matter where you are in the navigation stack if you call this one then you go right back all the
way to the beginner all the way to the beginner route you control.
And so we want to be a controller.
And for the for this one so this is going to the second view controller space visi.
So in this array.
This is the.
So you go 0 then 1 and then 2.
So this is the one item in the array.
So right here we're going to say on the C's array and the one item animated is true.
And then remember at this function perform action for for the item expects us to call the completion
handler and pass in a true or a false.
So here we have we've been successful.
So we're going to see completion Hendler and true that we go.
And then if we click on the ocean one we want to pop to the ocean Vcc and say nav we see that pop to
your controller.
If we control that we want to pop to VCs VCs array.
And this is the second item or the 0 1 tooth item.
And I mean it is true.
Kim and default is the first controller.
And so what we can do here is we can say never VC that pop to route controller since is the default
that we want to get back to the first controller.
We're just going to pop to the route and say animated is true and completion handler is true.
Looks There we go.
All right.
Perfect.
And then if we weren't able to get a short cut item so if this failed then we're going to say completion
handler and false.
All right.
So let's save this and run it and let's see if this works.
Whoops we got a little bit of an error here.
That's what we did wrong.
Oh.
You know what I did we forgot or I forgot to set the controller classes for these other two control
issue.
So with the second one selected here you controller this should be set to space Visi and the third one
should be set to oceanview see an area.
Let's try that again.
All right I'm going to go home.
Force touch.
And let's pick ocean and we should.
We're very nice.
We went directly to the ocean view and let's do that again let's go to space and go to space and you
can see that we can still navigate just like normal.
Let's go to all the way to the third one directly and then try to navigate back and that is all normal
as well.
All right.
Very nice.
So there you have it.
We are officially using quick actions to navigate directly to different view controllers in your app.
So that is pretty cool.
All right.
So there you have it.
Our app is pretty much complete now.
So we're going to do a little bit more.
We're going to see how this works with tab controllers.
All right.
So we're going to do a little bit more show you a little bit more tricks and I'll see you then.