Sei sulla pagina 1di 14

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
1esLlng your app on a slmulaLor ls all well and good. 8uL, aL some Llme, you wlll wanL Lo see lL work on
an acLual devlce. Several reasons:
1. Access Lo componenLs noL avallable on Lhe slmulaLor, such as Lrue CS, mulLl flnger gesLures,
compass, camera.
2. Speed of acLual devlce ls dlfferenL Lhan on Lhe Mac, Lyplcally slower. So, you may have Lo
modlfy your code Lo geL accepLable performance
3. ?ou may be lnadverLenLly uslng llbrarles on Lhe Mac LhaL are no avallable on Lhe lphone. So,
your app wlll run on Lhe slmulaLor buL wlll crash when you run lL on an acLual devlce.
Loadlng your app onLo Lhe devlce ls noL a maLLer of slmply Lransferrlng lL Lo Lhe devlce. 1here are
acLually a few Lhlngs LhaL need Lo be done so LhaL you can Lransfer your app Lo Lhe devlce. 1hese are
malnly relaLed Lo flrsL, ensurlng LhaL you are a valld developer, and second LhaL Lhe app can only work
on reglsLered devlces.

1here are 3 maln sLeps:
1. CeLLlng xcode 8eady (one Llme)
a.
CerLlflcaLes: valldaLe LhaL you are bona flde developer
b.
Moblle rovlslonlng roflles: AssoclaLes CerLlflcaLe wlLh parLlcular apps and devlce
i.
8eglsLer devlce on orLal
2. CeLLlng Lhe uevlce 8eady (one Llme)
a.
Loadlng Lhe Moblle rovlslonlng roflle (done wlLh l1unes synclng or xcode when
devlce ls reglsLered Lo be used for developmenL)
3. CeLLlng Lhe App 8eady
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)
1hus, Lhere are 2 maln concepLs:
1. CerLlflcaLes
2. Moblle rovlslonlng roflles
1he CerLlflcaLe valldaLes LhaL you are a bona flde developer. ?ou creaLe Lhe cerLlflcaLe on your Mac,
upload lL Lo Lhe lCS rovlslonlng orLal on Lhe developer.apple.com web slLe. 1hen, afLer lL's been
conflrmed by Apple (all happens ln real Llme and almosL lnsLanLaneously, you download Lhe cerLlflcaLe
along wlLh Lhe Apple's WWu8 cerLlflcaLe Lo your Mac. 1o lnsLall ln Lhe Mac CS x keychaln, double cllck
Lhe 2 downloaded flles ln Lurn. xcode wlll look for Lhese cerLlflcaLes ln your Mac CS x's keychaln so LhaL
xcode can slgn your appllcaLlon blnarles.

(?ou can check Lhe cerLlflcaLes are lnsLalled correcLly by openlng keyChaln Access app (AppllcaLlons >
uLlllLles ), and cllcklng on Lhe CerLlflcaLes caLegory.

1he Moblle rovlslonlng roflles Lles Lhe CerLlflcaLe wlLh your hardware devlces and your app.

Whlle Lhe CerLlflcaLe remalns on Lhe Mac, Lhe Moblle rovlslonlng roflle ls Lransferred Lo your devlce.

1o generaLe a Moblle rovlslonlng roflle, Lhere are 3 maln sLeps:
1. keg|ster dev|ces: Register the unique Identifiers of the devices (UDID - Unique Device
Identifier) you want to use.
2. Generate App ID
a.
name: Can be anyLhlng you wanL Lo refer Lo Lhe app.
b.
8undle Seed: SelecL Ceoetote New
c.
8undle ldenLlfler: use someLhlng llke com.someslLe.* (Lhe * ls a wlldcard and leLs you
use Lhls App lu for mulLlple appllcaLlons. CLherwlse, you would have Lo generaLe
a new App lu for each of your appllcaLlons. (LaLer, when preparlng your app for
deploymenL on Lhe devlce, you can use a name such as com.someslLe.MyApp1 eLc.)
1echnlcally Lhls slmply means LhaL all Lhe appllcaLlons creaLed uslng Lhls bundle
ldenLlfler wlll share Lhe same porLlon of Lhe keychaln on Lhe devlce.
3. Down|oad prof||e and |nsta|| |nto kcode by dragglng proflle onLo Lhe xcode ln dock:
a.
Cnce Lhe devlce ls reglsLered and you have an App lu, go Lo Lhe rovlslonlng >
ueveloper secLlon of Lhe orLal on Lhe developer.apple.com slLe.
b.
Cllck "new roflle," and enLer any roflle name. lL's besL Lo use someLhlng LhaL
has "ueveloper roflle" ln Lhe name.
c.
1hen, selecL App lu and Lhe devlces you wanL Lo assoclaLe wlLh Lhls proflle.
(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.


SimpIe Navigation TempIate App

n this document, we list the relevant code sections for a simple Navigation template based app.
n 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;
}

...
AppDeIegate
n 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;
}

...
RootViewControIIer.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
nstead of hard coding arrays in the viewDidLoad method, we can read from a text file. This
makes it easier to modify the app.

n 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/DepIoying App to Device
1 8
S
1. A CS

2. S M S

3. ? M S

L 1
1



1
1. C x 8
a.
C v
b.
M A C
i.
8
2. C u 8
a.
L M 1 x

3. C A 8
a. Setting the Bundle Identiier in Ino.plist to the App ID
b.Setting the Code Signing Identity with the proper Certiicate
c. Switch build or deice

Getting Xcode Ready (one time)
1
1. C
2. M
1 C ? M
CS 1
A
A WWu8 M 1 M CS x
x M CS x
x

? kC A A
u C

1 M C

W C M M

1 M
1. Z Register the unique IdentiIiers oI the devices (UDID - Unique Device
IdentiIier) you want to use.
2. '/
a.
n C
b.
8 S S 'E
c.
8 l u
A lu C
A lu L
D
1

3. y x
a.
C A lu
u
b.
C n n l
u
c.
1 A lu
(You can check whether it's installed correctly:
1. Mac: UserName ~ MobileDevice/Provisioning ProIiles (this will
get transIerred to your device the next time you synch with your
device with iTunes)
2. iPhone/iPad/iTouch: Settings ~ General ~ ProIile
linally, to deploy your app, need to:

Get your Deice Ready ,one time,
a. Plug your app into the Mac
i. I this deice regularly syncs with i1unes, let it irst
complete it's syncing.
ii. 1hen, open Xcode
1.Xcode will recognize your deice and ask
whether you want to use it or deelopment.

Get your App Ready
a.Open the *OGPQMJTU ile, and edit the #VOEMF *EFOUJGJFS key's alue
to be the Bundle Identiier you set when creating the App ID
on the portal. ,Set it, or example, to com.somesite.MyApp1,.
1his will match it with the Mobile Proisioning Proile on the
deice.
b.Open the Project window ,or example, by double-clicking the
App's project icon at the top o the (SPVQT 'JMFT pane in
Xcode.
i. Build tab
1.Code Signing Identiy Any iPhone OS
Deice: Select the iPhone Deeloper Certiicate
c.Switch rom Simulator mode to Deice and compile ,Build &
Run,. 1his will deploy app to the deice.

Potrebbero piacerti anche