Browse by

iOS App Programming Guide

Contents

About iOS App Programming 8
At a Glance 9 Translate Your Initial Idea into an Implementation Plan 9 UIKit Provides the Core of Your App 9 Apps Must Behave Differently in the Foreground and Background 9 iCloud Affects the Design of Your Data Model and UI Layers 10 Apps Require Some Specific Resources 10 Many App Behaviors Can Be Customized 10 Apps Must Be Tuned for Performance 10 The iOS Environment Affects Many App Behaviors 11 How to Use This Document 11 Prerequisites 11 See Also 11

The argc and argv parameters contain any launch-time arguments passed to the app from the system. These arguments are parsed by the UIKit infrastructure and can otherwise be ignored. The third parameter identifies the name of the principal app class. This is the class responsible for running the app. It is recommend that you specify nil for this parameter, which causes UIKit to use the UIApplication class. The fourth parameter identifies the class of your custom app delegate. Your app delegate is responsible for managing the high-level interactions between the system and your code. The Xcode template projects set this parameter to an appropriate value automatically.

●

●

Another thing the UIApplicationMain function does is load the app’s main user interface file. The main interface file contains the initial view-related objects you plan to display in your app’s user interface. For apps that use storyboards, this function loads the initial view controller from your storyboard and installs it in the window provided by your app delegate. For apps that use nib files, the function loads the nib file contents into memory but does not install them in your app’s window. You must install them yourself in the application:didFinishLaunchingWithOptions: method of your app delegate. An app can have either a main storyboard file or a main nib file but cannot have both. Storyboards, the preferred way to specify your app’s user interface, are not supported on all versions of iOS. Specify the name of your main storyboard file in the UIMainStoryboardFile key of your app’s Info.plist file. (For nib-based apps, specify the name of your main nib file using the NSMainNibFile key instead.) Normally, Xcode sets the value of this key when you create your project, but you can change it later if needed. For more information about the Info.plist file and how you use it to configure your app, see Figure 6-1 (page 114).

What to Do at Launch Time
When your app is launched (either into the foreground or background), use your app delegate’s application:didFinishLaunchingWithOptions: method to do the following:
●

Check the contents of the launch options dictionary for information about why the app was launched, and respond appropriately. Initialize the app’s critical data structures. Prepare your app’s window and views for display. Apps that use OpenGL ES should not use this method to prepare their drawing environment. Instead, they should defer any OpenGL ES drawing calls to the applicationDidBecomeActive: method.

●

●

●

Use any saved preferences or state information to restore the app to its previous runtime state.

If your app uses nib files to manage its views, you should use the application:didFinishLaunchingWithOptions: method to prepare your app’s window for display. For nib-based apps, you must create your window object, install the views from your initial view controller, and show the window. For apps that support both portrait and landscape orientations, always set up your window in a portrait orientation. If the device is in a different orientation at launch time, the system automatically rotates your views to the appropriate orientation before displaying the window. Your application:didFinishLaunchingWithOptions: method should always be as lightweight as possible to reduce your app’s launch time. Apps are expected to launch and initialize themselves and start handling events in roughly 5 seconds. If an app does not finish its launch cycle in a timely manner, the system kills it for being unresponsive. Thus, any tasks that might slow down your launch (such as accessing the network) should be executed asynchronously on a secondary thread. When launching into the foreground, the system also calls the applicationDidBecomeActive: method to finish the transition to the foreground. Because this method is called both at launch time and when transitioning from the background, use it to perform any tasks that are common to the two transitions. When launching into the background, there should not be much for your app to do except get ready to handle whatever event arrived.

Responding to Interruptions
When an alert-based interruption occurs, such as an incoming phone call, the app moves temporarily to the inactive state so that the system can prompt the user about how to proceed. The app remains in this state until the user dismiss the alert. At this point, the app either returns to the active state or moves to the background state. Figure 3-4 shows the flow of events through your app when an alert-based interruption occurs.
Figure 3-4
Foreground

Handling alert-based interruptions
Your code

Event Loop

A phone call arrives or an alert-based interruption occurs

applicationWillResignActive:

Ignore?
No

Yes

applicationDidBecomeActive:

Switch to a different app

In iOS 5, notifications that display a banner do not deactivate your app in the way that alert-based notifications do. Instead, the banner is laid along the top edge of your app window and your app continues receive touch events as before. However, if the user pulls down the banner to reveal the notification center, your app moves to the inactive state just as if an alert-based interruption had occurred. Your app remains in the inactive state until the user dismisses the notification center or launches another app. At this point, your app moves to the appropriate active or background state. The user can use the Settings app to configure which notifications display a banner and which display an alert. Pressing the Sleep/Wake button is another type of interruption that causes your app to be deactivated temporarily. When the user presses this button, the system disables touch events, moves the app to the background but sets the value of the app’s applicationState property to UIApplicationStateInactive (as opposed to UIApplicationStateBackground), and finally locks the screen. A locked screen has additional consequences for apps that use data protection to encrypt files. Those consequences are described in “What to Do When an Interruption Occurs” (page 45).

