Determine User’s Current Location Example in Swift

In this example Swift code you will learn how to get the current location of the user using CLLocationManager and CLLocationManagerDelegate. Below we look at a sample code:

  • Ask the user’s permission so that your application can always read the user’s location. You can use requestWhenInUseAuthorization() instead of requestAlwaysAuthorization(),
  • Update Info.plist projects with string values for the NSLocationAlwaysUsageDescription and NSLocationWhenWhenInUseUsageDescription dialogs,
  • Implement the locationManager didUpdateLocations function to continuously wait for location updates and call stopUpdatingLocation() to stop waiting for location updates,
  • Determines the current position of the user in latitude and longitude.


In order for your application to be able to retrieve the current location of the user, you need to open the Info.plist file as source code and add the two new keys and their corresponding values, as I did for example:

Do you always want to know your location with this application?
NSL LocalizationWhenWhenWhen using
Do you tell this application your current location?

Determine the current location of the user. Full example in Swift

Import UIKit
class ViewController : UIViewController, CLLocationManagerDelegate.

var locationManager:CLLocationManager!

Override viewDidLoad() {
// Make any additional settings after loading the view, usually from the pen.

Override didReceiveMemoryWarning() {
// Remove all resources that can be recreated.

Replace the viewWillAppear(_ animated : Bool) {


func defineMyCurrentLocation() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest

if CLLocationManager.locationServicesEnabled() {

locationManager(_ manager : CLLocationManager, didUpdateLocations location : [CLLocation]) enable [
lets userLocation:CLLocation = locations[0] as CLLocation.

// Call stopUpdatingLocation() to stop waiting for location updates,
// otherwise this function will be called every time the user changes location.

// manager.stopUpdatingLocation()

print (user latitude = (userLocation.coordinate.latitude))
print (user longitude = (userLocation.coordinate.longitude))

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)

For more information about the Swift code examples and tutorials, see the Swift code examples page on this site.

Written by Sergei Kargopolov

get current location swiftui,didupdatelocations not called swift,get current location xcode 11,swift requestlocation example,get current location google maps ios swift,how to get current location address in swift,cllocation get longitude latitude,swift get lat lon,swift 5 get latitude and longitude,how to find latitude and longitude in swift,swift get user location once,swift get user country,get user location swiftui,get location in background ios swift,swift get geo location,mapkit current location,update user location swift,show current location on google map ios swift,requestlocation swift,isauthorizedforpreciselocation,startupdatinglocation swift,corelocation tutorial,swift location tracking,standard location service swift,ios visits,swiftui get location,core location coordinates,apple significant change location service,swift 4 get user current location,show current location on map ios swift,get user location in swift 4,how to get current location latitude and longitude in swift 4,xcode swift get user current location,get latitude and longitude from address swift 4,how to get current location latitude and longitude in swift 5,how to set current location in swift 4

You May Also Like