Getting User Location on iOS with Swift

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on everybody it's your boy kilo loco and today we're going to be accessing the user's location so we're going to be using core location to access location updates we'll be using swift ui for our ui and then we'll be using combine to pass data from our object that gets those locations to our content view so with all that said let's go ahead and first take a look at the code in case you don't want to watch this entire video so let's just take a look at the code real quick of what we're actually going to be writing i know some of y'all don't want to waste waste any time but this is going to be the object we're going to be creating i'm going to be covering why we're doing everything that we're doing here so we have a device location object we are accessing our location manager we're requesting location updates we're using a couple of different delegate methods to handle some of those updates and then what we'll be doing is using our publisher to send data over to our content view so as you can see here that's essentially what we're going to be covering so if that's all you're here for then good but if you're more interested in understanding how all this works make sure you stay tuned all right so let's start by creating a new file called device location service first what we want to do is actually import core location and combine since we're going to be using those two different libraries and we're going to also make sure that our our new class is going to conform to observable object so that we can notify swift ui of any updates in our publishers alright so as you can see we have our publisher which is going to be called coordinates publisher and it's going to be able to pass a cl location coordinate 2d through our publisher and if there are any errors we can also call a completion that will send an error down that uh down that publisher we are going to actually make this a singleton because we don't really need multiple instances getting the device location because it's all going to essentially be doing the same thing so that's why i'm making it a singleton next what we want to do is we actually want our class to be a subclass of ns object and then we're going to make it conform to cl location manager delegate so we wouldn't be able to conform to cl location manager delegate without it being a subclass of ns object and since we are subclassing ns object then that means we need to do an override on the init method and we need to call superdot in it next what we want to do is create the location manager which is the object that's going to be responsible for managing actually retrieving the location updates all right so as you can see here we have our location manager instance and we are going to make it a lazy lazy variable so that we can modify the manager as we create it so what we're going to do is we're going to create an instance of the manager then we'll eventually return it but before we return it we want to do a couple of things to it so the first thing that we're going to do is we're going to set the desired accuracy of our location manager now there's a bunch of different location accuracies that you can actually pick from um the way that i figure out how to actually implement this because they are these old objective c constants with a nasty little k in front of them um the way that i do it is i just remember k and then start typing best and then you should be able to get uh auto complete to pull up the rest from there what you could do is you could do command click into it you jump to definition and you'll actually be able to see the different ones that you can choose from now keep in mind that cl location accuracy is actually a type alias for double so you could technically just stick some doubles in there it's all based in meters so if you wanted to do that but uh ideally you're going to choose from one of these and then there's also this one down here ios 14 some some some something like that but anyways we're setting our desired accuracy the next thing that we want to do is also make sure that we are going to set this object the device location service as the delegate to this location manager and there we go so we're all set with the delegate next we're able to start implementing some of these delegate methods the first one is going to handle the location updates so let's implement that all right so the first one that we're going to do is going to be this one it's did update locations now you're going to get an array back of locations so as opposed to using all those locations let's just get back the last one and then we'll send that through our pipeline and there we go two lines of code gets it all taken care of we get our location then we simply send the coordinate through our publisher if you are curious about you know combined or something like that check out the video up there next we have a different delegate method that we're going to implement just in case something goes wrong something fails you know we're developers we should know that things break and fail so we need to handle those errors right so let's go ahead and implement the error delegate and there we go so now we're going to access our coordinates publisher again and instead of sending a value down our publisher what we're going to do is we're going to actually send a failure this will actually cancel out all subscriptions and they'll complete with an error which at that point we can then do something to handle that error but we're not going to cover that today so before we go any further what i want to do is i want to make sure that we're requesting access to the user's location it's something that you have to ask for on ios and before we even do that we want to make sure that we have the correct the correct key inside of our info.plist to ask for that uh privacy permission so head over to your info.plist it's pretty hidden if you're new to xcode13 and you go to info you go over here you do the plus and you're going to look you're going to scroll all the way down you're going to scroll all the way down until you find privacy dash location something so privacy dash location when in use usage description and then i'm going to say hook it up with that location homie homie like that put something legit there because it's going to be shown by use two users all right now that we have that in entered in let's head back over to the device location service and now let's actually get into requesting permission so let's start by creating a function for that all right so as you can see here we have this request location updates function and what it's going to do is it's going to switch over this authorization status property of the location manager right and then we we really only care about three different use cases either we haven't determined whether we have access or not in which in which case uh the operating system is going to pop up that alert saying hey this app wants to use your location can we do it and they're going to choose something right so that's what this is about and depending on whether you want always or just when in use you're going to choose the correct like the correct function for that right then you have the winning use and the always case right so you shouldn't really never hit this use case if we're requesting one in use but whatever anyways either we are authorized and what we'll do is we'll start updating location now whether you want to immediately start updating the location that's totally up to you and your logic but we're just doing that here and then the last use case is if we got anything else in which case we don't have access and we're not authorized to get the user's location now we can just break right here let's take it one step further let's create another publisher that will actually handle sending some type of value through a publisher so that we can update it in our ui or something like that later so you can see how that would work so up here at the top let's go ahead and go to let's create a new publisher so i'll create one called denied location access publisher it'll be a pass-through subject and we'll just simply send a void um no need to send any values unless you want to make it actually send the authorization status and you can handle it in the view it's up to you but essentially it's never going to fail um either we're going to get a value down it or we're not and hopefully most cases we won't so essentially what we'll do here is we'll just simply send that void like so and we should be able to do send like this technically i think you're supposed to do it like this but i think they have it set up so that you can just do send like this when it's a void all right so now that we have this there's also the use case that happens when we are changing the authorization status right so when we're not determined we're going to request that access right uh then what will happen is we'll eventually end up in this state assuming that they say it's okay to use their location so what we need to do is we need to monitor that other location manager delegate method and let's go ahead and implement that with very similar logic to here all right so we have this location manager did change authorization method and what we're doing is pretty much the same thing as above we're switching on the authorization status now as opposed to accessing the local uh the local location manager right this instead of accessing this thing right here what i usually usually like to do in delegate methods is use the value that's passed to me so this is still the same location manager and i usually like to use the variable that's passed to that method anyways so we have manager we're switching over that and we're once again checking uh the always uh and the when in use use cases when we're authorized and we'll start updating and then in any other case because we should have determined right we shouldn't ever see a not determined case at this state right so then what we're going to do is we're going to make sure that we call stop updating whether we were updating it or not we'll just call stop make sure that the manager stops and then we're also going to send denied location access through that publisher one more time so that was the implementation let's go ahead and jump into the usage right so how would we actually use this let's head back over to the content view and we're going to start off by creating a couple of properties here at the top all right let's take a look at these real quick so we have a we have access to our singleton the device location service we have that instance now um we are going to create a set of tokens just so that we can have the syncs because we're once again using combine we're going to have the syncs be stored inside of this tokens set and then we have our coordinates so um i'm using a tuple here uh with the variable names lat and lawn and we're gonna give it an initial value of zero zero and i like using this because i don't have to import core location to my ui framework uh it's just a lot better in my personal opinion if you're not working with like map view in this content view right so yeah anyways we have those variables set up next let's go ahead and start observing the different publishers that are provided by our device location service all right so this observe coordinate updates function is going to access the coordinates publisher and we want to make sure that we're receiving on the main thread uh so because we are going to be updating ui and stuff like that in the sync we have for the completion uh section we're just going to check if there's an error in there and then you can go ahead and handle the error whether you want to show an alert however you want to do that i'll leave that to you uh then in the receive value we get the cl uh we get that cl location uh 2d coordinate whatever it's called and then we're going to set our self.coordinates that's that tuple up at the top we're going to set it to this value right here where we have latitude and longitude right and then lastly stored in the func stored in the tokens let's do something very similar and observe the access denied publisher too all right and here we go so once again very similar situation observe location access deny that's going to be our denied location access publisher uh once again receive on that main thread and then go ahead and handle it however you want inside of this sync and store it in the tokens all right so with those two functions set up we're pretty much ready to go all we need to do is update our ui and have something to show so i'm going to actually display the latitude and longitude and some just very simple text views and then we want to make sure that we call all the relevant functions all right here we go so we have a v stack just showing our latitude and longitude um accessing that tuple to display the latitude and long longitude values that are uh sent here and then um when we do have the on appear called modif the on appear modifier called on our v stack we're gonna make sure we observe before we actually request location updates so we'll have both of our publishers and subscriptions set up before we even try to request location updates and then um potentially have any value sent down uh downstream to our subscriptions so with all that done i think we are ready to go let's go ahead and run this all right here we go so it pulls up our app this is uh this should be a fresh install and there we go we immediately get the operating system request to allow location access we do allow while using app and then as you can see we are getting location updates now if you don't see these getting updated just make sure that you go up to like the features you go to location and then you set it to like city run this way you can get actual location updates or else it'll just stay fixed on one one set of values but so that's going to be it for today i hope that you enjoyed the video as you can see accessing the user location not too crazy hard just how to set up a class but if you have any questions feel free to leave them in the comments down below same thing goes for if you have any video recommendations any topics that you want to cover make sure you leave them in the description below uh thank you again for your time and go out there and keep coding passionately
Info
Channel: Kilo Loco
Views: 279
Rating: undefined out of 5
Keywords: user location swift, device location swift, corelocation swift, core location swift, location manager swift, locationmanager swift, user location ios, device location ios, corelocation ios, core location ios, location manager ios, locationmanager ios, CLLocationManager, CLLocationManagerDelegate, CLLocationCoordinate2D
Id: 6bKWCS2Aw6s
Channel Id: undefined
Length: 15min 40sec (940 seconds)
Published: Thu Dec 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.