DEV Community

Cover image for Get User Location Real-Time Swift 5
Raja Tamil
Raja Tamil

Posted on • Edited on

Get User Location Real-Time Swift 5

You’re going to learn how to get the user’s location in real-time using CoreLocation including getting updates when the app goes into background mode.

I assume that you have Xcode installed already and know how to create a project in Xcode.

STEP #1: Set Up LatLngLabel

Let’s create a UILabel called latLngLabel that displays the latitude and longitude values of the current location.

import UIKit

class ViewController: UIViewController{

    private let latLngLabel: UILabel = {
        let label = UILabel()
        label.backgroundColor = .systemFill
        label.numberOfLines = 0
        label.textAlignment = .center
        label.font = .systemFont(ofSize: 26)
        return label
    }()

    override func viewDidLoad() {
        super.viewDidLoad()        
        latLngLabel.frame = CGRect(x: 20, y: view.bounds.height / 2 - 50, width: view.bounds.width - 40, height: 100)
        view.addSubview(latLngLabel)
    }
}
Enter fullscreen mode Exit fullscreen mode

The LatLngUILabel is initialized with some properties and assigned a location (x, y) and dimensions (width, height) of it using frame property inside the viewDidLoad().

I could use Auto Layout but I used fixed values to position it on view for simplity sake.

Finally, add them to the view object using addSubView() method and the app view will look like this.

alt text

Add Privacy Properties To Info.plist

So, go to the project navigatorinfo.plist and hit the + sign at the top right in the key column and choose

  • Privacy – Location Always and When In Use Usage Description, and
  • Privacy – Location When In Use Usage Description

The value of these properties will actually be visible to the user on the permission alert view.

For Example: “You location is shared to the admin for efficient purposes”

alt text

Core Location

As you can see, I have imported CoreLocation at the top and declared locationManager which is optional with a type of CLLocationManager inside the class definition.

import UIKit
import CoreLocation

class ViewController: UIViewController{
    private var locationManager:CLLocationManager?
    ...

    override func viewDidLoad() {
        super.viewDidLoad()
        ...
        getUserLocation()
    }

   func getUserLocation() {
        locationManager = CLLocationManager()
        locationManager?.requestAlwaysAuthorization()
        locationManager?.startUpdatingLocation()
    }
}
Enter fullscreen mode Exit fullscreen mode

Then, instantiate CLLocationManager inside getUserLocation() function.

The requestAlwaysAuthorization() method is actually showing the permission alert view to the screen.

alt text

When the user gives permission by choosing Allow Once, the startUpdatingLocation() method is actually getting the location data from the device GPS.

And you can see the location icon at the status bar has a filled color.

alt text

CLLocationManagerDelegate()

To access an actual user location data, we need to use the didUpdateLocations() method which is a part of CLLocationManagerDelegate.

So add the CLLocationManagerDelegate in the class definition.

Continue Reading...

Top comments (0)