Creating an iPhone Application
At a high level, the process for creating an iPhone application issimilar to that for creating a Mac OS X application. Both use the sametools and many of the same basic libraries. Despite the similarities,there are also significant differences. An iPhone is not a desktopcomputer; it has a different purpose and requires a very differentdesign approach. That approach needs to take advantage of the strengthsof iPhone OS and forego features that might be irrelevant orimpractical in a mobile environment. The smaller size of the iPhone andiPod touch screens also means that your application’s user interfaceshould be well organized and always focused on the information the userneeds most.iPhone OS lets users interact with iPhone and iPodtouch devices in ways that you cannot interact with desktopapplications. The Multi-Touch interface reports on each separate fingerthat touches the screen and making it possible to handle multifingergestures and other complex input easily. In addition, built-in hardwarefeatures such as the accelerometers, although present in some desktopsystems, are used more extensively in iPhone OS to track the screen’scurrent orientation and adjust your content accordingly. Understandinghow you can use these features in your applications will help you focuson a design that is right for your users.
The best way tounderstand the design of an iPhone application is to look at anexample. This article takes you on a tour of the MoveMe sample application. This sample demonstrates many of the typical behaviors of an iPhone application, including:
[*]Initializing the application
[*]Displaying a window
[*]Drawing custom content
[*]Handling touch events
[*]Performing animations
Figure 1shows the interface for this application. Touching the Welcome buttontriggers an animation that causes the button to pulse and center itselfunder your finger. As you drag your finger around the screen, thebutton follows your finger. Lift your finger from the screen and, usinganother animation, the button snaps back to its original location.Double-tapping anywhere outside the button changes the language of thebutton’s greeting.
Figure 1 The MoveMe application window
http://developer.apple.com/iphone/library/referencelibrary/GettingStarted/Creating_an_iPhone_App/Art/MoveMe.jpg
Before reading the other sections of this article, you should download the sample (MoveMe)so that you can follow along directly in the source code. You shouldalso have already read the following orientation pages in the iPhoneDev Center to get a basic understanding of iPhone OS and the tools andlanguage you use for development:
[*]iPhone OS Overview
[*]Tools for iPhone OS Development
If you are not familiar with the Objective-C programming language, you should also have read <!-- a target="_top" -->Objective-C Primer<!-- /a--> to familiarize yourself with the basic syntax of Objective-C.
Examining the MoveMe Sample Project
Downloading the MoveMesample provides you with the source code and support files needed tobuild and run the application. You manage projects for iPhone OS usingthe Xcode application (located in /Developer/Applicationsby default). Each Xcode project window combines a workspace forgathering your code and resource files, build rules for compiling yoursource and assembling your application, and tools for editing anddebugging your code.
Figure 2shows the Xcode project window for the MoveMe application. To open thisproject, copy it to your local hard drive and double-click the MoveMe.xcodeprojfile to open it. (You can also open the project from within Xcode byselecting File > Open and choosing the file.) The project includesseveral Objective-C source files (denoted by the .m extension), some image files and other resources, and a predefined target (MoveMe) for building the application bundle.
Figure 2 The MoveMe project window
http://developer.apple.com/iphone/library/referencelibrary/GettingStarted/Creating_an_iPhone_App/Art/moveme_project.jpghttp://developer.apple.com/iphone/library/referencelibrary/GettingStarted/Creating_an_iPhone_App/Art/moveme_project.jpg
In iPhone OS, the ultimate target of your Xcode project is an application bundle,which is a special type of directory that houses your application’sbinary executable and supporting resource files. Bundles in iPhone OShave a relatively flat directory structure, with most files residing atthe top level of the bundle directory. However, a bundle may alsocontain subdirectories to store localized versions of strings and otherlanguage-specific resource files. You do not need to know the exactstructure of the application bundle for the purposes of this article,but you can find that information in “The Application Bundle” in iPhone Application Programming Guide if you are interested in it.
Building the MoveMe Application
To build the MoveMe application and run it in the simulator, do the following:
[*]Open the MoveMe.xcodeproj file in Xcode.
[*]Inthe project toolbar, make sure the simulator option is selected in theActive SDK menu. (If the Active SDK menu does not appear in thetoolbar, choose Project > Set Active SDK > Simulator.)
[*]Select Build > Build and Go (Run) from the menu, or simply click the Build and Go button in the toolbar.
Whenthe application finishes building, Xcode loads it into the iPhonesimulator and launches it. Using your mouse, you can click the Welcomebutton and drag it around the screen to see the application’s behavior.If you have a device configured for development, you can also buildyour application and run it on that device. For information about howto configure devices for development and load applications, see iPhone Development Guide.
A Word About Memory Management
iPhoneOS is primarily an object-oriented system, so most of the memory youallocate is in the form of Objective-C objects. iPhone OS uses a reference counting schemeto know when it is safe to free up the memory occupied by an object.When you first create an object, it starts off with a reference countof 1. Clients receiving that object can opt to retain it, therebyincrementing its reference count by 1. If a client retains an object,the client must also release that object when it is no longer needed.Releasing an object decrements its reference count by 1. When anobject’s reference count equals 0, the system automatically reclaimsthe memory for the object.
<div class="notebox clear">Note: iPhone OS does not support memory management using the garbage collection feature that is in Mac OS X v10.5 and later.
页:
[1]