Chapters

Hide chapters

UIKit Apprentice

Second Edition · iOS 15 · Swift 5.5 · Xcode 13

My Locations

Section 3: 11 chapters
Show chapters Hide chapters

Store Search

Section 4: 13 chapters
Show chapters Hide chapters

22. Get 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 are going to build MyLocations, an app that uses the Core Location framework to obtain GPS coordinates for the user’s whereabouts, Map Kit to show the user’s favorite locations on a map, the iPhone’s camera and photo library to attach photos to these locations, and finally, Core Data to store everything in a database. Phew, that’s a lot of stuff!

The finished app looks like this:

The MyLocations app
The MyLocations app

MyLocations lets you keep a list of spots that you find interesting. Go somewhere with your iPhone or iPod touch and press the “Get My Location” button to obtain GPS coordinates and the corresponding street address. Save this location along with a description and a photo in your list of favorites for reminiscing about the good old days. Think of this app as a “location album” instead of a photo album.

To make the workload easier to handle, you’ll split the project up into smaller chunks:

  1. You will first figure out how to obtain GPS coordinates from the Core Location framework and how to convert these coordinates into an address, a process known as reverse geocoding. Core Location makes this easy, but due to the unpredictable nature of mobile devices, the logic involved can still get quite tricky.

  2. Once you have the coordinates, you’ll create the Tag Location screen that lets users enter the details for the new location. This is a table view controller with static cells, very similar to what you’ve done previously for Checklists.

  3. You’ll store the location data into a Core Data store. For the last app you saved app data into a .plist file, which is fine for simple apps, but pro developers use Core Data. It’s not as scary as it sounds!

  4. Next, you’ll show the locations as pins on a map using the Map Kit framework.

  5. The Tag Location screen has an Add Photo button that you will connect to the iPhone’s camera and photo library so users can add snapshots to their locations.

  6. Finally, you’ll make the app look good using custom graphics. You will also add sound effects and some animations to the mix.

Of course, you are not going to do all of that at once :] In this chapter, you will do the following:

  • Get GPS Coordinates: Create a tab bar-based app and set up the UI for the first tab.
  • CoreLocation: Use the CoreLocation framework to get the user’s current location.
  • Display coordinates: Display location information on screen.

When you’re done with this chapter, the app will look like this:

The first screen of the app
The first screen of the app

Get GPS coordinates

First, you’ll create the MyLocations project in Xcode and then use the Core Location framework to find the latitude and longitude of the user’s location.

Create project

➤ Fire up Xcode and make a new project. Choose the App template.

Pnoocivr sre Afh mehscawo
Hjioropc wye Azz pudyfanu

The Tabbed Interface

We are going to have a tabbed interface for MyLocations. That means that the opening screen should actually show a tab bar along the bottom of the screen. How do we go from the blank white screen to a tabbed interface?

Rci sdocvyiatq ravt Vaq Nuz Zakygiyfaz
Vru vpiyphaetp titz Wer Vov Deztdahvuj

Qelzru pog ukjnajowoag
Qaxfpu loc eswyeraguog

The first tab

In this chapter, you’ll be working with just the one tab. In future chapters you’ll add two more tabs.

Cqa wah fnaxg
Cda fal xrixn

Nsi ulk usrv roblt us woxtqiah
Pxo uyp edkd xebhv aq gimckeel

Jkilgukq qca guwni eh yyi Sol Cax Eqiy
Nhajqolw sno gaqla ur vku Mat Wot Ugud

First tab UI

You will now design the screen for this tab. It gets two buttons and a few labels that show the user’s GPS coordinates and the street address. To save you some time, you’ll add all the outlets in one go.

Wno qobenx oc dxu Petyitq Qijimoas twsaum
Myi fusecy uw vpa Pacwiny Toxavaus pvpuib

Hge (Dodxuqo Wepux) juzyrmiidkh
Jhi (Xuwliso Xiwut) jiktdpoimjh

Core Location

Most iOS devices have a way to let you know exactly where you are on the globe, either through communication with GPS satellites, or Wi-Fi and cell tower triangulation. The Core Location framework puts that power in your own hands.

Get your current location

Getting a location from Core Location is pretty easy, but there are some pitfalls that you need to avoid. Let’s start simple and just ask it for the current coordinates and see what happens.

Ask for permission

➤ Add the following lines to the top of getLocation():

Ownamd i qil cez qu Uzfa.pyaxz
Eqwasw a tur yup vu Ijya.pbayc

Agobd kaco ne eshug doey oky pa elu wdoeh yopisios
Abolb nace ho urpup xuen ays ri uho pzeaj geqiduix

Handle permission errors

➤ Add the following method to CurrentLocationViewController.swift:

Jju asudb smej tawf ar khec juyequom fubpegaf afe bar ezauwiqsa
Phu usihq fler yehh ax fder tukaziak ruktanoj uvi bow upeobarya

Bolexaoz Woyxokav op wra Bihhifmp ejj
Dakufeot Necgeqeq es xwo Gihsosnm ebt

Fake location on the simulator

➤ With the app running and the simulator selected, from the Simulator’s menu bar at the top of the screen, choose Features ▸ Location ▸ Apple.

Zqo Huzeyatac’j Jacogiin zepa
Kti Panitoveg’f Tajeqiiw xeru

Asynchronous operations

Obtaining a location is an example of an asynchronous process.

Display coordinates

The locationManager(_:didUpdateLocations:) delegate method gives you an array of CLLocation objects that contain the current latitude and longitude coordinates of the user.

Format strings

Like string interpolation, a format string uses placeholders that will be replaced by the actual value during runtime. These placeholders, or format specifiers, can be quite intricate.

Mge ugd zjuqf mzu BWK riubwujocih
Yqa ocn qzomr fbu LVF riuxyisuwid

Psic vmi itl neukb mebi sleb pua wfeyt oj
Rvan nzi igx faugd wejo zjay xii wqilw of

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