Determine User’s Current Location Example in Swift

In this Swift code example we will learn how to determine user’s current location using CLLocationManager and CLLocationManagerDelegate. The code example below will cover:

  • Request user’s permission for your app to always be able to read user’s location. Optionality you could use requestWhenInUseAuthorization() instead of requestAlwaysAuthorization()
  • Update projects Info.plist with String values for NSLocationAlwaysUsageDescription and NSLocationWhenInUseUsageDescription alert dialogs
  • Implement locationManager didUpdateLocations function to continuously listen for location updates and call stopUpdatingLocation() to stop listening for location updates
  • Get user’s current location latitude and longitude

Info.plist

For your app to be able to request user’s current location, you will need to open Info.plist file as Source Code and add two new keys and corresponding values like for example I did:

 
	NSLocationAlwaysUsageDescription
	Will you allow this app to always know your location?
	NSLocationWhenInUseUsageDescription
	Do you allow this app to know your current location?

Determine User’s Current Location. Complete Example in Swift

 
import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {
    
    var locationManager:CLLocationManager!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        determineMyCurrentLocation()
    }
    
    
    func determineMyCurrentLocation() {
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()
        
        if CLLocationManager.locationServicesEnabled() {
            locationManager.startUpdatingLocation()
            //locationManager.startUpdatingHeading()
        }
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let userLocation:CLLocation = locations[0] as CLLocation
        
        // Call stopUpdatingLocation() to stop listening for location updates,
        // other wise this function will be called every time when user location changes.
        
       // manager.stopUpdatingLocation()
        
        print("user latitude = \(userLocation.coordinate.latitude)")
        print("user longitude = \(userLocation.coordinate.longitude)")
    }
    
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
    {
        print("Error \(error)")
    }
}

Learn iOS Development with these Video Courses


How to Make a Freaking iPhone App - iOS 10 and Swift 3
icon icon


iOS 10 & Swift 3: From Beginner to Paid Professional
icon


The Complete iOS 10 Developer Course - Build 21 Apps
icon


Swift 3 - Learn to Code with Apple's New Language
icon icon


Learn How to Build Mobile Apps for iOS with Swift, PHP and MySQL
icon icon

Follow me on one of your favourite social networks to learn about new video tutorials and code examples:

Twitter: @SwiftVideoBlog
Google Plus: https://plus.google.com/+SergeyKargopolov/posts
Facebook: Swift Developer Blog on Facebook