Chapters

Hide chapters

UIKit Apprentice

Third Edition · iOS 18 · Swift 5.10 · Xcode 16

My Locations

Section 3: 11 chapters
Show chapters Hide chapters

Store Search

Section 4: 13 chapters
Show chapters Hide chapters

23. Use Location Data
Written by Fahim Farook

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

You’ve learnt how to get GPS coordinate information from the device and to display the information on screen.

In this chapter, you will learn the following:

  • Handle GPS errors: Receiving GPS information is an error-prone process. How do you handle the errors?
  • Improve GPS results: How to improve the accuracy of the GPS results you receive.
  • Reverse geocoding: Getting the address for a given set of GPS coordinates.
  • Testing on device: Testing on device to ensure that your app handles real-world scenarios.
  • Support different screen sizes: Setting up your UI to work on iOS devices with different screen sizes.

Handle GPS errors

Getting GPS coordinates is error-prone. You may be somewhere where there is no clear line-of-sight to the sky — such as inside or in an area with lots of tall buildings — blocking your GPS signal.

There may not be many Wi-Fi routers around you, or they haven’t been catalogued yet, so the Wi-Fi radio isn’t much help getting a location fix either. And of course your cellular signal might be so weak that triangulating your position doesn’t offer particularly good results either.

All of that is assuming your device actually has a GPS or cellular radio. I just went out with my iPod touch to capture coordinates and get some pictures for this app. In the city center it was unable to obtain a location fix. My iPhone did better, but it still wasn’t ideal.

The moral of this story is that your location-aware apps had better know how to deal with errors and bad readings. There are no guarantees that you’ll be able to get a location fix, and if you do, then it might still take a few seconds.

This is where software meets the real world. You should add some error handling code to the app to let users know about problems getting those coordinates.

The error handling code

➤ Add these two instance variables to CurrentLocationViewController.swift:

Stop location updates

If obtaining a location appears to be impossible for wherever the user currently is on the globe, then you need to tell the location manager to stop. To conserve battery power, the app should power down the iPhone’s radios as soon as it doesn’t need them anymore.

Start location updates

➤ Also add a new startLocationManager() method — I suggest you put it right above stopLocationManager(), to keep related functionality together:

Rvo ozk ej bauraly po jaqiuqa LGY liuldanijuc
Qpe ojr ep goukomm ni heseinu VNW yiipfoqibus

Improve GPS results

Cool, you know how to obtain a CLLocation object from Core Location and you’re able to handle errors. Now what?

Get results for a specific accuracy level

➤ Change locationManager(_:didUpdateLocations:) to the following:

Short circuiting

Because location is an optional object, you cannot access its properties directly — you first need to unwrap it. You could do that with if let, but if you’re sure that the optional is not nil you can also force unwrap it with !.

Update the UI

To make this clearer, you are going to toggle the Get My Location button to say “Stop” when the location grabbing is active and switch it back to “Get My Location” when it’s done. That gives a nice visual clue to the user. Later on, you’ll also show an animated activity spinner that makes this even more obvious.

Reverse geocoding

The GPS coordinates you’ve dealt with so far are just numbers. The coordinates 37.33240904, -122.03051218 don’t really mean that much, but the address 1 Infinite Loop in Cupertino, California does.

The implementation

➤ Add the following properties to CurrentLocationViewController.swift:

Closures

Unlike the location manager, CLGeocoder does not use a delegate to return results from an operation. Instead, it uses a closure. Closures are an important Swift feature and you can expect to see them all over the place — for Objective-C programmers, a closure is similar to a “block”.

Handle reverse geocoding errors

➤ Replace the contents of the geocoding closure with the following:

Display the address

Let’s show the address to the user.

Pihisro moezatulp bilkb wjo alsfict foc cwa BGK niuccehitab
Yiliddo ceefasutn yofqs lla oxfcasb qeb mlo FSM hoabfalilak

Testing on device

When I first wrote this code, I had only tested it on the Simulator. It worked fine there. Then, I put it on my iPod touch and guess what? Not so good.

First fix

➤ Change locationManager(_:didUpdateLocations:) to:

Second fix

There is another improvement you can make to increase the robustness of this logic, and that is to set a time-out on the whole thing. You can tell iOS to perform a method one minute from now. If by that time the app hasn’t found a location yet, you stop the location manager and show an error message.

Jsu oyvod alqaf e peko oeg
Qqo ovdov ozquz a piva eix

Required device capabilities

The Info tab can have a key, Required device capabilities, that lists the hardware that your app needs in order to run. This is the key that the App Store uses to determine whether a user can install your app on their device.

Ivzoqd bezawoup-vadvikaj he Exyo.kroqz
Obvigg fenetuad-vehrijof ra Ogqo.tqocq

Attributes and properties

Most of the attributes in Interface Builder’s inspectors correspond directly to properties on the selected object. For example, a UILabel has the following attributes:

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now