Getting user location in iOS
People often use location-based applications that use the user's location to provide them better services. For example, Uber, Ola, Flipkart, Amazon, etc., are using user's locations to get the best deals and services. However, in iOS, we can use CoreLocation to get the user's location. We can also update the user's location when the app is in the background.
This tutorial will discuss how to get the user's location in real-time. We will also create a demo iOS app which shows fetches the user's longitude and latitude and shows the result in the application.
Consider the following steps to create a location-based iOS application.
1. Create a new XCode project as LocationDemo.
2. Create an instance of UILabel to represent the location in the ViewController.
As, we have initialized locationLbl with the properties like backgroundColor, numberOfLines, textAlignment, and font. We have updated the frame of locationLbl in viewDidLoad() and added it to the view. This will display an empty label with a black background color in the center of the ViewController, as shown in the below image.
3. Configure info.plist to get the permissions from the user
We need to create an entry into info.plist file for whatever third-party device resource we use in our application, such as camera, gallery, etc. In this scenario, we need to add privacy properties in the info.plist to access the user's location. For this purpose, navigate to info.plist in XCode, click the + icon and choose the following two privacy permissions.
However, we can also add the following code to the info.plist file if it is being accessed as source code.
The description text will be displayed to the user for getting permissions.
4. Use CoreLocation to fetch Location.
We need to instantiate CLLocationManager class to configure, start and stop the CoreLocation Services. CoreLocation is a framework that is used to obtain the geographic location and orientation of a device. This framework gathers data using all available components on the device, including the Wi-Fi, GPS, Bluetooth, magnetometer, barometer, and cellular hardware.
To user CLLocationManager, import the CoreLocation framework in ViewController. In this demo project, we have instantiated CLLocationManager as given below.
The requestAlwaysAuthorization() method is showing the permission alert view to the user.
Once the user tap Allow Once, the startUpdatingLocation() method gets the user's location from the device's GPS.
5. Implement the CLLocationManagerDelegate protocol
To access the user's actual location data in longitude and latitude, we need to implement the CLLocationManagerDelegate protocol and use its method didUpdateLocations(). For this purpose, we need to make our ViewController conform to the CLLocationManagerDelegate and set the location object's delegate property to self. Finally, define the didUpdateLocations() method which will be called each time, the location is updated.
Set the location's delegate property to self and add the following code to ViewController.
To get the location displayed in the simulator, we need to navigate to simulator -> Debug -> Location -> Freeway Drive.
However, This will set the text of locationLbl to the latitude and longitude of the device location, as shown below.
6. Update the location in background modes
Our code will perfectly work fine until our application remains in the foreground state. Whenever our app goes to the background, the location updates will stop working.
However, we can enable the background mode functionality by adding the Background Modes Capability in the XCode. For this purpose, navigate to Application Target -> Signing and Capabilities and select the Background Modes after tapping the + icon in the top right.
Now, set the allowsBackgroundLocationUpdates property of CLLocationManager object (location) to true.
This enables the location updates in background modes as well. Now we will get the location updates when the app remains in background mode also. To test this, we can now see the location icon flashes in the top left corner of the simulator screen, which shows that the location updates are active.
The full source code of the View Controller is given below.