How to Use Core Location for a User’s Current Position
Core Location gives an iOS app access to location data from GPS, Wi-Fi, Bluetooth, and mobile networks. With the right permissions and a small amount of Swift code, you can find a device’s approximate or precise position and use it for maps, delivery tracking, weather, local search, or location-based reminders.
Getting a coordinate is straightforward, but a production app needs to handle permission states, inaccurate readings, disabled services, battery use, and privacy expectations. These details matter for Australian users in dense areas such as Sydney and Melbourne, as well as people travelling through regional Queensland, Western Australia, or areas with limited network coverage.
Add Core Location Permission To Your App
Start by importing the framework and creating a CLLocationManager. The manager requests access and delivers location updates through its delegate. Keep it as a property rather than creating it inside a method, because it must remain in memory while the system communicates with your app.
Before requesting access, add a usage description to the app’s Info.plist. For a foreground location feature, add Privacy - Location When In Use Usage Description, with a clear explanation such as:
We use your location to show nearby services and centre the map.
Apple displays this text in the permission alert. A vague message can make users distrust the app, especially when they are already careful about privacy and data collection. Your explanation should describe a real feature and avoid implying that location is required if the app can work without it.
The basic setup looks like this:
import CoreLocation
final class LocationService: NSObject, CLLocationManagerDelegate {
private let manager = CLLocationManager()
override init() {
super.init()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyHundredMeters
}
func requestPermission() {
manager.requestWhenInUseAuthorization()
}
}
Request Permission At The Right Moment
Ask for permission when the user reaches a feature that needs location, not immediately on the first launch. For example, a local café finder can request access when the user taps “Use My Location”. This timing gives users enough context to understand the benefit.
Most apps should begin with requestWhenInUseAuthorization(). It allows location access while the app is visible and is easier to justify than background access. Always-on tracking requires requestAlwaysAuthorization() and extra configuration, so use it only for a genuine requirement such as navigation, fitness recording, or a safety service.
The permission state can change at any time. On current iOS versions, implement locationManagerDidChangeAuthorization(_:) and respond to the relevant status:
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
switch manager.authorizationStatus {
case .authorizedWhenInUse, .authorizedAlways:
manager.requestLocation()
case .denied, .restricted:
print("Location access is unavailable")
case .notDetermined:
break
@unknown default:
break
}
}
If a user has denied access, calling the permission method again will not show the system prompt. Your interface should explain how to enable access in Settings, while still providing a useful alternative where possible.
Retrieve A Single Current Coordinate
For a button that centres a map or finds nearby shops, a one-time request is usually better than continuous tracking. Call requestLocation() after permission has been granted. The delegate then receives either a location or an error.
func getCurrentLocation() {
guard manager.authorizationStatus == .authorizedWhenInUse ||
manager.authorizationStatus == .authorizedAlways else {
return
}
manager.requestLocation()
}
func locationManager(
_ manager: CLLocationManager,
didUpdateLocations locations: [CLLocation]
) {
guard let location = locations.last else { return }
let latitude = location.coordinate.latitude
let longitude = location.coordinate.longitude
print("Latitude: \(latitude)")
print("Longitude: \(longitude)")
}
func locationManager(
_ manager: CLLocationManager,
didFailWithError error: Error
) {
print("Could not get location: \(error.localizedDescription)")
}
The CLLocation object contains more than latitude and longitude. Its horizontalAccuracy value estimates the radius of uncertainty in metres, while timestamp tells you when the reading was captured. A result with an accuracy of -1 is invalid, and an old reading may be unsuitable for navigation or time-sensitive features.
You can ignore readings that are too imprecise or too old:
guard location.horizontalAccuracy >= 0,
location.horizontalAccuracy <= 100,
abs(location.timestamp.timeIntervalSinceNow) < 30 else {
return
}
The correct threshold depends on the feature. A weather app may accept several hundred metres, while a turn-by-turn navigation screen needs a much more precise and frequently updated position.
Choose Accuracy And Manage Battery Use
desiredAccuracy expresses the level of precision your app would like. Options include kCLLocationAccuracyThreeKilometers, kCLLocationAccuracyHundredMeters, kCLLocationAccuracyNearestTenMeters, and kCLLocationAccuracyBest. Higher precision can consume more battery and may take longer to obtain.
For a local search screen, hundred-metre accuracy is often sufficient. A map marker near the centre of Adelaide does not need the same precision as a running app measuring a route through Canberra. Set the lowest useful accuracy, then stop updates when the feature no longer needs them.
For continuous tracking, use startUpdatingLocation() and stop it promptly:
manager.startUpdatingLocation()
// Later, when tracking is no longer needed:
manager.stopUpdatingLocation()
You can also set distanceFilter to control how far the device should move before another update is delivered:
manager.distanceFilter = 50
This is useful for delivery or transport apps, where an update every few metres would waste energy. If your app supports background tracking, configure background modes carefully and explain the behaviour clearly. Background access has a greater privacy impact and should never be enabled simply because it is convenient during development.
Handle Approximate Location And Real-World Conditions
Users can allow approximate location instead of precise location. This is common when someone wants nearby results without sharing an exact position. Check manager.accuracyAuthorization when your feature truly depends on precision, and design a fallback rather than blocking the entire app.
if manager.accuracyAuthorization == .reducedAccuracy {
print("The user has allowed approximate location")
}
If precise coordinates are essential, you can request temporary full accuracy for a purpose configured in the app’s property list. Use this sparingly. A public transport app might need it while guiding someone to a platform, but a general list of nearby restaurants usually does not.
Location can also fail indoors, underground, between tall buildings, or in remote areas. A person travelling across the Nullarbor may experience different results from someone standing in central Melbourne. Simulator testing is useful, but test on physical devices in open spaces, buildings, and areas with weak reception.
Practical Implementation Recommendations
- Explain the location feature before displaying Apple’s permission prompt.
- Use
requestLocation()for one-off searches and stop continuous updates promptly. - Validate
horizontalAccuracyandtimestampbefore using a coordinate. - Offer manual search, postcode entry, or map movement when location is unavailable.
- Store only the location data your feature genuinely needs and protect it appropriately.
Present Location Data Safely In The Interface
A coordinate is useful to software but rarely meaningful to a person. Convert it into a map marker, nearby result, suburb name, or distance. MapKit can display the position with MKMapView, while CLGeocoder can translate coordinates into a placemark. Reverse geocoding should be used thoughtfully because it may involve network activity and should not be repeated for every location update.
For an Australian app, a nearby search could show “Fremantle” or “Parramatta” rather than exposing a long decimal coordinate. Be careful with addresses around homes, schools, and workplaces. Showing a precise user location in a shared feed can create a safety and privacy risk.
Location-based products should also work across the local market rather than assuming one city or one kind of address. Australian users may search by suburb, postcode, street, or landmark, and regional users may have fewer nearby results. Distances should use kilometres, and interfaces should account for Australian time zones when location triggers reminders or opening-hours displays.
When the user taps a “current location” control, show a loading state and then communicate whether the result is precise, approximate, unavailable, or outdated. Avoid silently placing the map at an old coordinate. Clear status messages make the feature easier to trust and simpler to debug.
Build A Reliable Location Workflow
A dependable implementation follows a predictable sequence: explain the feature, check whether services are enabled, request the appropriate permission, obtain a coordinate, validate its quality, and stop work when the result is no longer needed. This workflow keeps location access focused and makes failures easier to handle.
Test every authorisation state in Xcode and on a physical iPhone. Check first launch, approximate access, denied access, restricted access, disabled Location Services, airplane mode, stale readings, and a user who changes permissions in Settings. Test both a dense Sydney neighbourhood and a less connected regional environment so assumptions about signal quality do not become bugs.
Core Location provides the position, but your app is responsible for deciding when that position is necessary and how it is presented. A strong junior iOS project combines correct delegate code with transparent permission copy, sensible accuracy settings, battery awareness, and a useful fallback. Remember that the best current-position feature is precise enough for its purpose, respectful of the user’s privacy, and resilient when location data is unavailable.