Determining the Transfer Status of a File or Directory
Items you write to an iCloud container directory are transferred automatically to the iCloud server as quickly as possible. However, depending on the network and the type of device, a file might not be uploaded to the server or downloaded to a device immediately. In cases where you need to know the state of a file, you can use the getResourceValue:forKey:error: method of NSURL to retrieve the value for one of the following attributes:
NSURLIsUbiquitousItemKey—Indicates whether or not the item is stored in iCloud. NSURLUbiquitousItemIsDownloadedKey—Indicates whether the current version of the item is

downloaded and accessible.
NSURLUbiquitousItemIsDownloadingKey—Indicates whether the current version of the item is being

downloaded and is not yet available.
NSURLUbiquitousItemPercentDownloadedKey—For an item being downloaded, indicates what

percentage of the changes have already been downloaded. You can use this value to update progress bars.
NSURLUbiquitousItemIsUploadedKey—Indicates that locally made changes were successfully uploaded

to the iCloud server.
NSURLUbiquitousItemIsUploadingKey—Indicates that locally made changes are being uploaded to

the iCloud server now.
NSURLUbiquitousItemPercentUploadedKey—For an item being uploaded, indicates what percentage

of the changes have already been uploaded to the server. Although the iCloud server aggressively pulls changes your app makes locally, iOS devices typically do not pull changes from the server until you try to access the file. If you try to open a file that is currently being downloaded, iOS blocks the thread that issued the open request until the file is downloaded and available for use. Thus, if you are concerned about potential delays, check the file’s current state as needed and possibly update your user interface to reflect that the file is not yet available or is currently downloading. For more information about the attributes you can request for URLs, see NSURL Class Reference .

Working With Files That Are Not Yet Downloaded
When a change occurs to a file in iCloud, iOS devices do not automatically download the data associated with that change. Instead, iOS devices download the metadata for the file so that they know that a change exists. The actual data for the file is not downloaded until one of the following happens:
●

Your app attempts to open or access the file. Your app calls the startDownloadingUbiquitousItemAtURL:error: method to download the changes explicitly.

