Getting the user's location

Platform & distribution Junior Swift 5.9+ iOS 17+ Reviewed By Alok Singh

Pick the fix

You want to show nearby stores once when a screen opens. Best approach?

Options
Hint 1 of 1

How many locations do you need?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A Request when-in-use permission and take one location, for example the first value from CLLocationUpdate.liveUpdates()

Correct. One fix with the least permission.

Why not the others?
  • B Request Always permission. Background access isn't needed.
  • C Start continuous updates with best accuracy and never stop. It drains the battery for a one-time need.
  • D Use significant-change monitoring. It's for background movement, and too coarse for this.

30-second answer

Add the usage description to Info.plist, request when-in-use permission with CLLocationManager, and ask for the accuracy you need. On iOS 17+, CLLocationUpdate.liveUpdates() gives an async sequence of updates; for a single fix, take the first update or use requestLocation(). Ask for Always permission only for features that truly need background location.

Practise saying it · 30 s. No recording, just a timer

Explain how you get location responsibly.

Say your answer out loud, then check which of these points you covered:

  • Usage description
  • When-in-use
  • Async updates
  • Stop when done

Deep dive

Users can grant approximate location; check accuracyAuthorization.

CLServiceSession (iOS 18) declares when your app needs authorization.

Stop updates as soon as you have what you need.

Explain the benefit before showing the system prompt.

Common mistakes

  • Requesting Always at first launch.
  • Running best accuracy updates continuously.

Senior take & follow-ups

Seniors ask for the least permission and accuracy that works.

Follow-up questions

Think of your answer first, then open the hint.

  1. How do you handle approximate location?

    Show hint

    Check accuracyAuthorization and request temporary full accuracy if needed.

  2. What does liveUpdates give you?

    Show hint

    An AsyncSequence of CLLocationUpdate values.

Suggest a fix if something here is wrong or out of date.