Get User Location Real-Time Swift 5
Tags: ios, swift, programming
Published: 2021-05-25 13:23:48

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.

{% youtube XQ268oQRuiY %}

###STEP #1: Set Up LatLngLabel

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

```Swift
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)
    }
}
```

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](https://softauthor.com/wp-content/uploads/2020/03/core-location-ios-latitude-longitude-1.png)

##Add Privacy Properties To Info.plist

So, go to the **project navigator** → **info.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](https://softauthor.com/wp-content/uploads/2020/03/add-privacy-location-properties-plist.png)

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

```Swift
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()
    }
}
```

Then, instantiate **CLLocationManager** inside **getUserLocation()** function.

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

![alt text](https://softauthor.com/wp-content/uploads/2020/03/core-location-ask-permission.png)

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](https://softauthor.com/wp-content/uploads/2020/03/location-indicator-status-bar.png)

###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...](https://softauthor.com/ios-swift-get-user-location-real-time/)