iOS 6 Application Development For Dummies (2013)

Introduction

iOS 6 Application Development For Dummies is a beginner’s guide to developing iOS applications. And not only do you not need any iOS development experience to get started, but you also don’t need any Macintosh development experience, either. We’ve written this book as though you are coming to iPhone and iPad application development as a blank slate, ready to be filled with useful information and new ways to do things. Well, almost a blank slate, anyway; see the upcoming “Foolish Assumptions” section for details on what you need to know before using this book.

Because of the nature of the iPhone and iPad, you can create small, bite-sized applications that can be quite powerful. Also, because you can start small and create real applications that do something important for a user, it’s relatively easy to transform yourself from an “I know nothing” person into a developer who, though not (yet) a superstar, can still crank out quite a respectable application.

But the iPhone and iPad can be home to some pretty fancy software as well — so we’ll take you on a journey through building an industrial-strength application and show you the ropes for developing one on your own.

A Bit of History

It’s 6:00 a.m. PST on January 9, 2007. One of us (Dave) is walking through a cold mist to join the end of a distressingly long line of nerds wrapped all the way around San Francisco’s Moscone Center. Why? To hear Steve Jobs give his MacWorld Expo keynote address. It was nuts to get up so early on a cold morning, but Steve Jobs was rumored to be introducing an Apple phone.

No one knew whether an Apple phone would be any good, but perhaps Steve would show us magic — something that would revolutionize an industry. Perhaps it would be as cool and important as the iPod! Probably not, but I wanted to be there, just in case.

A few hours later, Steve told us that “Apple is going to reinvent the phone.” Steve was never modest, but he was certainly correct — Apple completely blew away the phone industry that day. The damage was not yet visible to the current phone vendors (Palm, Motorola, Nokia, Sony, Ericsson, RIM, and Microsoft), but they were suddenly left back in the twentieth century. The future had arrived.

The iPhone Stands Alone

The first iPhone shipped in late June 2007. It came with a bunch of Apple’s native apps such as Contacts, Maps, Stocks, Safari, and so on. The problem was that only Apple could develop these native apps. The Apple developer “evangelists” told us developers that we should be happy writing web apps. This did not make us happy — we wanted to write native Objective-C apps.

Steve Jobs and Apple eventually saw the light. Apple released a beta version of the iPhone Software Development Kit (SDK) in the spring of 2008, and it opened the App Store for business in July 2008. At this point, you could develop apps — but only for the iPhone, because the iPad did not yet exist.

The iPad Joins the Party

Apple released the first iPad in April 2010, so now you could write apps for either the iPhone or the iPad.

Many developers then developed two different versions of each application — one for the iPhone and a different one (often called the “HD” version back in 2010) optimized for the larger iPad screen. The problem was that you would then have two separate Xcode projects to maintain. Some of the project code would be shared between the iPhone and iPad versions, but many of the files would be slightly different. Code maintenance became a nightmare.

It gradually became clear that it would be better to have a single Xcode project that could be used to build a universal app — a single app that runs with the iPhone user interface on the iPhone, but uses a different iPad user interface on the iPad.

A Universal App

“So,” you say, “What are the benefits of merging the iPhone and iPad apps into a single universal app?” The benefits include the following:

image You can add a new feature in the one Xcode project that can appear in both the iPhone and iPad versions.

image You can fix a bug in one project rather than two.

image You only have to make changes for a new version of iOS in one project.

image You only have to make changes for new versions of Objective-C or the compiler in one place.

Serial or Parallel Development?

This book shows you how to build a universal version of our RoadTrip project. The compiled native app will run on the iPhone with a user interface designed for the iPhone. The same app will run on the iPad with a different user interface, optimized for the iPad’s larger screen. The obvious question involves the order in which the different versions are developed. Your choices are diagrammed in the following figure, where time flows to the right. (Einstein’s special theory of relativity proved that time always flows to the right, unless you are in the southern hemisphere.)

image

Strategies you can use include the following:

A. (Serial) Develop and test the iPhone version first, and later add the iPad version to the universal Xcode project.

B. (Serial) Develop and test the iPad version first, and later add the iPhone version to the universal Xcode project.

C. (Parallel) Incrementally develop both versions, more or less in parallel. Get the basic iPhone and iPad versions each working. Then add features to each version until time runs out. This is the approach we recommend, and the approach we use in this book.

Our Plan for This Book

You will build this book’s RoadTrip app using the following steps:

1. Create the initial storyboards for both the iPad and iPhone versions, starting with Xcode’s Master-Detail project template.

The template’s iPad storyboard is based on using UIKit’s UISplitViewController, which uses the same custom MasterViewController and custom DetailViewController used in the iPhone version. The Master View controller will appear on the left when the iPad is held in landscape orientation, while the Detail View controller appears on the right.

2. Build and test the iPad version in the iPad simulator. You should see a Table view embedded in a Navigation controller in the Master view.

