Professional Documents
Culture Documents
iPhone
Project Templates Application Skeleton Application Bundle Using the Debugger Using the Simulator Using the Compiler Directives The Core Application Objects The Application Life Cycle The Application States and Transitions Preserving the Applications State Launching in Landscape Mode Inter-application Communication Application Preferences Application Configuration
iPhone ToolChain
XCode Objective-C, GDB
Simulator Testing
Project Templates
Several project templates are provided: View-based Application. An application that uses a single view to implement its user interface. It provides a view controller to manage the view and an empty xib file to populate the view GUI elements Navigation-based Application. An application that presents data hierarchically, using multiple screens. Tab Bar Application. An application that presents a radio interface that lets the user choose from several screens. Window-based Application. This template serves as a starting point for any application, containing an application delegate and a window. Use this template when you want to implement your own view hierarchy. OpenGL ES Application. An application that uses an OpenGL ESbased view to present images or animation. Utility Application. An application that implements a main view and lets the user access a flip-side view to perform simple customizations. The Stocks application is an example of a utility application. Split Viewbased Application. (for iPad)
Starting a Project
(A) moves you between the project overview and the visual debugger. (B) sets your targets and configuration. These control the application you intend to build and the way you intend to build it. ( by default iPhone templates provide two configurations, Debug and Release) (C), a pop-up that offers typical project functionality like adding new files and Reveal in Finder to locate those files on your Macintosh. (D). Build and Go button ( on the device or in the simulator) (E) Info button, when clicked opens a window that you can use to customize parts of your project. (F) Groups and Files column . List includes any files used to build the application plus any other files youve added to the project. The folder system shown is completely arbitrary. Can be skipped entirely. (G) Products folder , last item in the folders list by default, contains the item you intend to build. (H) The HelloWorld.app application . Shown in red when it is not built. (I) Targets item. Click the gray disclosure triangle to reveal the application. (J) Detail lists and previews files in project. It helps find files and open them to edit. (K) pane option: Project Find (L) SCM Results , shows the status of files relative to a Source Code Management System (M) Build presents a results window for building projects, showing any errors and their details. (N) Detail pane (default selection for the project window). Lists all the files in the project. (O) offers a preview of whichever element has been selected on the top. Its also a live editor, so any changes made in the bottom pane update the file in question. (P) Resize bar sits between the top file list and the bottom editor/preview. Used to adjust the proportions between the two elements.
The Executable
Boilerplate Code
Your Apps Code These folders, called groups are just abstractions to help you organize your project -- they dont even exist in the filesystem. Rearrange however you want.
Application Skeleton
Main.m
Creates a primary autorelease pool Invokes the application Event loop
int retVal = UIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString*delegateClassName );
appDelegate(.h/.m)
Responsible for initializing a windowing system at launch time and wrap up at termination Key player for handling memory warnings Monitor the high-level behavior of the application
viewController (.h/.m/.xib)
UI-Driven Programming
int main(int argc, char *argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; int retVal = UIApplicationMain(argc, argv, nil, nil); [pool release]; return retVal; }
UI-Driven Programming
- (void)applicationDidFinishLaunching:(UIApplication *)application { // Override point for customization after app launch ! [window makeKeyAndVisible]; }
UI-Driven Programming UIApplication ------- UIAppDelegate - (void)applicationDidFinishLaunching:(UIApplication *)application { //Initialize the User Interface [window makeKeyAndVisible]; } After which point your app is almost entirely UI Driven
View : The view is by default a member of the UIView class Files Owner represents the view controller. An abstract class, and it is a proxy because it plays a role in IB, but the object is not itself embedded in the .xib archive. View controllers dont have a visual presentation. They manage views, but they dont display anything of their own. Each view controller has an instance variable called view which is set to some UIView (in this case, the one at the right) that is responsible for providing the actual onscreen presentation. In the case of view controllers, the Files Owner proxy represents the object that loads and owns the .xib. First Responder: a proxy object. It represents the onscreen object that is currently responding to user touches.
It changes during the lifetime of an application as users interact with the screen.
Outlets
Instance variables ( in the IB-talk).
Misc.
The style of the project window depends on an Xcode setting. Choose Xcode > Preferences (Command-,),
select the General pane, and choose the layout from the pop-up: All-In-One layout: combines operations to a single window Condensed: separate windows for most tasks Default: has a core project window and separate tool windows.
The simulator versions of the frameworks are located in the Xcode developer directory:
/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.0.sdk/System/Library
Enabling Zombies:
a zombie is an object that has been destroyed or released that you are still trying to send messages to. debug mode lets you gather information about messages sent to invalid objects Add the NSZombieEnabled to the variables section of the Executable Info
#else
3.0 Code #endif
#define __IPHONE_2_0 20000 , #define __IPHONE_2_1 20100 #define __IPHONE_2_2 20200 , #define __IPHONE_3_0 30000
Pragma marks: organize source code by adding bookmarks into the method list pop-up button at the top of each Xcode window.
#pragma mark delegate functions #pragma mark - ( spacer )
/* * main.m * <<PROJECTNAME>> * * Created by <<FULLUSERNAME>> on <<DATE>>. * Copyright (c) <<YEAR>> <<ORGANIZATIONNAME>>. All rights reserved. * */ Your user and organization names are retrieved from your Address Book
Using Instruments
Instruments plays an important role in tuning applications. offer a suite of tools to monitor performance. Leak detection: track, identify, and resolve memory leaks within the program. Cached Objects Allocation Monitoring / Simulate Memory Warnings:
Hardware > Simulate Memory Warning sends calls to the application delegate and view controllers, asking them to release unneeded memory Allows creation of trace files to compare runs.
applicationDidBecomeActive: or applicationDidEnterBackground:
NSApplicationDidFinishLaunchingNotification can be added in any class to listen to the same event
applicationDidBecomeActive: applicationWillResignActive:
Called when the user presses the Home button or the system launches another application
applicationWillEnterForeground:
The application is about to enter the foreground. UIApplicationWillEnterForegroundNotification notification is also available for tracking when your application reenters the foreground.
applicationWillTerminate:
The application is about to be terminated and purged from memory entirely. Use this method to perform any final clean-up tasks, such as freeing shared resources, saving user data, invalidating timers, and storing enough application state to reconstitute the applications interface when it is relaunched.
applicationDidEnterBackground:
Application Termination
Applications are generally moved to the background and suspended if any of the following conditions are true, the application is terminated and purged from memory instead of being moved to the background: The application is linked against a version of iOS earlier than 4.0. The application is deployed on a device running a version of iOS earlier than 4.0. The current device does not support multitasking; The application includes the UIApplicationExitsOnSuspend key in its Info.plist file
One approach is to build a property list that is structured to match the organization of the view controllers. The property list saves information about each view controller in a dictionary object. The keys of the dictionary identify properties of the view controller Should save information only about those view controllers that are not part of your applications default user interface. Save in applicationDidEnterBackground: or applicationWillTerminate: as an application preference. In application:didFinishLaunchingWithOptions: method, load the property list from preferences
To track whether data protection is currently enabled. The application delegate can implement the applicationProtectedDataWillBecomeUnavailable: and applicationProtectedDataDidBecomeAvailable: methods and use them to track changes to the availability of protected data. An application can register for the UIApplicationProtectedDataWillBecomeUnavailable and UIApplicationProtectedDataDidBecomeAvailable notifications. The protectedDataAvailable property of the shared UIApplication object indicates whether protected files are currently accessible.
To register custom URL Scheme, include the CFBundleURLTypes key in your applications Info.plist file
Application Configuration
Set up the applications properties to customize its runtime environment Configure its entitlements to take advantage of iOS security features Xcode uses two main types of property-list file to store runtime-configuration information for the application: Information-property list: (info-plist files) contain essential information used by the application and iOS. Entitlements: define properties that provide the application access to iOS features (such as push notifications) and secure data (such as the users keychain).
Application Entitlements