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

36. URLSession
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

So far, you’ve used the Data(contentsOf:) method to perform the search on the iTunes web service. That is great for simple apps, but I want to show you another way to do networking that is more powerful.

iOS itself comes with a number of different classes for doing networking, from low-level sockets stuff that is only interesting to really hardcore network programmers, to convenient classes such as URLSession.

In this chapter you’ll replace the existing networking code with the URLSession API. That is the API the pros use for building real apps, but don’t worry, it’s not more difficult than what you’ve done before — just more powerful.

You’ll cover the following items in this chapter:

  • Branch it: Creating Git branches for major code changes.
  • Put URLSession into action: Use the URLSession class for asynchronous networking instead of downloading the contents of a URL directly.
  • Cancel operations: Canceling a running network request when a second network request is initiated.
  • Search different categories: Allow the user to select a specific iTunes Store category to search in instead of returning items from all categories.
  • Download the artwork: Download the images for search result items and display them as part of the search result listing.
  • Merge the branch: Merge your changes from your working Git branch back to your main branch.

Branch it

Whenever you make a big change to the code — such as replacing all the networking stuff with URLSession — there is a possibility that you’ll mess things up. I certainly do often enough! That’s why it’s smart to create a Git branch first.

The Git repository contains a history of all the app’s code, but it can also contain this history along different paths.

You just finished the first version of the networking code and it works pretty well. Now you’re going to completely replace that with a — hopefully — better solution. In doing so, you may want to commit your progress at several points along the way.

What if it turns out that switching to URLSession wasn’t such a good idea after all? Then you’d have to restore the source code to a previous commit from before you started making those changes. In order to avoid this potential mess, you can make a branch instead.

Main branch Create branch The new branch Merge back into main branch Work on new features or bug fix Change in the original code
Branches in action

Every time you’re about to add a new feature to your code or have a bug to fix, it’s a good idea to make a new branch and work on that. When you’re done and are satisfied that everything works as it should, merge your changes back into the main branch. Different people use different branching strategies but this is the general principle.

So far you have been committing your changes to the “main” branch. Now you’re going to make a new branch, let’s call it “urlsession”, and commit your changes to that. When you’re done with this new feature you will merge everything back into the main branch.

You can find the branches for your repository in the Source Control navigator:

The Source Control branch list
The Source Control branch list

➤ Right-click on the branch name (main) to get a context-menu with possible actions and select New Branch from “main”…:

The branch context-menu
The branch context-menu

➤ You will get a dialog asking for the new branch name. Enter urlsession as the new name and click Create.

Creating a new branch
Creating a new branch

When Xcode is done, you’ll see that a new “urlsession” branch has been added and that it is now the current one.

This new branch contains the exact same source code and history as the main branch, or whichever branch you used as the parent for the new branch. But from here on out the two paths will diverge — any changes you make happen on the “urlsession” branch only.

Put URLSession into action

Good, now that you’re in a new branch, it’s safe to experiment with these new APIs.

A brief review of closures

You’ve seen closures a few times now. They are a really powerful feature of Swift and you can expect to be using them all the time when you’re working with Swift code. So, it’s good to have at least a basic understanding of how they work.

Handle status codes

After a successful request, the app prints the HTTP response from the server. The response object might look something like this:

Parse the data

➤ First, put iTunesURL(searchText:) back to the way it was — use ⌘+Z to undo.

Handle errors

➤ At the very end of the completion handler closure, below the if statements, add the following:

Cancel operations

What happens when a search takes a long time and the user starts a second search while the first one is still going? The app doesn’t disable the search bar, so it’s possible for the user to do this. When dealing with networking — or any asynchronous process, really — you have to think these kinds of situations through.

Search different categories

The iTunes store has a vast collection of products and each search returns at most 200 items. It can be hard to find what you’re looking for by name alone. So, you’ll add a control to the screen that lets users pick the category they want to search in. It will look like this:

Yuejydovh ed fxu Qabcneje lusajefd
Reimkyijw of zva Saxfpica gohumomd

Add the segmented control

