Get Current Location in Swift IOS

Apple provides the CoreLocation Framework to fetch the user’s current location through GPS. In the following article its explained how to get current location in Swift in the form of Latitude & Longitude. Just copy the following code in your ViewController, rest of the work will be done by it. The working of the code is explained below.

Get Current Location in Swift IOS

Full Code to Get Current Location (Latitude & Longitude) in Swift IOS

import UIKit
import MapKit
import CoreLocation

class LocationViewController: UIViewController, CLLocationManagerDelegate {
    let locationManager = CLLocationManager()
    override func viewDidLoad() {

        // Do any additional setup after loading the view.
    func getCurrentLocation() {
        // Ask for Authorisation from the User.

        // For use when app is in foreground

        if CLLocationManager.locationServicesEnabled() {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
        let lat = Double(locValue.latitude)
        let lng = Double(locValue.longitude)
        print("cordinates = \(lat) \(lng)")
   //   locationManager.stopUpdatingLocation() // If you only want to receive location once

How does it work?

As you can see in the above code, first of all we need to import CoreLocation framework to use its features in Swift. You also need to declare an object of class CLLocationManager at your class level. The method getCurrentLocation()requests permissions from the user. If the user did not allow to access his location yet he will see a popup to allow the location permission to the App. This getCurrentLocation() method just requests the Location Manager to get receive location through its Delegate.

Don’t forget to implement CLLocationManagerDelegate in your ViewController, as you can see in the above code its implemented. When the location is available, delegates following method will get called automatically and you will receive your location’s latitude & longitude.

locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 

If you only want to get location update once, in this delegate method you can stop listening location using locationManager.stopUpdatingLocation() method. Otherwise, this delegate method will keep on calling whenever users’ latitude and longitude are changed.

Add LocationUsageDescription Strings in your Info.plist

Don’t forget to add Location Usage Description strings in your info.plist file. For that purpose Open your info.plist file as a source and add descriptions under the top-level <dict> tag in the following way.

<string>This allows HandyOpinion App to use your location to find nearby friends more accurately.</string>
<string>This allows HandyOpinion App to use your location to find nearby locations.</string>

Apple Rejects App if Location Usage Description is not well written

Yes, it happens usually. Developers just put a general string as Location Usage Description which is not meaningful and Apple rejects such App and asks to modify it. Actually you need to add a meaning full reason that, Why this App really needs that location permission. The same happens for other permissions as well, like the camera, etc.

That’s it. Enjoy 🙂

Next: Find Distance between 2 locations in SWIFT

Here you can also find many other Swift & IOS coding articles and tutorials.

Please share this post:
Native Mobile Application Developer (Android + IOS) having experience in Java, Swift, Kotlin, Objective C, Unity, C#, C/C++, NODE JS & PHP.

One thought on “Get Current Location in Swift IOS

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: