iOS Apps with REST APIs

iOS Apps with REST APIs

Building Web-Driven Apps in Swift

Making your first Swift app that uses a web service can be overwhelming. It seems like every time you try to figure it out you just add more things to learn to your list: REST, Alamofire, parsing JSON, OAuth, App Transport Security, setting headers, … But it doesn’t have to be confusing.

About the Book

Skip the hundreds of pages that barely get into how to make a network call in iOS. No esoteric details about Core Anything or mathematical proofs of flatmap. Only the nitty gritty that you need to get real work done now: interfacing with your web services and displaying the results in your UI.

This book was written using Swift 4, Alamofire 4.7, Xcode 9, and iOS 11 (with support for iOS 10).

All code samples included.

After reading this book you’ll be able to:

- Analyze a JSON response from a web service call and write Swift code to parse it into model objects.

- Display those model objects in a table view so that when the user launches the app they have a nice list to scroll through.

- Add authentication to use web service calls that require OAuth 2.0, a username/password, or a token.

- Transition from the main table view to a detail view for each object, possibly making another web service call to get more info about the object.

Share this book

Feedback

About the Author

Christina Moulton has been developing iOS apps for 5 years and still randomly types retain]; once in a while. She develops custom iPhone & iPad apps at Teak Mobile Inc in Ontario, Canada. She enjoys every part of the process: starting with initial concepts then designing the interface, writing code, and submitting apps to the App Store (well, not really the submission part, iTunesConnect is awful). She's currently off sailing for a few years.

Christina holds B.A.Sc. and M.A.Sc. degrees in Systems Design Engineering from the University of Waterloo. She writes Swift developer tutorials at GrokSwift.com as often as she can.

Packages

The Book

Get your app built faster. The TL;DR Edition will help guide you through the book to find the parts you need and get your app started. Then once you're on your way, the Adding More API Calls Reference Sheet will help you through all of the necessary steps each time you find yourself integrating yet another REST API call in your Swift app.

Includes:

extras

TL;DR Edition (PDF)

extras

Adding More API Calls Reference Sheet (PDF)

English

PDF

EPUB

MOBI

APP

$39.00

Minimum price

$39.00

Suggested price

Team License

A team license for iOS Apps with REST APIs gives you freedom to distribute the book to your team members working for your company or collaborating on products with you - anyone working with your or for you on iOS apps. Includes the TL;DR Edition and Adding More API Calls Reference Sheet.

Includes:

extras

TL;DR Edition (PDF)

extras

Adding More API Calls Reference Sheet (PDF)

extras

Team License

English

PDF

EPUB

MOBI

APP

$149.00

Minimum price

$149.00

Suggested price

Bundles that include this book

Reader Testimonials

Mark Johnson

Sr. Architect Apple Products at Proscape Technologies

I had to comment on how you progressed from simple NSURLSession calls to Alamofire serializers. I really like how you explained why you would choose to use one vs. the other and how some libraries can dramatically simplify the code.

Igor Shpak

Director, North American Regions Applications at Estee Lauder Companies

I have always been a Microsoft developer and only recently gotten into iPhone development. Your book is exactly what myself and my team needed to get started. Every example was relevant to what we needed for the application we are developing.

Dave LaPorte

Senior Developer at Volm Companies Inc

Your book made me a rockstar in my company :) I can't thank you enough!

Table of Contents

Thanks

1. From JSON API to Swift App

1.1 What Will You Be Able to Do?

1.2 Who Is This Book For?

1.3 Who Is This Book Not For?

1.4 Using This Book

1.5 What We Mean By Web Services / APIs / REST / CRUD

1.6 What about GraphQL?

1.7 JSON

1.8 Versions

1.9 Source Code

1.10 Disclaimer

1.11 Trademarks

2. Our App’s Requirements

2.1 Match Tasks to Endpoints

2.2 User Interface

2.3 API Requirements

2.4 Make a Plan

3. Swift JSON Parsing & Networking Calls 101

3.1 REST API Calls with URLSession

3.2 REST API Calls with Alamofire

4. Using a Router to Organize Alamofire Calls

4.1 Generating the URL Requests

4.2 Strongly Typed GET and POST Calls with Alamofire

