We've got Expo location installed.
So let's start to make use of it to prompt the user and ask them for permission to track their location.
This process is actually gonna be just a little bit complicated.
Someone to very quickly show you a diagram of how we're going to do this any time they track create
screen appears we're going to immediately prompt the user and ask them to track their location when
we make that call.
It is a async function call.
So if the user decides to reject our request then the call is going to throw in air so we can use that
thrown error message to determine whether or not the user granted us permission.
So if the user does not grant us permission we will show an error message to the user.
Otherwise if we do get permission we'll start the tracking.
Now some point time later if the user ever returns back to this track create screen we are always going
to rerun that permissions code.
But if we ever call that permission stuff a second or third or fourth or however many times the operating
system is going to automatically approve or reject us based upon whatever the user entered during that
initial request.
OK.
So that mind let's flip back over to our track create screen and start writing out some code so inside
of my track create screen at the very top I'm going to import a function called What's the name of it
and pull it up really quickly.
All right.
It's a long one.
That's why I had to look it up really quickly I did not recall it off the top my head.
So it's request permissions async.
Notice how it's permissions.
So there is an S there at the very end and we're going to get that from Expo dash location now just
a second ago.
I said that we want to make sure that we attempt to request permissions any time that our track create
appears on the screen of the device.
So that's usually a sign remember anytime we say that we want to run some code any time a component
is displayed.
That's always a sign that we probably want to make use of that use effect hook from react.
Some also go into import use effect from react.
Finally we also said that if a user rejects that request we want to show an error message.
So that would involve somehow rendering our component.
And once again and he's and we talk about rendering our component or updating it we are always talking
about making use of state.
So I'm also going to import used state from react as well.
So now inside of a track rate screen I'm going to first get started by making a little helper function
that's gonna kick off this entire permissions process and then start watching the user's location.
So I'm going to make a helper function called start watching.
This is going to be an async function to make sure you get the async keyword inside there.
Then inside the function I'm going to put a try catch statement together inside the tried case.
We're going to start to request for permissions.
So I'm going to say a wait.
Request permissions async like so so if the user denies this request we're going to automatically fall
into this catch case so he can use that as an opportunity to set some kind of error message and then
display that air to the user let's just try to focus on showing that error message to the user for right
now.
So I'm going to add in a new state variable at the top of my component to track whether or not we have
an error.
So I'll make a new state variable called e r r short for error and set E R and by default its value
will be no.
So if error is equal to null.
That means everything is OK if we fall into the catch case right here.
We're going to update the value of error.
So I'll say set error with E.
Like so.
So that's the error that comes out the catch case so now we've got this variable that's going to change
over time depending upon whether or not we have an error message we can then use that to decide whether
or not to show an error message to our user down inside of our ASX block.
So right after my map I'm going to put in a ternary expression and I'll say if error is defined then
I want to show a text element that says something like Please enable location services and then otherwise
we'll just show.
No.
Which basically means of course show nothing.
OK so it looks pretty good.
Now the very last thing we need to do is make sure that we eventually call start watching.
So once again we want to call that anytime our component is first displayed on the screen.
So make use of that use effect hook so right after start watching.
We'll call use effect.
Passing a function inside there I'll call start watching and we probably only want to run this code
one time when our component is first displayed on the screen.
So going to pass in as a second argument an empty array like so OK.
So that should be enough to get started.
Now before I test this there's one quick thing I want you to understand right now the only thing we're
doing is requesting permission from the user to track their location.
And then if they reject it we want to show an error message.
So right now we are really testing the case in which we reject this now.
The issue here is that at present on ISIS or at least on the ISIS simulator that I'm running if I reject
permissions it does not result in an error.
And that is a bug with the expo location library.
So when I test this out on ISIS I am not going to see an error message.
And once again that is a bug.
Now hopefully by the time you're watching this video that bug will be fixed the code that we have right
now should work correctly on Android and I'll be able to show it to you.
But once again there is a bug inside the expo location library where rejecting permissions on IWM is
just not resulting in an air OK.
So going to see this file and then I'll do a quick test.
So back on my IOW device right here I'll go over to track create.
Now when I go to check right you'll notice that I do not get any kind of permission request right now
and that's because I have already approved this expo application to use location services at some point
in the past.
So this kind of throws us into an interesting little bind here.
You might have seen the pop up right away but once you click confirm or reject or whatever you hit Well
you're never gonna see that window again because remember we only get displayed at that pop up window
one time once a user grants permission or rejects it any time we call it request permissions.
Again the underlying operating system is just going to automatically resolve that thing for us so this
really.
Like I said throws this into an interesting question how do we actually effectively test out this permission
stuff repeatedly if every single time we do it it's going to be resolved automatically one way or another
by the operating system.
Well that's a really interesting thing.
So let's take a quick pause right here we're gonna come back the next video and figure out how we can
kind of really effectively test this permission stuff.