This chapter serves as both a refresher for creating apps for iOS and a foundation for completing the basic tasks that are repeated throughout this book. The first eight recipes walk you through fundamental tasks such as setting up an application, connecting and referencing user interface elements in your code, and adding images and sound files to your project. The knowledge you will acquire from the recipes in this chapter is necessary to complete many of the tasks in the other chapters of this book.
The last four recipes in this chapter deal with select, useful tasks such as setting up simple APIs for default error and exception handling, including a "lite" version of your app in your projects, and making an app launch seem quick and easy in the eyes of the user. For the most part, these tasks wont be repeated in the remainder of this book; however, we feel they are essential knowledge for any developer.
Recipe 1-1: Setting Up a Single View Application
Many of the recipes in this book are implemented in a test application with a single view. Such a project is easy to set up in Xcode using the Single View Application template. This template enables you to use storyboards.
A storyboard is a way to build interfaces consisting of multiple scenes and their connections. Before the introduction of storyboards in iOS 5, .xib files were typically used to build the interface for individual scenes; in other words, there was one .xib file per scene. The connections between .xib files were handled in code. While you still have the ability to use .xib files, Apple is pushing developers to use storyboards instead. As such, you will be using storyboards for most of this book.
To create a new single view application, go to the main menu and select File New Project. This brings up the dialog box with available project templates (see Figure ). The template you want is located on the Application page under the iOS section. Choose Single View Application and click Next.
Figure 1-1.
The single view application template in the iOS application section
Enter a few properties for your Application recipes:
A Product Name , such as My Test App
An Organization Name , which can be your name, unless you already have an organization name
A Company Identifier , which preferably is your Internet domain, if you have one
If you like, you can also enter a class prefix that will be applied to all classes you create using the Objective-C file template. This can be a good idea if you want to avoid future name conflicts with third-party code; however, if this app is meant only for testing a feature, you can leave the class prefix item blank.
You also need to specify which device type your application is for: iPad, iPhone, or both (Universal). Choose iPhone or iPad if you are testing. You can also pick Universal, but then the template will generate more code, which you probably dont need if your only purpose is trying a new feature. Figure shows the properties you need to fill out in the project options window.
Figure 1-2.
Configuring the project
Click the Next button and then select a folder where the project will be stored. Bear in mind that Xcode creates a new folder for the project within the folder you pick, so select the root folder for your projects.
Theres often a good reason to place the project under version control . It allows you to check changes to the code so you can go back to a previous version if something goes wrong or if you simply want to see the history of changes to the application. Xcode comes with Git, a feature-rich, open-source version-control system that allows multiple developers to work on a project simultaneously with ease. To initialize it for your project, check the Create local git repository for this project checkbox, as in Figure . As of Xcode 5, you can specify a server as well as your Mac for this repository.
Figure 1-3.
Selecting the parent folder for the project
Now click the Create button. An application with an app delegate, a storyboard, and a view controller class will be generated for you (see Figure ).
The setup is now complete, and you can build and run the application (which at this point shows only a blank screen).
Figure 1-4.
A basic application with an app delegate and a view controller
At this point, you have a good foundation on which you can build for the next seven recipes.
An Alternate Way
Whether or not to use storyboards is a topic of debate among developers. Some developers love storyboards, while others dislike them. As a developer, it is likely you will work on projects that dont use storyboards or that use a mix of .xib files and storyboards. We wont argue for or against storyboards, but many developers tend to agree that storyboards can present difficulties when using version control and when multiple developers need to work on the same storyboard. With this in mind, it might be beneficial to learn the .xib approach. Learning this approach is entirely optional, so you can move on to Recipe 1-2 if you choose.
To create an empty application and add a ViewController class with an accompanying .xib file, you first need to create a new project. Choose Empty Application instead of Single View Application (refer to Figure ) and then click Next.
The next screen will look almost the same as Figure . This time, there will be a new check box called Use Core Data. Leave it cleared and click Next. Again, youll be prompted for a save location. Find a suitable location to save and click Create.
Upon creation of your new, empty application, youll see there are no ViewController.m or ViewController.h files; you will need to create those. Click the + sign in the lower-left corner of the Xcode window and select new file (see Figure ). You can also press Cmd + N.