4.3 And That’s All

5. Parsing JSON with Codable

5.1 Converting a Single Item to JSON

5.2 Convert an Array of Items to JSON

5.3 Generating JSON from an Item

5.4 Nested JSON Objects

5.5 What if the JSON Doesn’t Match My Struct / Object?

5.6 Some Warnings

5.7 And That’s Codable

5.8 And That’s All

6. Why I Use Libraries Like Alamofire

7. Hooking Up a REST API to a Table View

7.1 Our Swift Project

7.2 Analyzing the API JSON Response

7.3 Setting Up the Table View

7.4 Getting & Processing the API Response

7.5 And That’s All

8. Custom HTTP Headers

8.1 Per Request Headers

8.2 Session Custom Headers

8.3 Headers in URLRequestConvertible

8.4 Request Adapter

8.5 And That’s All For Headers

9. Loading UITableViewCell Images from an API

9.1 Loading Images from URLs

9.2 UITableViewCell Images from URLs

9.3 Enhancements

9.4 Caching Images

9.5 A Better Cache: PINRemoteImage

9.6 And That’s All

10. Pagination, a.k.a., Load More on Scroll

10.1 Where is the Next Page?

10.2 Fetching and Appending

10.3 Integrating with the View Controller

10.4 When to Load More Gists?

10.5 And That’s All for Pagination

11. Pull to Refresh

11.1 Adding Pull to Refresh

11.2 Showing the Last Refreshed Time

11.3 And That’s All

12. Authentication: Basic and HTTP Headers

12.1 The Docs

12.2 Basic Auth: Username/Password

12.3 HTTP Header Authentication

12.4 Alamofire Validation

12.5 And That’s All

13. Authentication: Implementing the OAuth 2.0 Flow

13.1 Get the OAuth Token

13.2 Login View

13.3 The OAuth Login Flow

13.4 Using the OAuth Token for API Calls

13.5 Storing the OAuth Token Securely

13.6 Making Authenticated Calls

13.7 And That’s the Login Flow for OAuth 2.0

14. Integrating OAuth with the UI

14.1 Error Handling During OAuth Login

14.2 Refresh Tokens

14.3 Unauthorized Responses: 404 vs 401

14.4 And That’s All

15. Switching Lists

15.1 Setting Up the UI

15.2 Sharing a Completion Handler

15.3 And That’s All

16. Switching Between View Controllers and More JSON Parsing

16.1 JSON Parsing: Dates

16.2 JSON Parsing: Dictionaries & Arrays

16.3 Configuring the Detail View Controller

16.4 Passing Data in a Segue

16.5 Adding a Table View

16.6 Displaying Gist File Content

16.7 And That’s All

17. Adding More API Calls - Starring

17.1 Is the Gist Starred?

17.2 Starred Status in the Table View

17.3 PUT and DELETE Calls to Star and Unstar Gists

17.4 Authorization Check

17.5 And That’s All

18. Deleting Gists

18.1 DELETE API Call

18.2 User Interface: Table View Delete Features

18.3 And That’s All

19. Creating Gists and Clearing the Cache

19.1 POST API Call with Nested JSON Parameters

19.2 Creating an Input Form with Validation

19.3 And That’s All

20. What if They’re Offline?

20.1 How Do We Know?

20.2 Verifying Offline Behavior

20.3 OAuth Offline

20.4 Save a Local Copy

20.5 Databases

21. What Next?

21.1 User Interface

21.2 Test the User Experience

21.3 Suggested Exercises

21.4 Did I Miss Anything?

A Brief Introduction to CocoaPods

Adding a CocoaPod to a Project

What Does the Podfile Mean?

Dependencies

CocoaPods Version Numbers

Updating CocoaPods

The Leanpub 45-day 100% Happiness Guarantee

Within 45 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Free Updates. Free App. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers), EPUB (for phones and tablets), MOBI (for Kindle) and in the free Leanpub App (for Mac, Windows, iOS and Android). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Write and Publish on Leanpub

Authors, publishers and universities use Leanpub to publish amazing in-progress and completed books and courses, just like this one. You can use Leanpub to write, publish and sell your book or course as well! Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks. Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. It really is that easy.