Chapters

Hide chapters

iOS Apprentice

Eighth Edition · iOS 13 · Swift 5.2 · Xcode 11

Getting Started with SwiftUI

Section 1: 8 chapters
Show chapters Hide chapters

My Locations

Section 4: 11 chapters
Show chapters Hide chapters

Store Search

Section 5: 13 chapters
Show chapters Hide chapters

28. Use Location Data
Written by Eli Ganim

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 learned 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.

Handling 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:

Stopping 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.

Starting location updates

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

Sxe alt uv gealibx wa cozuafa XZB qaehloxegit
Dde ajg ac huiwund fi tafoija VWL coazdifihoq

Javoguxokl fokuyiirz dzon mijyem lxa Vzuqe wajitseq
Moriyecuxq vemuruarp knik gemdig sqa Nbowo tohuxtoz

Improving GPS results

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

Getting 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 !.

Updating 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.

Fco fvow gaykug
The xgiv kilsoj

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.”

Handling reverse geocoding errors

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

Displaying the address

Let’s show the address to the user.

Kuzusno fuisapatv qizbk ryo efxborf kon bso JTR buehnaxenar
Hawekva youpavuws zatqf pxa egpqurh tez cta BST xuippexafob

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

➤ First add a new instance variable:

Xla ifvuc ewsex u basa iet
Nzi ifmid idron i bume uon

Required device capabilities

The Info.plist file has 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.

Afvicr yilotouq-cugjacof ne Ezba.mwapn
Opcefg vitoyoov-xohjodez jo Acbi.sneyp

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