The template’s initial iPhone storyboard design begins with a custom MasterViewController (a Table view) embedded in a Navigation view. Selecting an item in the Table view displays data managed by a custom DetailViewController.

3. Build and test the iPhone version in the iPhone simulator. It should also work perfectly, because you haven’t had a chance to make any mistakes yet.

The opening screens of the iPhone and iPad versions are compared in the following figure, as created by the Master-Detail template.

4. Add a TestDriveController to the iPhone storyboard. Build and test. Add it to the iPad storyboard. Build and test.

5. Add animation and sound to the Test Drive controller. Build and test both (BATB) the iPhone and iPad versions.

6. Add additional features to each version until done.

image

The final screens of the iPhone and iPad versions of RoadTrip are compared in the following figure.

image

iOS and Xcode Game Changers

With iOS 6 and — more importantly — with Xcode 4.5 (and later versions), the nuts and bolts of iOS application development have changed dramatically. Xcode 4.5 has added much more functionality to the Integrated Development Environment (IDE) with which you develop iOS applications, especially when it comes to writing syntactically correct (and bug-free) code that’s better able to manage memory. Storyboards, one of our all-time favorite new features, allow you to develop applications in a new way, with much less code and a better idea of what the flow of the application (and subsequent user experience) will be. Of course, for all this new functionality, you pay a price: more complexity. But after you get the hang of working with Xcode 4.5, writing applications becomes much easier than it has ever been.

Of course, the rub is getting the hang of Xcode 4.5. That’s where we come in. We carefully take you through Xcode 4.5, pointing out its features and describing how to best use them. When you set this book aside, you’ll have a great understanding of how to take advantage of all those features that will make your life easier.

You find out how to develop a single application that includes features that readers and students of earlier books have been asking for — including more animation and sound — as well as an infrastructure that people can use to develop more robust applications. The resulting example is an app we call RoadTrip, which can send you on your way to developing apps that you can be proud of and that other people will want to have.

This new edition is based on iOS 6 and Xcode 4.5. If you want to find out how to develop applications, the tools discussed in this book are the tools you absolutely need to use to do it the right way.

But what’s more, storyboards all of a sudden make it easier to develop universal applications — applications that run with equal aplomb on the iPad or iPhone — using the same code base!

This is great stuff!

About This Book

iOS 6 Application Development For Dummies distills the hundreds (or even thousands) of pages of Apple documentation (not to mention our own development experience) into only what’s necessary to start you developing real applications. But this is no recipe book that leaves it up to you to put it all together. Rather, it takes you through the frameworks (the code supplied in the SDK) and iOS architecture in a way that gives you a solid foundation in how applications really work, and also acts as a road map to expand your knowledge as you need to.

