What's up guys I'm Jacob Blitzer with slopes dot com and in the last the last lesson we finished up
getting our basic designs done.
So in this lesson what we're going to do is we're going to get our begin run b c done.
So we're going to get this map view showing up and also showing our location.
So a lot of a cool.
And then we'll get the start button wired up to go to the the on run Visi and we will also get all the
IAB outlets on the begin run Visi hooked up and a few other things will get our cell location delegate
and all that stuff wired in and work in.
So this will be good.
And before we start though we need to open up our terminal and make sure you guys are at least committing
your changes and keeping track of it with get.
I'm going to make another branch so you guys can go exactly to this branch when you're following along
and get exactly the work up to this lessens point.
So I'm going to do a get check out dash B and this is going to be less than four.
All right.
Great.
And let's get going.
So what do we have to do we have to start hooking up IAB outlets for the run Visi before we can do anything
else.
So how do we do that.
You know we could manually type out the Iby outlets and things but I prefer just doing the will split
view here and here is our begin view lets get this window out of the way.
If I remember how there we go just click this show document outlined down here below your storyboard
and if you click this shella assistant editor if the run VC was selected it should pop up automatically
if not no big deal just click up here go to manual go into your project and you can find the view controller
you want and we want begin run Visi and let's delete this did receive memory Warren in.
We don't need that.
And also this comma the comments are cool.
So first things first let's hook up all of our our outlets.
So if you hold control like let's select our map do here you hold control and drag over here we can
go ahead and call this our map view and it can.
Or hit enter and that's connected.
And now you can click on our.
We actually don't need an IP outlet for the start.
Ron and I will discuss that in a second.
So let's just go ahead and hook up our central location as well.
And we don't need an I.V. for this one either.
Let's just hold control click and drag and underneath the view to load let go and make sure you change
the connection type to action and we will call this location center button pressed.
It's always nice to be as descriptive as you can and this will be the function that's called when you
push that button and that'll be centering on our location in a little bit.
I don't think it'll happen this lesson but that is what it will be for.
And like I said before we're getting yelled at because I hooked up a map view and we don't have map
kit imported So let's go ahead and do import map kit and that should take care of that.
It wants us to
yeah now are good.
All right.
And so I said we don't need an action for this or an IAB outlet because we can actually just click this
and do a controlled drag to this Visi to the current run Visi and let go and just click on Show.
And now that button will go to this Visi And that's exactly what we want.
So we don't need to add any code for that.
So that's pretty nice.
So something I want you guys to think about.
We are using C-L location and like Map kit stuff and both these VCs and we have to you know get user
permission and all that good stuff.
So what I'm going to do is I'm actually going to show you we're going to make another VC that both these
VCs can inherit from.
So we only have to write code once like the authorization code and then we'll also use the
excuse me I forgot the name so I'm going to look we can also use the C-L location manager for both views
and only have one we just have to make sure we assign the delegate to each view when we go to it.
And then we can use the same manager.
And it makes things a lot easier and we don't have to rewrite code.
But before we go any further you do have to ask the user permission to find their location.
So you have to open up the info list over here.
Inside your project and on the bottom here just hit this last arrow and if you start typing privacy
you'll see all these things.
And if you go and find location and then this one location when in use that's the one we want.
And now if you do a location always Apple usually will reject apps for that so you get to keep that
in mind unless you have a really good reason for using it.
But in-use is fine because we're going to be using the app when we're running.
You might run into problems with it going in the background but you can always address those letter.
But for everything we're doing right now when in use is fine and right here in the string here this
is the message that shows up.
So we're going to since we called our app treads I'm going to say
treads needs your location for tracking for tracking your run.
And I think that's good and that will show up in the message when the users prompt for giving their
location.
And so if we go back to our beginning run we see when to actually go ahead and get rid of the assistant.
View there because we have our ibl hooked up we have everything that we need at the moment.
So yeah let's see what should we do right now.
I think what I'm going to do is make that extra VC I said where were going to set up all of our the
C-L location manager stuff.
So on the controller folder go ahead and right click that and hit new file and this is going to be Iowa
cocoa touch class and we are going to call this location Visi and make sure it's you I view controller
that's important.
And that looks good.
Create and let's delete all this stuff here.
And we don't need these comments.
All right so what we need in our location Visi we're going to need to import our map kit again
right map kit and we're also going to have to inherit from our M-K map view delegates right.
So when we do that that will basically allow us to set the C-L location manager and we can also when
we inherit when our other VCs inherit from inherit from this they won't have to inherit from either
of these two classes so it's really nice.
So the first things first we have to create our RCL location manager so let's call it var manager and
it's going to be of type C-L location manager.
And this is optional because we don't actually know if this is assigned so we don't want to just force
on wrap things.
And then in the view load We're going to say Manager equals equals.
See location manager and then opening and closing parentheses.
And that sets our manager to a new instance of a location manager and a couple of things we're going
to need to do is we need to know the the accuracy and the activity type.
And these are just you can find these in Apple documents if you need to look them up.
So we're going to go ahead and say Manager dot desired accuracy and this is going to hit hit Eagle and
you have to do a case see look I'm sorry Casey location accuracy best.
So we want it the best like we're not driving a car we're just you know we're running we're moving not
as you know not as far as like a car GPS would be so we want as accurate as possible because we're only
moving you know like I don't know four or five miles an hour not very fast.
So once we do that we also want to set the activity time.
And we can do that by manager and then activity type equals and then you just do a dot and it will find
all the options for us so if you if it was an auto a motive or if it was an automotive app we could
do automotive navigation or whatever you want but we want to set this to fitness and that'll make it
a little more accurate for us.
And then one function that we need that we're going to share between our begin run Visi and our current
run Visi is we have to check the location off status.
So we're just going to call this function check location status.
OK.
And then basically we're just going to check to see what the status is so if C-L location manager dot
authorization status does not equal dot authorize when in use.
So that means the user has an authorized use for us to know their location.
Then we need to call the manager that we established and say request when in use authorization.
OK.
So when that is done.
So basically if it's if the status isn't authorized the phone or the user will be prompted for authorization
and that's all we need in this location visi.
And I'm just going to drag this location b.s. to the top because it's going to be shared.
And now if we go back to our begin run Visi always do to have all that information from this Visi is
let's get rid of this you view controller we don't inherit it anymore because location Visi does and
we just have to inherit from location visi.
It's pretty cool.
And so once we do that the your view is going to load and we need to check authorization status right
away.
So let's go ahead and say check location off status and that since we're inheriting we have this full
capability to access that function which is pretty cool.
And then we also need to now set our map view delegate and that is that delegate.
And we want to just set that to sell.
We're only using the map view on this one.
So that's pretty easy.
And I think that's good for now.
But now we also have to make sure the manager doesn't get a set and we don't want that view did load
because once this views loaded if we go to the current run we see and come back there's a good chance
is still loaded.
So you did load will not be called and we have to make sure the delegate gets set in both current run
Visi and begin run visi.
So we have to set that delegate.
And our view will appear so if we just side view will appear perfect and we can just go ahead and call
our manager which we have access to because we inherited from location VC and just to delegate equal
self.
And now the delegate for C-L location is set to this visi.
So that's pretty cool.
And now we can also say a manager dot start monitoring location start monitoring now start up date and
location.
All right.
Cool.
Oh you know what this needs to be.
You will appear
want us to see.
I mean we can force
OK.
So we unwrapped it as a location manager delegate which is fine.
This all worked just great.
And in our we also want to do of you did disappear.
You did disappear and we want to make sure we stop updating location because we're going to be reassigning
this delegate when we go to the other VCs So we want to just say stop monitoring location.
OK.
Cool.
All right so with that being done we should be able to run this and just at least see the map so let's
go ahead and run this
whoas superzoom then I'm just going to like 50 percent that an awesome so our check authorization works
we're going to click allow And now we'll see if a dot shows up should show up somewhere.
Let me see if our Loek go to debug location and let's go to the Apple OK.
Well our map showing and we probably just have to add a few more things like we have to set.
Show user location and also enable tracking.
But we're getting there things are and our start run button should take us all the VC suite.
So our other VC works but obviously we can't get out of it.
So let's go ahead and stop this and let's finish making things work.
So we need to inherit or we need to extend this class and we also have to have it inherit from C-L location
manager or delegate.
OK so if we do an extension down here extension and then this is called the begin run Visi and it needs
to inherit from C-L location manager.
DELEGATE.
And the reason we're doing this is because we need location manager and like we need to show the user
location on the map view only on this PC because the other VC doesn't even have a map view so it doesn't
matter.
And now since we inherited the C-L location manager We're going to be able to delete this so let's delete
that.
We don't have to cast it as anything.
And it just it knows what it's supposed to be now.
So that's cool.
So let's go ahead and do a function.
Location Manager and we need did change authorization status.
So where would that change
it did fail.
There's got to be a better way to find this.
One second guys did change
did change authorization there go so go ahead and when you see location manager did change authorization
hit enter.
And in this function we need to just check to make sure our authorization is still set.
So it is passing in a status.
And let's just go ahead and say if status and if that is equal to authorization women use that's good.
And if that's true we want to check location status again just in case you know something.
We want to just always make sure we're authorized to have access and that will actually re-initialize
a lot of things here.
So if we just check our status again that's fine.
And then we want to make our map view show user location you know we need to set.
That's true.
OK.
And then we also need our map view that user tracking mode equals.
And then you can just do a period dot follows and you can do numerous other things.
But we wanted to follow our DOT and those are the only two things we probably have to do to get our
data showing up so let's go ahead and run this again.
And there we go.
And it even zoomed in on us without setting a radius.
So that's pretty cool.
I think we can call this video done and I think in the next video we should probably work on.
Let's see what should we do.
I think we should move on to the current run and what we'll do is we'll get our slider button all hooked
up.
So if we enter this we see we can at least and the run and get back to the other visi.
So in the next video you guys should try to do this.
We're basically animating the UI image view here and allowing a slight gesture to move it.
And when it hits a certain location it can call an action and it will return.
It will exit or dismiss this VC.
So you guys should try to do that.
It's OK if you can't.
This is a pretty cool feature and it can be kind of complicated to understand at first but before we
end let's just make sure we pull up our terminal and do a get ad period and then a dash and.
And let's see we got our map view and Loek shared location.
And I just hit enter and we are good to go and I will see you guys in the next lesson.