➤ Open the storyboard. Drag a new Toolbar into the view and put it below the Search Bar. You will be using the Toolbar purely as a container for the segmented control.

Qpu Hashecvas Zicbviw medv uw o Loatqor qiyut dhi Faunth Num
Cye Cuhqisyik Nomgmik misf ey e Haamfoj rixep fye Juuczw Paq

Zhi kucudzud Rorbufdoh Xuhtsuf
Yhe fezeqqef Zabbeqliy Zabplol

Use the assistant editor

➤ Press Control+Option+⌘+Enter to open the Assistant editor and then Control-drag from the Segmented Control into the view controller source code to add the new outlet:

Agqotl eh olcuud joydiq nug xfe pixyelfox masdbeg
Ascopk ak ojtiot heqjub dog pbo savwoqlin camtyan

Nti reysahtig letrzeq ej opqaec
Yza quppuhlog gotxvaq uz acsaif

Use the segmented control

If you do a search now, you’ll notice that the first row of the table view is partially obscured again. Because you placed a toolbar below the search bar, you need to add another 44 points to the table view’s content inset.

Tei goy huv rirab hco qoegfg ri nojn a-rualw
Fio naz xod gudac wla daoktv mi sukj e-buirh

Set the launch screen

➤ Remove the LaunchScreen.storyboard file from the project.

Download the artwork

The JSON search results contain a number of URLs to images and you put two of those — imageSmall and imageLarge — into the SearchResult object. Now you are going to download these images over the Internet and display them in the table view cells. Downloading images, just like using a web service, is simply a matter of doing an HTTP request to a server that is connected to the Internet. An example of such a URL is:

SearchResultCell refactoring

First, you will move the logic for configuring the contents of the table view cells into the SearchResultCell class. That’s a better place for it. Logic related to an object should live inside that object as much as possible, not somewhere else.

UIImageView extension for downloading images

OK, here comes the cool part. You will now add an extension for UIImageView that downloads the image and automatically displays it via the image view on the table view cell with just one line of code!

Use the image downloader extension

➤ Switch to SearchResultCell.swift and add a new instance variable, downloadTask, to hold a reference to the image downloader:

Mye uvt len bepzyeixv tsu exwaj itbfotn
Yvu ucb rum hutzneucg wro anjox eynbiqq

App transport security

While your image downloading experience worked great here, sometimes when dealing with image downloads, or accessing any web URL for that matter, you might see something like the following in the Xcode Console, along with a ton of error messages like the following for failed download tasks:

Ogimtosupf Ikn Kvepfjidq Qakaqolg
Ecoyyepoxl Edz Sbawkfijw Zeqogapz

Cancel previous image downloads

These images already look pretty sweet, but you’re not quite done yet. Remember that table view cells can be reused, so it’s theoretically possible that you’re scrolling through the table and some cell is about to be reused while its previous image is still downloading.

Caching

Depending on what you searched for, you may have noticed that many of the images were the same. For example, you might get many identical album covers in the search results. URLSession is smart enough not to download identical images — or at least images with identical URLs — twice. That principle is called caching and it’s very important on mobile devices.

Merge the branch

This concludes the section on talking to the web service and downloading images. Later on, you’ll tweak the web service requests a bit more to include the user’s language and country, but for now, you’re done with this feature. I hope you got a good glimpse of what is possible with web services and how easy it is to build this functionality into your apps using URLSession.

Merge the branch using Xcode

Now that you’ve completed a feature, you can merge this temporary branch back into the main branch.

Seclarx xuup ttagbos qurd po bhu qeex xmulxg
Vohrard neoq xcivduq rayv be yli xiep klevpn

Xzo biysuxsuhuok veeray yofovi cavgivh bvefhow
Xni mebkamlehoab houkah kicaxi bucmunq xqavvey

Merge the branch from the command line

The source control features in Xcode used to be a bit rough around the edges. So, it was possible that certain commands, especially merging changes, might not work correctly. Or Xcode might crash. If Xcode didn’t want to cooperate when you tried to merge changes, here is how you’d do it from the command line.

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