We assume that you’re in this for the long haul and that you want to master the whole application-development ball of wax. We use real-world applications to show the concepts and give you the background on how things actually work on iOS — the in-depth knowledge you need to go beyond the simple “Hello World” apps and create those killer iOS applications. So be prepared! You may find some places where you might want to say, “Get on with it,” but — based on our experience (including more than ten apps in the App Store, 12 books (and counting), and untold hours expended on in-person classes and technical talks — you’ll find what you need to move from following recipes in a cookbook by rote to modifying and even creating your own recipes.

It’s a multicourse banquet, intended to make you feel satisfied (and really full) at the end.

Conventions Used in This Book

This book guides you through the process of building iOS applications. Throughout the book, you use the classes provided by Apple’s iOS frameworks (and create new classes of your own, of course). You code them using the Objective-C programming language.

Code examples in this book appear in a monospaced font so that they stand out a bit better. That means that the code you see will look like this:

#import <UIKit/ UIKit.h>

Objective-C is based on C, which is case sensitive, so please enter the code that appears in this book exactly as it appears in the text. We also use the standard Objective-C naming conventions — for example, class names always start with a capital letter, and the names of methods and instance variables always start with a lowercase letter.

Note that all URLs in this book appear in a monospaced font as well, like this:

www.nealgoldstein.com

When we ask you to add code to a program, it will be in bold like this:

#import <UIKit/ UIKit.h>

You’ll notice — starting around Chapter 8 — that you will sometimes be asked to delete some of the code you have in place for your project to make room for some new stuff. When that happens, we’ll comment out the code (to make things really clear). We refer to code we want you to delete as commented out, bold, underline, and italic code, because said code will show up as commented out, bold, underlined, and italic. Simple enough, as shown in the following example:

// Delete this

If you’re ever uncertain about anything in the code, you can always look at the source code on Neal’s website at www.nealgoldstein.com or the For Dummies website at www.dummies.com/go/ios6applicationdevelopment. From time to time, we’ll provide updates for the code there and post other things you might find useful.

Foolish Assumptions

To begin programming your iOS applications, you need an Intel-based Macintosh computer with the latest version of OS X on it. (No, you can’t develop iOS applications directly on the iPhone or iPad.) You also need to download the iOS Software Development Kit (SDK) — which is free. And, oh yeah, you need an iPhone and/or iPad. You won’t start running your application on it right away — you’ll use the iOS Simulator that Apple provides with the iOS SDK during the initial stages of development — but at some point, you’ll want to test your application on a real, live iOS device.

This book assumes that you have some programming knowledge and that you have at least a passing acquaintance with object-oriented programming, using some variant of the C language (such as C++, C#, or maybe even Objective-C). In case you don’t, we point out some resources that can help you get up to speed. The application example in this book is based on the frameworks that come with the SDK; the code is pretty simple (usually) and straightforward. (We don’t use this book as a platform to dazzle you with fancy coding techniques.)

We also assume that you’re familiar with the iPhone and iPad themselves and that you’ve at least explored Apple’s included applications to get a good working sense of an iOS app’s look and feel. It might also help to browse the App Store to see the kinds of applications available there and maybe even download a few free ones (as if we could stop you).

How This Book Is Organized

iOS 6 Application Development For Dummies has six main parts, which are described in the following sections.

Part I: Getting Started

Part I introduces you to the iOS world. You find out what makes a great iOS application and see how an iOS application is structured. In Chapter 2, we give an overview of how Xcode 4.5 works that gets you up to speed on all its features; you can use this chapter as a reference and return to it as needed. You also create your Xcode project in this part — a universal application that can run equally well on an iPad or iPhone — and we take you on a guided tour of what makes up the Xcode project that will become your home away from home.

Part II: Building RoadTrip

In this part of the book, you find out how to create the kind of user interface that will capture someone’s imagination. We explain the Interface Builder editor, which is much more than your run-of-the-mill program for building graphical user interfaces. You also discover storyboards, which are the icing on the Interface Builder cake that let you lay out the entire user experience and application flow — saving you a lot of coding, to boot.

We also take you on a brief tour of the RoadTrip app, the app that you build along with us in this book. We show you not only what the app can do but also how it uses the frameworks and SDK to do that.

We also explain how the main components of an iOS application go together. We describe how the iOS applications work from a viewpoint of classes and design patterns, as well as show how the app works at runtime. We spend some time on three very important ideas: how to extend the framework classes to do what you want them to, how to manage memory, and how to take advantage of declared properties. We also explain how everything works together at runtime, which should give you a real feel for how an iOS application works.

Parts I and II give you the fundamental background that you need to develop iOS applications.

Part III: Getting Your Feet Wet — Basic Functionality

Now that you have the foundation in place, Part III starts you on the process of having your application actually do something. You start off by determining whether a network is available to support the application functionality that requires Internet access. You find out how to customize the appearance of the controls provided by the framework to make your application a thing of beauty. You finish off by adding animation and sound just to get going. You also see how to connect the elements on your storyboard to your application code to make them do things — such as have a ’59 pink Cadillac Eldorado Biarritz convertible drive up and down the screen.

Part IV: The Model and Application Structure

Now you begin to get down to the real work. You find out about the iPad’s popovers and Split View controllers, and you also add navigation to the application. Along the way, we really get into showing you how to account for the differences between an iPad and an iPhone, and make sure that the app can run flawlessly on whatever device the user has handy. You also add the application model, which provides both the data and the logic you need to create an application that delivers real value to the user. Finally, you finish the storyboard so that you can see your basic application flow.

Part V: Adding the Application Content

Now that you have the application foundation and the user experience architecture in place, Part V takes you into the world of applications that contain major functionality. We show you how to display the weather using a web page right off the Internet, how to allow the user to page through local events as if he were reading a book, how to display a map of where the user is going and where he is right now, how to find a location that he has always wanted to visit and display it on a map, and even how to change where he is going (limited in the RoadTrip app to New York and San Francisco, but it’s incredibly easy to add other destinations). We don’t go slogging through every detail of every detail, but we demonstrate almost all the technology you need to master if you intend to create a compelling application like this on your own.

Part VI: The Part of Tens

Part VI consists of some tips to help you avoid having to discover everything the hard way. It talks about approaching application development in an “adult” way right from the beginning (without taking the fun out of it). We also revisit the app and explain what else you would need to do to make this app a commercial and critical success.

Icons Used in This Book

imageThis icon indicates a useful pointer that you shouldn’t skip.

imageThis icon represents a friendly reminder. It describes a vital point that you should keep in mind while proceeding through a particular section of the chapter.

imageThis icon signifies that the accompanying explanation may be informative (dare we say interesting?), but it isn’t essential to understanding iPad application development. Feel free to skip past these tidbits if you like (though skipping while learning may be tricky).

imageThis icon alerts you to potential problems that you may encounter along the way. Read and obey these blurbs to avoid trouble.

imageThis icon indicates how to use an important part of Xcode functionality. This helps you wade through Xcode’s complexity and focus on how to get specific things done.