Skip to content
tezvyn:

Request the user's location one time

Source: interviewEasyHow cards are made

Summary

The Core Location permission and one-shot fetch flow.

Key points

Add a usage description to Info.plist, set a CLLocationManager delegate, request when-in-use authorization, call requestLocation, handle didUpdateLocations and didFailWithError.

WHAT THIS TESTS This verifies you know Core Location's permission gating, the dedicated one-shot API, and the delegate callbacks, plus the privacy plist requirement that trips up beginners.

A GOOD ANSWER COVERS First add a usage-description string to Info.plist, NSLocationWhenInUseUsageDescription, explaining why you need location; without it the system denies access and may crash. Create and retain a CLLocationManager instance, set its delegate to your object conforming to CLLocationManagerDelegate. Call requestWhenInUseAuthorization() to prompt the user. React to the authorization result in the delegate, and once you have when-in-use or always authorization, call requestLocation(), which is purpose-built to deliver a single location fix and then stop. Implement locationManager(_:didUpdateLocations:) to read the most recent CLLocation from the array, and locationManager(_:didFailWithError:) to handle errors like denied or unavailable.

COMMON WRONG ANSWERS Calling requestLocation() before authorization is determined, which fails silently or errors. Forgetting the Info.plist key, the single most common cause of no prompt appearing. Using startUpdatingLocation() and immediately stopping, which is wasteful compared to requestLocation(). Not retaining the manager, so it deallocates before the callback.

LIKELY FOLLOW-UPS The newer locationManagerDidChangeAuthorization(_:) delegate that replaced the deprecated didChangeAuthorization status callback. The difference between when-in-use and always authorization. How requestLocation respects desiredAccuracy and times out. What happens if the user picks the temporary precise-location toggle.

ONE CONCRETE EXAMPLE In your view controller you store let manager = CLLocationManager(), set manager.delegate = self, and call manager.requestWhenInUseAuthorization(). In locationManagerDidChangeAuthorization you check manager.authorizationStatus, and if it is authorizedWhenInUse you call manager.requestLocation(). The system delivers locationManager(_:didUpdateLocations:) with one CLLocation; you read locations.last.coordinate and use it. If the user denied access, didFailWithError fires and you show a message guiding them to Settings.

Interview question

A developer calls requestLocation() immediately after creating CLLocationManager and nothing happens. What is the most likely root cause?

  • a.desiredAccuracy defaults too low to produce a fix
  • b.The manager must be a singleton to deliver updates
  • c.Authorization was never requested or granted before the callCorrect
  • d.requestLocation only works on a background queue
Why?

requestLocation needs an authorized status; calling it before requesting and receiving when-in-use authorization yields no fix. The accuracy default is fine, and the manager need not be a singleton, only retained.

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

Read the original → developer.apple.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

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