Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
To request and monitor a user's location, we need a location manager. Over the next few videos we're going to use the Core Location framework and its classes to get a location fix. Before we can do any of that though we need to ask the user for permission. In this video, let's get some basic authorization code in place
-
0:00
Let's add a new file to our project under the permissions group.
-
0:04
So, it's a New File, we're going to, this is going to be a Swift File and
-
0:08
we'll call it LocationManager.
-
0:13
In it we're going to create a LocationManager class that will handle
-
0:17
a couple different things.
-
0:19
Requesting permissions to get location updates from the user, starting
-
0:23
the location service and informing us when a location fix has been obtained.
-
0:29
Class LocationManager and we're going to make this class inherit from NSObject.
-
0:36
Essentially we want this to be an objective C class.
-
0:39
Now, location services are part of the core location framework, so
-
0:43
we need to import that so we don't get any compiler errors.
-
0:48
I keep referring to the core location framework, and
-
0:50
it's important to understand the different things it can do.
-
0:54
As always, I've linked to the framework reference in the teacher's notes, so
-
0:57
you can check it out.
-
0:58
The main class we're going to be working with is CLLocationManager.
-
1:03
So let's head over to the documentation to understand the steps to using this class.
-
1:08
Help > Documentation API Reference, and you should type CLLocationManager.
-
1:17
CLLocationManager is the central point for
-
1:20
configuring the delivery of location and heading-related events to your app.
-
1:25
And we can use it to manage any type of location activity,
-
1:28
including the simple initial fix that we want to get.
-
1:31
If we scroll down a bit on this page, It says here, to configure and
-
1:36
use a CLLocationManager object we need to follow these five steps.
-
1:41
So let's start with the first one, which involves requesting permission.
-
1:46
Now we need a manager to work with so that we can use it to ask for permission.
-
1:49
So back in our class, let's add one as a private stored property,
-
1:56
private let manager = CLLocationManager.
-
2:01
Now let's add a simple method to ask for permission.
-
2:05
So say func requestLocationAuthorization and
-
2:12
we'll make this method a throwing one.
-
2:15
CLLocationManager has a class method authorization status that we can use
-
2:20
to check the app's current authorization status for using location services.
-
2:26
The method is going to return an enum value of type CL authorization status.
-
2:31
So for example, we can check the current auth status by saying let
-
2:34
authorizationStatus = CLLocationManager.
-
2:39
Remember this is a class method,
-
2:41
we're calling it on the type itself, authorizationStatus.
-
2:45
So in here,
-
2:46
before we actually request authorization, we're going to check the current status.
-
2:51
So we have three possible situations.
-
2:53
In the first one, the user has restricted or denied us access to their location.
-
2:58
We may have asked them before, and they just went ahead and denied us.
-
3:01
In this case, since this method that we've defined is a throwing one, if we hit this
-
3:06
scenario we'll throw an error to the call site and deal with it there.
-
3:09
So for example, if the user denies it,
-
3:11
in our permissions controller we can spin up an alert that says so.
-
3:16
We'll say if authorizationStatus equal now,
-
3:19
this returns an enum value so we're going to inspect that value and say,
-
3:24
if it is equal to .restricted or we're using the OR operator here or
-
3:29
if it is equal to .denied, then we're going to throw an error.
-
3:35
So inside this if block, we need an error to throw.
-
3:38
So let's create a small type in here.
-
3:41
At the top we'll say enum LocationError, and
-
3:44
this type has to conform to the error protocol.
-
3:47
Let's give it three different values here,
-
3:52
so let's say case unknownError,
-
3:56
case disallowedByUser and unableToFindLocation.
-
4:02
Okay, so back in our if statement, we can now throw an appropriate error.
-
4:06
So we can say throw LocationError.disallowedByUser.
-
4:13
The second situation we can be in is an undetermined one.
-
4:17
This is where we'll end up if we've never asked for permission before.
-
4:21
So here we'll say, else if authorizationStatus
-
4:28
= .notDetermined then if it hasn't been determined before,
-
4:33
we can go ahead and ask our manager to request the appropriate authorization.
-
4:37
So, we'll say manager.requestWhenInUseAuthorization.
-
4:43
We need to be specific about the type of authorization request because
-
4:47
remember that we added to our info.p list the ns location,
-
4:51
when in use usage description key, and that corresponding descriptive string.
-
4:56
Well these two pieces, the descriptive string and
-
4:59
the request type go hand and hand.
-
5:01
If you provide a when in use description but
-
5:04
request an always authorization, the user won't even see a prompt.
-
5:09
The location request will silently fail, and
-
5:11
you won't be able to offer functionality.
-
5:14
So remember, if you're making a when in use authorization request,
-
5:17
you need to use the right key.
-
5:20
Okay, so let's make a final else case here.
-
5:23
We'll hit this else, if we've obtained authorization before and
-
5:27
we're still requesting for it.
-
5:29
So, all we're going to do here is return from the method, without doing anything.
-
5:33
Okay. Permission requested.
-
5:35
Let's go back to the docs and look at step two.
-
5:39
Step two says, create an instance of the CL location manager class and
-
5:44
store a strong reference to it somewhere in your app.
-
5:47
Okay, we've already taken care of that.
-
5:49
Good.
-
5:49
For step three, we need to assign a custom object to the manager's delegate property.
-
5:55
After a location manager initiates location requests,
-
5:59
any updates we get back are delivered through its delegate.
-
6:03
So, we'll create an init method and
-
6:05
set the delegate to self inside this init method.
-
6:08
So we'll do this right after the sort properties.
-
6:12
Now this initializer's not going to accept anything for now and in doing so
-
6:16
we'll customize that in a second but when we write the init method, just this way,
-
6:20
we need to override it because we're inheriting it from the NS object class.
-
6:25
Okay so we don't have any stored properties we need to initialize, so
-
6:28
we can call superinit to initialize the base class and
-
6:32
now in here we can say manager.delegate equal self.
-
6:40
Now the protocol in question is the CLLocationManager delegate protocol.
-
6:45
Before we actually implement any methods, we'll take a small break.
-
6:49
But before we end the video, we need to get rid of this error.
-
6:52
And this error is occurring because we're not conforming to the delegate protocol.
-
6:57
So over here, let's just go ahead and say CLLocationManagerDelegate.
-
7:01
All right, see you in the next video.
You need to sign up for Treehouse in order to download course files.
Sign up