You are on page 1of 7

Simple Navigation Template App

In this document, we list the relevant code sections for a simple Navigation template based app. In such apps, each view is a list of items. Clicking on an item takes you another view of a list of items, or more details related to the clicked item. These lists, hence, are grouped hierarchically.

Source Code
The relevant sections of the source code sections: 1. main.m 2. MyNav_AppDelegate 3. RootViewController

main.m
#import <UIKit/UIKit.h> int main(int argc, char *argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; int retVal = UIApplicationMain(argc, argv, nil, nil); [pool release]; return retVal; } ...

AppDelegate
In the AppDelegate, the ... - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // Add the navigation controller's view to the window and display. [window addSubview:navigationController.view]; [window makeKeyAndVisible];

return YES; } ...

RootViewController.m
... @implementation RootViewController NSMutableArray *ItemArray;

#pragma mark #pragma mark View lifecycle // - (void)viewDidLoad { [super viewDidLoad]; // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem; self.title = @"Mental Math"; NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithObjects:@"Multiplication", nil]; ItemArray = [tmpArray mutableCopy]; [tmpArray release]; NSLog(@"# of items in array = %d",[ItemArray count]); } #pragma mark #pragma mark Table view data source // Customize the number of sections in the table view. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; }

// Customize the number of rows in the table view. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

NSLog(@"Number of Rows in Section: # of items in array = %d",[ItemArray count]); return [ItemArray count]; }

// Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } // Configure the cell. cell.textLabel.text = [ItemArray objectAtIndex:indexPath.row]; return cell; } #pragma mark #pragma mark Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // MultiplicationPractice *detailViewController = [[MultiplicationPractice alloc] initWithNibName:@"MultiplicationPractice" bundle:nil]; // ... // Pass the selected object to the new view controller. [self.navigationController pushViewController:detailViewController animated:YES]; [MultiplicationPractice release]; // }

...

Specifying Data
Instead of hard coding arrays in the viewDidLoad method, we can read from a text file. This makes it easier to modify the app. In our app, each view containing a list of items is specified in an XML file. The structure of the file is as follows: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http:// www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Item1</key> <dict> <key>Label</key> <string>Item 1</string> <key>ShortLabel</key> <string>Item 1</string> <key>Rank</key> <string>yyy</string> <key>NextFileName</key> <string>Item1DetailData</string> <key>NextViewType</key> <real>1</real> </dict> <key>Item2</key> <dict> <key>Label</key> <string>Item 2</string> <key>ShortLabel</key> <string>Item 2</string> <key>Rank</key> <string>zzz</string> <key>NextFileName</key> <string>Item2ListData</string> <key>NextViewType</key> <integer>0</integer> </dict> </dict>

</plist> ...

Loading/Deploying App to Device


Testing your app on a simulator is all well and good. But, at some time, you will want to see it work on an actual device. Several reasons: 1. Access to components not available on the simulator, such as true GPS, multi finger gestures, compass, camera. 2. Speed of actual device is different than on the Mac, typically slower. So, you may have to modify your code to get acceptable performance 3. You may be inadvertently using libraries on the Mac that are no available on the iphone. So, your app will run on the simulator but will crash when you run it on an actual device. Loading your app onto the device is not a matter of simply transferring it to the device. There are actually a few things that need to be done so that you can transfer your app to the device. These are mainly related to first, ensuring that you are a valid developer, and second that the app can only work on registered devices. There are 3 main steps: 1. Getting Xcode Ready (one time) a. Certificates: Validate that you are bona fide developer

b.Mobile Provisioning Profiles: Associates Certificate with particular apps and device i. Register device on Portal
2. Getting the Device Ready (one time) a.Loading the Mobile Provisioning Profile (done with iTunes syncing or Xcode when device is registered to be used for development) 3. Getting the App Ready

a. Setting the Bundle Identifier in Info.plist to the App ID b.Setting the Code Signing Identity with the proper Certificate c. Switch build for device
Getting Xcode Ready (one time)
Thus, there are 2 main concepts: 1. Certificates 2. Mobile Provisioning Profiles The Certificate validates that you are a bona fide developer. You create the certificate on your Mac, upload it to the iOS Provisioning Portal on the developer.apple.com web site. Then, after it's been confirmed by Apple (all happens in real time and almost instantaneously, you download the certificate

along with the Apple's WWDR certificate to your Mac. To install in the Mac OS X keychain, double click the 2 downloaded files in turn. Xcode will look for these certificates in your Mac OS X's keychain so that Xcode can sign your application binaries. (You can check the certificates are installed correctly by opening KeyChain Access app (Applications > Utilities ), and clicking on the Certificates category. The Mobile Provisioning Profiles ties the Certificate with your hardware devices and your app. While the Certificate remains on the Mac, the Mobile Provisioning Profile is transferred to your device. To generate a Mobile Provisioning Profile, there are 3 main steps: 1. Register devices: Register the unique Identifiers of the devices (UDID - Unique Device Identifier) you want to use. 2. Generate App ID a. Name: Can be anything you want to refer to the app.

use this App ID for multiple applications. Otherwise, you would have to generate a new App ID for each of your applications. (Later, when preparing your app for deployment on the device, you can use a name such as com.somesite.MyApp1 etc.) Technically this simply means that all the applications created using this bundle identifier will share the same portion of the keychain on the device. 3. Download profile and install into Xcode by dragging profile onto the Xcode in dock: a.Once the device is registered and you have an App ID, go to the Provisioning > Developer section of the Portal on the developer.apple.com site. b.Click "New Profile," and enter any Profile Name. It's best to use something that has "Developer Profile" in the name. Then, select App ID and the devices you want to associate with this profile. c.

b.Bundle Seed: Select Generate New c. Bundle Identifier: Use something like com.somesite.* (the * is a wildcard and lets you

(You can check whether it's installed correctly: 1. Mac: UserName > MobileDevice/Provisioning Profiles (this will get transferred to your device the next time you synch with your device with iTunes) 2. iPhone/iPad/iTouch: Settings > General > Profile Finally, to deploy your app, need to: Get your Device Ready (one time) a. Plug your app into the Mac

i. If this device regularly syncs with iTunes, let it first complete it's syncing. ii. Then, open Xcode 1.Xcode will recognize your device and ask whether you want to use it for development.
Get your App Ready

a.Open the Info.plist file, and edit the Bundle Identifier key's value to be the Bundle Identifier you set when creating the App ID on the portal. (Set it, for example, to com.somesite.MyApp1). This will match it with the Mobile Provisioning Profile on the device. b.Open the Project window (for example, by double-clicking the App's project icon at the top of the Groups & Files pane in Xcode. i. Build tab 1.Code Signing Identify > Any iPhone OS Device: Select the iPhone Developer Certificate c.Switch from Simulator mode to Device and compile (Build & Run). This will deploy app to the device.

You might also like