Ios Apprentice Ios Apprentice Matthijs Hollemans
Total Page:16
File Type:pdf, Size:1020Kb
iOS Apprentice iOS Apprentice Matthijs Hollemans Copyright ©2016 Razeware LLC. Notice of Rights All rights reserved. No part of this book or corresponding materials (such as text, images, or source code) may be reproduced or distributed by any means without prior written permission of the copyright owner. Notice of Liability This book and all corresponding materials (such as source code) are provided on an “as is” basis, without warranty of any kind, express of implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in action of contract, tort or otherwise, arising from, out of or in connection with the software or the use of other dealing in the software. Trademarks All trademarks and registered trademarks appearing in this book are the property of their own respective owners. License By purchasing iOS Apprentice, you have the following license: • You are allowed to use and/or modify the source code in iOS Apprentice in as many apps as you want, with no attribution required. • You are allowed to use and/or modify all art, images and designs that are included in iOS Apprentice in as many apps as you want, but must include this attribution line somewhere inside your app: “Artwork/images/designs: from iOS Apprentice book, available at www.raywenderlich.com”. • The source code included in iOS Apprentice is for your personal use only. You are NOT allowed to distribute or sell the source code in iOS Apprentice without prior authorization. • This book is for your personal use only. You are NOT allowed to sell this book without prior authorization, or distribute it to friends, coworkers or students; they would need to purchase their own copies. raywenderlich.com 2 iOS Apprentice About the author Matthijs Hollemans is a mystic who lives at the top of a mountain where he spends all of his days and nights coding up awesome apps. Actually he lives below sea level in the Netherlands and is pretty down-to-earth but he does spend too much time in Xcode. Check out his website at www.matthijshollemans.com. About the cover Striped dolphins live to about 55-60 years of age, can travel in pods numbering in the thousands and can dive to depths of 700 m to feed on fish, cephalopods and crustaceans. Baby dolphins don't sleep for a full a month after they’re born. That puts two or three sleepless nights spent debugging code into perspective, doesn't it? :] raywenderlich.com 3 iOS Apprentice Table of Contents: Extended Tutorial 4: StoreSearch ............................................................. 6 The StoreSearch app .......................................................................................................... 6 In the beginning… ............................................................................................................... 8 Custom table cells and nibs ............................................................................................. 31 The debugger..................................................................................................................... 50 It’s all about the networking ............................................................................................ 58 Asynchronous networking ................................................................................................. 87 URLSession........................................................................................................................... 99 The Detail pop-up........................................................................................................... 128 Fun with landscape ......................................................................................................... 168 Refactoring the search ................................................................................................... 200 Internationalization ......................................................................................................... 222 The iPad ............................................................................................................................ 243 Distributing the app........................................................................................................ 273 The end.............................................................................................................................. 289 raywenderlich.com 5 Tutorial 4: StoreSearch 4By Matthijs Hollemans The StoreSearch app One of the most common things that mobile apps do is talking to a server on the internet. It’s beyond question: if you’re writing mobile apps, you need to know how to upload and download data. In this lesson you’ll learn how to do HTTP GET requests to a web service, how to parse JSON data, and how to download files such as images. You are going to build an app that lets you search the iTunes store. Of course, your iPhone already has apps for that (“App Store” and “iTunes Store” to name two), but what’s the harm in writing another one? Apple has made a web service available for searching the entire iTunes store and you’ll be using that to learn about networking. The finished app will look like this: The finished StoreSearch app raywenderlich.com 6 iOS Apprentice Tutorial 4: StoreSearch You will add search capability to your old friend, the table view. There is an animated pop-up with extra information when you tap an item in the table. And when you flip the iPhone over to landscape, the layout of the app completely changes to show the search results in a different way. There is also an iPad version of the app: The app on the iPad The to-do list for building StoreSearch is roughly as follows: • Create a table view (yes, again!) with a search bar. • Perform a search on the iTunes store using their web service. • Understand the response from the iTunes store and put the search results into the table view. • Each search result has an artwork image associated with it. You’ll need to download these images separately and place them in the table view as well. • Add the pop-up screen with extra info that appears when you touch an item. • When you flip to landscape, the whole user interface changes and you’ll show all of the icons in a paging scroll view. • Add support for other languages. Having your app available in languages besides English dramatically increases its audience. • Make the app universal so it runs on the iPad. This tutorial fills in the missing pieces and rounds off the knowledge you have obtained from the previous tutorials. You will also learn how to distribute your app to beta testers with so-called Ad Hoc Distribution, and how to submit it to the App Store. raywenderlich.com 7 iOS Apprentice Tutorial 4: StoreSearch There’s a lot of work ahead, so let’s get started! In the beginning… Fire up Xcode and make a new project. Choose the Single View Application template and fill in the options as follows: • Product Name: StoreSearch • Organization Name: your name • Organization Identifier: com.yourname • Language: Swift • Devices: iPhone • Use Core Data, Include Unit Tests, Include UI Tests: leave these unchecked When you save the project Xcode gives you the option to create a so-called Git repository. You’ve ignored this option thus far but now you should enable it: Creating a Git repository for the project If you don’t see this option, click the Options button in the bottom-left corner. Git and version control Git is a so-called revision control system. In short, Git allows you to make snapshots of your work so you can always go back later and see a history of what you did. Even better, a tool such as Git allows you to work on the same code with multiple people. Imagine what happens if two programmers changed the same source file at the same time. Things will go horribly wrong! It’s quite likely your changes will accidentally be overwritten by a colleague’s. I once had a job where I had to shout raywenderlich.com 8 iOS Apprentice Tutorial 4: StoreSearch down the hall to another programmer, “Are you using file X?” just so we wouldn’t be destroying each other’s work. With a revision control system such as Git, each programmer can work independently on the same files, without a fear of undoing the work of others. Git is smart enough to automatically merge all of the changes, and if there are any conflicting edits it will let you resolve them before breaking anything. Git is not the only revision control system out there but it’s the most popular one for iOS. A lot of iOS developers share their source code on GitHub (github.com), a free collaboration site that uses Git as its engine. Another popular system is Subversion, often abbreviated as SVN. Xcode has built-in support for both Git and Subversion. In this tutorial I’ll show you some of the basics of using Git. Even if you work alone and don’t have to worry about other programmers messing up your code, it still makes sense to use it. After all, you might be the one messing up your own code and with Git you’ll always have a way to go back to your old – working! – version of the code. The first screen in StoreSearch will have a table view with a search bar, so let’s make the view controller for that screen. ➤ In the project navigator, rename ViewController to SearchViewController. ➤ In SearchViewController.swift, change the class line to: class SearchViewController: UIViewController { Note that this is a plain UIViewController, not