Skip to content
tezvyn:

Track location continuously in the background

Source: interviewMediumHow cards are made

Summary

Tuning Core Location for accuracy versus battery and enabling background updates.

Key points

Set desiredAccuracy, distanceFilter, activityType; enable the Location background mode and allowsBackgroundLocationUpdates; request Always authorization.

WHAT THIS TESTS This evaluates whether you understand the tradeoffs in Core Location configuration and the concrete project setup that lets location continue while the app is backgrounded.

A GOOD ANSWER COVERS On the CLLocationManager you tune desiredAccuracy: kCLLocationAccuracyBestForNavigation or best while actively navigating, stepping down to hundredMeters when less precision is acceptable to save power. distanceFilter sets the minimum movement, in meters, before an update fires, throttling callbacks. activityType, such as automotiveNavigation or fitness, lets the system optimize sensor use and pause updates intelligently. For background operation you must enable the Location updates background mode in the target's Signing and Capabilities, set manager.allowsBackgroundLocationUpdates = true, and request Always authorization, which needs NSLocationAlwaysAndWhenInUseUsageDescription in Info.plist. You also handle the blue background-location status bar indicator and consider pausesLocationUpdatesAutomatically.

COMMON WRONG ANSWERS Forgetting to enable the Location updates background capability, so updates stop when backgrounded. Always using best accuracy, draining the battery. Requesting only when-in-use authorization for a background feature. Not setting allowsBackgroundLocationUpdates, which silently prevents background delivery despite the capability.

LIKELY FOLLOW-UPS When to prefer significant-location-change monitoring or region monitoring to save power when continuous precision is not needed. How showsBackgroundLocationIndicator behaves. The deferred updates API. Handling the user choosing approximate location.

ONE CONCRETE EXAMPLE For a navigation feature you set manager.desiredAccuracy = kCLLocationAccuracyBestForNavigation, manager.activityType = .automotiveNavigation, manager.distanceFilter = 5, manager.allowsBackgroundLocationUpdates = true, and call manager.requestAlwaysAuthorization(). You tick Location updates under Background Modes and add the Always usage string. Now startUpdatingLocation() keeps delivering accurate fixes during turn-by-turn navigation even when the phone locks, while distanceFilter and activityType keep battery use reasonable when the user is stationary.

Interview question

Your navigation app stops receiving location updates the moment it is backgrounded, despite the Location background mode being enabled. What is the likely fix?

  • a.Lower desiredAccuracy to save power so the system keeps the app alive
  • b.Set allowsBackgroundLocationUpdates to true on the location managerCorrect
  • c.Call startUpdatingLocation again every time the app backgrounds
  • d.Switch to when-in-use authorization for lower overhead
Why?

The background capability alone is not enough; you must also set allowsBackgroundLocationUpdates = true (with Always authorization) for delivery to continue while backgrounded. Lowering accuracy or downgrading authorization would not restore background updates.

Just read this? Test yourself on what you have been reading.

Read the original → developer.apple.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on ios — each one lists the topics its interview covers.

See open roles