DEV Community

Cover image for Get User Location Real-Time Swift 5

Get User Location Real-Time Swift 5

Raja Tamil
Hi I am Raja Tamil • Best Selling Instructor at Udemy • Featured Author @ • Founder of (20K visitors/month)
・2 min read

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() {
        latLngLabel.frame = CGRect(x: 20, y: view.bounds.height / 2 - 50, width: view.bounds.width - 40, height: 100)
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() {

   func getUserLocation() {
        locationManager = CLLocationManager()
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


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...

Discussion (0)