What is CoreLocation and why is it needed?
\nCoreLocation is a built-in framework from Apple for iOS, macOS, watchOS, and tvOS platforms. It provides developers with access to hardware and software tools for determining the device's location. Unlike third-party libraries, CoreLocation does not require installation via package managers (CocoaPods, SPM) — it is part of the standard SDK.
\nKey features of the framework:
\n- \n
- Determining current geographic coordinates (latitude and longitude). \n
- Real-time location change tracking. \n
- Geofencing — receiving notifications when entering/exiting a specified area. \n
- Determining direction (compass) and movement speed. \n
- Reverse geocoding (converting coordinates to an address) and forward geocoding (address to coordinates). \n
The library is essential for navigation apps, delivery services, fitness trackers, social networks with geotags, and any other applications that require knowing the user's location.
\n\nInstalling CoreLocation
\nSince CoreLocation is part of the operating system, it does not need to be installed via pip or npm. To use it, simply import the module into your code.
Important: To work with geolocation, you must add a usage description to your project's Info.plist file. Without this, the app will crash when attempting to request permission.
Add one of the following keys (depending on when you need the location):
\n- \n
NSLocationWhenInUseUsageDescription— for access only while the app is actively in use. \n NSLocationAlwaysAndWhenInUseUsageDescription— for constant access (even in the background). \n
As the value, provide a string that will be shown to the user in the permission request dialog (e.g., "The app needs access to geolocation to display nearby cafes").
\n\nQuick Start with Code Example
\nLet's look at a minimal example of getting the user's current coordinates.
\nimport CoreLocation\n\nclass ViewController: UIViewController, CLLocationManagerDelegate {\n\n let locationManager = CLLocationManager()\n\n override func viewDidLoad() {\n super.viewDidLoad()\n locationManager.delegate = self\n locationManager.requestWhenInUseAuthorization()\n locationManager.startUpdatingLocation()\n }\n\n // MARK: - CLLocationManagerDelegate\n func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {\n guard let location = locations.last else { return }\n print("Latitude: \\(location.coordinate.latitude)")\n print("Longitude: \\(location.coordinate.longitude)")\n // Stop updating to save battery\n locationManager.stopUpdatingLocation()\n }\n\n func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {\n print("Error getting location: \\(error.localizedDescription)")\n }\n}\n\nThis code creates a manager, requests permission from the user, and starts tracking. Once the coordinates are obtained, they are printed to the console.
\n\nKey Methods/Functions of CoreLocation
\nBelow are the key methods and properties of the CLLocationManager class, as well as helper classes.
1. requestWhenInUseAuthorization()
\nDescription: Requests the user's permission to access geolocation only while the app is in use (the app is active on the screen).
\nParameters: None.
\nExample:
\nlet manager = CLLocationManager()\nmanager.requestWhenInUseAuthorization()\n\n\n