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.

Factsheet

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:

NSLocationAlwaysUsageDescription
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
CoreLocation
class ViewController : UIViewController, CLLocationManagerDelegate.

var locationManager:CLLocationManager!

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

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

Replace the viewWillAppear(_ animated : Bool) {
super.viewWillAppear(animated))

DefinitionMyCurrentLocation()
}

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

if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
//locationManager.startUpdatingHeading()
}
}

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)
{
print(Error)
}
}

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

http://server.digimetriq.com/wp-content/uploads/2021/01/Determine-Users-Current-Location-Example-in-Swift.jpeg

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

Benefits of CBD Oil for Dogs

Maybe you have a hyperactive puppy who lets out a storm of…

Your EPUB files got corrupted? Here’s the fix

⇒ Get Adobe digital editions 3. Unpack and re-file the EPUB file…

“Can’t connect to this network” WiFi / Hotspot Issue on Windows 10 Laptop

Does Windows 10 keep displaying the error message Could not connect to…

How To Code a Character Variable into Integer in Pandas?

If you work with a Pandas database that contains variables of different…