If your app opens a file that is not yet downloaded, the file coordinator used to open the file blocks your app until the file or its changes have been downloaded. Depending on the size of the changes, this might not lead to the best user experience, so it is preferable to check the download status of a file before trying to open it. The NSURL class defines properties related to iCloud items, including whether the file is stored in iCloud and whether it is currently downloaded. To obtain the value for one of these keys, use the getResourceValue:forKey:error: method of NSURL. For example, to determine whether a file was downloaded, you could use code similar to the following:
- (BOOL)downloadFileIfNotAvailable:(NSURL*)file { NSNumber* isIniCloud = nil;

if ([file getResourceValue:&isIniCloud forKey:NSURLIsUbiquitousItemKey error:nil]) { // If the item is in iCloud, see if it is downloaded. if ([isIniCloud boolValue]) { NSNumber* isDownloaded = nil;

Updating Your User Interface for iCloud
Any user interface changes you make related to iCloud should be as unobtrusive as possible to the user. The documents you store in iCloud are the same ones you store locally when iCloud is not available. The only difference is their location in the file system. So the bulk of your user interface should look about the same. Sometimes, though, you might want to modify your user interface for iCloud. Modify your UI:
●

When a user-generated document must be downloaded before it can be used. Giving the user control over whether to download a document is needed only if your app presents some sort of document browser. For files your app manages privately, download them automatically if they are not available. Any indicators you use should be subtle and provide the user with the option to begin downloading the document. If a download might take more than a few seconds, you might also want to display the current download progress. When there is a version conflict that the user must resolve. Version conflicts can occur when the same document is modified on two different devices at the same time. (This can occur if one of the devices was not connected to the network when the changes were made.) If your app needs user assistance to resolve the conflict, present a subtle indicator that this is the case. Do not display an alert or any sort of disruptive interface to notify the user that a conflict exists. When you want to give the user the option to enable or disable iCloud usage entirely for your app. If your app includes a Settings bundle or inline preferences, you could include a preference to toggle whether your app stores content in iCloud at all. For example, an app whose data consists entirely of privately managed files might do this to give the user the choice of how those files are stored.

●

●

For tips and guidance about how to design your app’s user interface, see iOS Human Interface Guidelines .

Using iCloud in Conjunction with Databases
Using iCloud with a SQLite database is possible only if your app uses Core Data to manage that database. Accessing live database files in iCloud using the SQLite interfaces is not supported and will likely corrupt your database. However, you can create a Core Data store based on SQLite as long as you follow a few extra steps when setting up your Core Data structures. You can also continue to use other types of Core Data stores—that is, stores not based on SQLite—without any special modifications. When using Core Data with a SQLite store, the actual database file is never transferred to the iCloud sever. Instead, each device maintains its own copy of the SQLite store and synchronizes its contents by writing out changes to log files. It is the log files that are then transferred to and from iCloud and the other devices. On each device, Core Data takes the contents of the log files and uses them to update its local database. The result is that each local database ends up with the exact same set of changes.

Setting up your Core Data store to handle iCloud requires only a little extra effort on your part. The steps you must follow depend on whether you are using a single Core Data store as a central library for your app or whether you are creating separate stores for individual documents. The following sections assume that you are using a SQLite store to manage your data. SQLite stores are intended for apps that have large amounts of data to manage or want fine-grained change notifications. You do not need to read these sections if you are creating an atomic binary store. Important For the latest information about using Core Data with iCloud, see Using Core Data with iCloud Release Notes .

Using Core Data to Manage Documents
For apps that manage Core Data stores as individual documents, use instances of the UIManagedDocument class to manage individual documents. The UIManagedDocument class automatically looks for any managed object models in your application bundle and uses them as the basis for your document data. (You can also override the managedObjectModel property of the class to customize the object models for a given subclass.) Because most of your data is handled by a managed object context, this means that you can often use the UIManagedDocument class without subclassing. Behaviors such as saving are handled automatically thanks to the inherited autosave behavior provided for all documents. When creating new documents, do the following:
1. 2.

Create your instance of the UIManagedDocument class. Add the NSPersistentStoreUbiquitousContentNameKey key to the dictionary in your document’s persistentStoreOptions property. The value of this key is a unique name that your app can use to identify the document. Add some initial data to the document. Save the document to disk using the saveToURL:forSaveOperation:completionHandler: method. When saving a document, you can either save it directly to iCloud or you can save it to a local directory and move it to iCloud later. To save the document directly to iCloud, specify a URL that is based on a location returned by the URLForUbiquityContainerIdentifier: method. If you save the document locally, you can move it to iCloud later using the setUbiquitous:itemAtURL:destinationURL:error: method.

3. 4.

When you create a new document, Core Data creates a file package containing the document contents. Among these contents are a DocumentMetadata.plist file and a directory containing the SQLite data store. Everything in the file package is transferred to the iCloud server except for the SQLite data store, which remains local to the device.

When opening existing documents that reside in iCloud, do the following:
1.

Use an NSMetadataQuery object to search for documents in iCloud. Metadata queries identify all of your Core Data documents, regardless of whether they were created locally or on another device. For documents created on other devices, the only thing present in the document’s file package initially is the DocumentMetadata.plist file.

2.

Open the DocumentMetadata.plist file and retrieve the value of the NSPersistentStoreUbiquitousContentNameKey key. Create your instance of the UIManagedDocument class. Add the NSPersistentStoreUbiquitousContentNameKey key to the dictionary in your document’s persistentStoreOptions property. The value of this key should match the value you retrieve from the DocumentMetadata.plist file. Call the openWithCompletionHandler: method of the document to open it.

3. 4.

5.

The first time your app opens a Core Data document that was created on another device, Core Data automatically detects the absence of the SQLite store and creates it locally. It then uses the value of the NSPersistentStoreUbiquitousContentNameKey key (that you added to the document’s NSPersistentStoreUbiquitousContentNameKey property) to retrieve the appropriate transaction logs and rebuild the contents of the database. From that point on, you can make changes to the document and save them back to iCloud. The changes you make are stored in a new log file so that they can be incorporated into the SQLite stores on other devices. When changes for a document are received from iCloud, Core Data automatically folds them into that document’s SQLite store and sends your app a NSPersistentStoreDidImportUbiquitousContentChangesNotification notification. Apps should always register for this notification and use it to refresh any affected records. If your app does not refresh its local copy of the data, it could save old changes back out to iCloud and create a conflict that would need to be resolved. By incorporating changes when they arrive, your app should be able to avoid such conflicts. When you want to delete a document, you must delete both the file package for the document and the directory containing the document’s transaction logs. Deleting both of these items requires you to perform a coordinated write operation using an NSFileCoordinator object. The DocumentMetadata.plist file of your document contains a NSPersistentStoreUbiquitousContentURLKey key with the URL of the transaction logs directory for your document. For more information on using file coordinators, see File System Programming Guide . For information on how to use Core Data stores to manage the objects in your app, see Core Data Programming Guide .