iphone ilearn beginners developer course

Upload: sumantra-joshi

Post on 05-Apr-2018

242 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    1/65

    Achieving Capability Excellence

    SEG Academy

    iPhone Developer Training

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    2/65

    2 2

    Table of content1. Introduction

    2. iPhone OS Architecture & Technologies

    3. Whats in the iPhone SDK?4. Getting iPhone setup.

    5. Object Basics

    6. Objective-C Language

    1. Messaging syntax

    2. Objective-C Types3. Working with Classes & Objects4. Delegates and Memory Allocation5. Foundation Classes

    7. Create View Based Project Hello World Example

    1. Connect Code to An Interface Builder

    2. Update UI on events3. Executing code4. Analysis

    8. Navigation Based Application

    1. Adding Sub-View to UITableView

    9. Windows Based Application

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    3/65

    3 3

    Introduction

    The iPhone OS platform was builtusing the knowledge that went intothe creation of Mac OS X, and many ofthe tools and technologies used fordevelopment on the platform havetheir roots in Mac OS X as well.Despite its similarities to Mac OS X,

    you do not need to be an experiencedMac OS X developer to writeapplications for iPhone OS. The iPhoneSoftware Development Kit (SDK)provides everything you need to getstarted creating iPhone applications.

    The iPhone SDK contains the tools needed to design, create, debug, and optimize software foriPhone OS. It also contains header files, sample code, and documentation for the platformstechnologies. You can download the iPhone SDK from the members area of the iPhone Dev Center,which is located at http://developer.apple.com/iphone. Registration is required but free.

    iPhone OS is the operating system at the heart of iPhone and iPod touch devices.

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    4/65

    4 4

    iPhone OS Architecture & Technologies

    iPhone OS is the operating system that runs on iPhone and iPod touchdevices. This operating system manages the device hardware and alsoprovides the basic technologies needed to implement native applicationson the phone.

    The iPhone OS architecture is similar to the basic architecture found inMac OS X. At the high level, iPhone OS acts as an intermediary betweenthe iPhone and iPod touch hardware and the applications that appear onthe screen, as shown in figure. Applications that you create neverinteract directly with the hardware but instead go through systeminterfaces, which interact with the appropriate drivers. This abstractionprotects your application from changes to the underlying hardware.

    iPhone OS uses a fairly straightforward software stack. At the verybottom of this stack is the Mach kernel and hardware drivers, whichmanage the overall execution of programs on the device. On top of thatlayer are additional layers that contain the core technologies andinterfaces you use for development. Although iPhone OS does notexpose any of the kernel or driver-level interfaces, it does exposetechnologies at the higher levels of the stack.

    Applications layered on top of iPhone OS

    The implementation of iPhone OS technologies can be viewed as a set of layers. Atthe lower layers of the system are the fundamental services on which allapplications rely, while higher-level layers contain more sophisticated services andtechnologies.

    Whenever possible. Use higher-level frameworks in your program over lower-level. Higher-level frameworks provide object-oriented abstractions for lower-level

    constructs.

    iPhone OS Architecture

    iPhone OS Technologies

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    5/65

    5 5

    Whats in the iPhone SDK?

    XCode Tools

    Provides tools that support iPhone applicationdevelopment, including the following key applications:

    XCode - an integrated development environmentthat manages your application projects and letsyou edit, compile, run, and debug your code.

    InterfaceBuilder - a tool you use to assembleyour user interface visually. The interface objectsyou create are then saved to a resource file formatand loaded into your application at runtime.

    Instruments - a runtime performance analysisand debugging tool. You can use Instruments togather information about your applicationsruntime behavior and identify potential problems.

    iPhone Simulator

    Mac OS X application that

    simulates the iPhonetechnology stack, allowingyou to test iPhone applicationslocally on your Intel basedMacintosh computer.

    iPhone Reference Library

    SDK includes reference documentation foriPhone OS by default and you can downloadmore complete version of the iPhoneReference Library by subscribing to theiPhone OS Library doc set. From Xcode,choose Help > Documentation and then clickthe subscribe button next to the iPhone OS

    Library doc set in the left-hand column.

    Xcode and Instruments also let you interact directlywith an attached device to run and debug your codeon the target hardware. Development on an actualdevice requires signing up for Apples paid iPhoneDeveloper Program and configuring a device fordevelopment purposes

    &

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    6/65

    6 6

    Getting iPhone setup

    Sign Up For a Developer Account

    Head on over toApple Developer Connection andsign up for a developer account. You can sign upfor an account for free to be able to gain access toall of the tools and documentation. This will alsoprovide you with a built-in iPhone simulator so thatyou can test your applications on your localmachine. However, when you are ready to deployyour application on an iPhone, you must sign upfor the iPhone Developer Program.

    Get the iPhone SDK

    Now that you have signed up for that, you will haveaccess to the iPhone SDK, Documentation, SampleCode, and API. The first thing to do is download theiPhone SDK. This includes the latest version ofXCode and contains the entire suite for developingiPhone applications. The installation is pretty straitforward. Just accept all of the defaults and youll beon your way.

    What Can You Create?

    Users can run two different types of customapplications on a device: web applications and nativeapplications.

    Web applications use a combination of HTML,cascading style sheets, and JavaScript code toimplement interactive applications that live on a webserver and are transmitted over the network and runinside the Safari web browser.

    Native applications, on the other hand, are installeddirectly on the device and can run without thepresence of a network connection.

    The iPhone SDK supports the creation of nativeforeground applications that appear on the devicesHome screen only. It does not support the creation ofother types of code, such as drivers, background

    applications, frameworks, or dynamic libraries. If youwant to integrate code from a framework or dynamiclibrary into your application, you should link that codestatically into your applications executable file whenbuilding your project.

    https://connect.apple.com/cgi-bin/WebObjects/MemberSite.woa/wa/promo?source=ADCLOG&code=ADCLOG-NEXhttp://developer.apple.com/iphone/program/http://developer.apple.com/iphone/download.action?path=/iphone/iphone_sdk_9m2199a/iphone_sdk_final.dmghttp://developer.apple.com/iphone/download.action?path=/iphone/iphone_sdk_9m2199a/iphone_sdk_final.dmghttp://developer.apple.com/iphone/download.action?path=/iphone/iphone_sdk_9m2199a/iphone_sdk_final.dmghttp://developer.apple.com/iphone/download.action?path=/iphone/iphone_sdk_9m2199a/iphone_sdk_final.dmghttp://developer.apple.com/iphone/program/https://connect.apple.com/cgi-bin/WebObjects/MemberSite.woa/wa/promo?source=ADCLOG&code=ADCLOG-NEX
  • 7/31/2019 iPhone iLearn Beginners Developer Course

    7/657 7

    Object Basics

    OOP Vocabulary

    Class: defines the grouping of data and code, the type of an object.

    Instance: a specific allocation of a class. Method: a function that an object knows how to perform. Instance Variable (or ivar): a specific piece of data belonging to an object. Encapsulation : keep implementation private and separate from interface. Polymorphism : different objects, same interface. Inheritance : hierarchical organization, share code, customize or extend behaviors.

    More OOP Info?

    Tons of books and articles on OOP Most Java or C++ book have OOP introductions Objective-C 2.0 Programming Language

    http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    8/658 8

    Objective-C

    About Objective-C

    Strict superset of C. Mix C with ObjC Or even

    C++ with ObjC (usually referred to as ObjC++). A very simple language, but some new syntax. Single inheritance, classes inherit from one and

    only one superclass. Protocols define behavior that cross classes. Dynamic runtime & Loosely typed, if youd like.

    Syntax Additions

    Small number of additions Some new types

    Anonymous object Class Selectors

    Syntax for defining classes Syntax for message

    expressions.

    Dynamic Runtime

    Object creation All objects allocated out of the heap No stack based objects

    Message dispatch Introspection

    OOP From ObjC Perspective

    Everybody has their own spin on OOP.

    Apple is no different. For the spin on OOP from an ObjC perspective:

    Read the Object-Oriented Programmingwith Objective-C document.

    http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/OOP_ObjC

    Classes and Objects

    Classes declare state andbehavior

    State (data) is maintainedusing instance variables

    Behavior is implemented usingmethods

    Instance variables typicallyhidden

    Accessible only using

    getter/setter methods.

    Classes and Instances

    In Objective-C, classes and instances are bothobjects.

    Class is the blueprint to create instances.

    http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/OOP_ObjChttp://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/OOP_ObjChttp://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/OOP_ObjChttp://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/OOP_ObjC
  • 7/31/2019 iPhone iLearn Beginners Developer Course

    9/659 9

    Message Syntax

    To get an object to do something, you send it a message telling it to apply a method.In Objective-C, message expressions are enclosed in brackets: [receiver message]

    The receiver is an object, and the message tells it what to do. In source code, the message is simply the name of amethod and any arguments that are passed to it.

    When a message is sent, the runtime system selects the appropriate method from the receivers repertoire andinvokes it. For example, this message tells the myRectangle object to perform its display method, which causes therectangle to display itself:

    [myRectangle display];

    Message syntax

    [receiver message]

    [receiver message:argument]

    [receiver message:arg1 andArg:arg2]

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    10/6510

    Message examples

    P e r s o n * v o t e r ;

    / / a s s u m e t h i s e x i s t s

    [ v o t e r c a s t B a l l o t ] ;

    i n t t h e A g e = [ v o t e r a g e ] ;

    [ v o t e r s e t A g e : 2 1 ] ;

    i f ( [ v o t e r c a n L e g a l l y V o t e ] ) {

    / / d o s o m e t h i n g v o t e r - y

    }

    10

    Messaging syntaxClass and Instance Methods

    Instances respond to instance methods- ( i d ) i n i t ;

    - ( f l o a t ) h e i g h t ;- ( v o i d ) w a l k ;

    Classes respond to class methods+ ( i d ) a l l o c ;+ ( i d ) p e r s o n ;+ ( P e r s o n * ) s h a r e d P e r s o n ;

    Terminology

    Message expression

    [ r e c e i v e r m e t h o d : a r g u m e n t ]

    Message

    [ r e c e i v e r m e t h o d : a r g u m e n t ]

    Selector[ r e c e i v e r m e t h o d : a r g u m e n t ]

    Method

    The code selected by a message.

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    11/6511 11

    Messaging syntax

    Method definition examples

    P e r s o n * v o t e r ; / / a s s u m e t h i s e x i s t s

    P e r s o n * c a s t B a l l o t ;

    [ v o t e r c a s t B a l l o t ] ;

    - ( i n t ) a g e ;

    i n t t h e A g e = [ v o t e r a g e ] ;

    - ( v o i d ) s e t A g e : ( i n t ) a g e ;

    [ v o t e r s e t A g e : 2 1 ] ;

    - ( B O O L ) c a n L e g a l l y V o t e ;

    i f ( [ v o t e r c a n L e g a l l y V o t e ] ) {

    / / d o s o m e t h i n g v o t e r - y

    }

    -( v o i d ) r e g i s t e r F o r S t a t e : ( N S S t r i n g * ) s t a t e

    p a r t y : ( N S S t r i n g * ) p a r t y ;

    [ v o t e r r e g i s t e r F o r S t a t e : @ " C A " p a r t y : @ " I n d e p e n d a n t " ] ;

    - ( P e r s o n * ) s p o u s e ;

    -( N S S t r i n g * ) n a m e ;

    N S S t r i n g * n a m e = [ [ v o t e r s p o u s e ] n a m e ] ;

    Dot Syntax

    Objective-C 2.0 introduced dot syntax Convenient shorthand for invoking accessor methods

    f l o a t h e i g h t = [ p e r s o n h e i g h t ] ;

    f l o a t h e i g h t = p e r s o n . h e i g h t ;

    [ p e r s o n s e t H e i g h t : n e w H e i g h t ] ;

    p e r s o n . h e i g h t = n e w H e i g h t ;

    Follows the dots. ..

    [ [ p e r s o n c h i l d ] s e t H e i g h t : n e w H e i g h t ] ;

    / / e x a c t l y t h e s a m e a s

    p e r s o n . c h i l d . h e i g h t = n e w H e i g h t ;

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    12/6512 12

    Objective-C Types

    Dynamic and static typing

    Dynamically-typed object

    i d a n O b j e c t Just id Not id * (unless you really, really mean it)

    Statically-typed object

    P e r s o n * a n O b j e c t Objective-C provides compile-time, not runtime,

    type checking Objective-C always uses dynamic binding

    The null object pointer

    Test for nil explicitly

    i f ( p e r s o n = = n i l )r e t u r n ;

    Or implicitly

    i f ( ! p e r s o n ) r e t u r n ; Can use in assignments and as arguments ifexpected

    p e r s o n = n i l ;[ b u t t o n s e t T a r g e t : n i l ] ;

    Sending a message to nil?

    p e r s o n = n i l ;[ p e r s o n c a s t B a l l o t ] ;

    Selectors identify methods by name

    A selector has type SEL

    S E L a c t i o n = [ b u t t o n a c t i o n ] ;[ b u t t o n

    s e t A c t i o n : @ s e l e c t o r ( s t a r t : ) ] ; Conceptually similar to function pointer

    Selectors include the name and all colons, for example:

    - ( v o i d ) s e t N a me : ( N S S t r i ng * ) n a m ea g e : ( i n t ) a g e ;

    would have a selector:

    S E L s e l = @ s e l e c t o r ( s e t N a m e : a ge : ) ;

    BOOL typedef

    When ObjC was developed, C had no boolean type

    ObjC uses a typedef to define BOOL as a typeB O O L f l a g = N O ;

    Macros included for initialization and comparison: YES & NO

    i f ( f l a g = = Y E S )i f ( f l a g )i f ( ! f l a g )i f ( f l a g ! = Y E S )f l a g = Y E S ;

    f l a g = 1 ;

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    13/6513 13

    Working with Classes & Objects

    Class Introspection

    You can ask an object about its class

    C l a s s m y C l a s s = [ m y O b j e c t c l a s s ] ;N S L o g ( @ " M y c l a s s i s % @ " , [ m y O b j e c t

    c l a s s N a m e ] ) ; Testing for general class membership (subclasses included):

    i f ( [ m y O b j e c ti s K i n d O f C l a s s : [ U I C o n t r o l c l a s s ] ] ) {

    / / s o m e t h i n g}

    Testing for specific class membership (subclasses excluded):

    i f ( [ m y O b j e c ti s M e m b e r O f C l a s s : [ N S S t r i n gc l a s s ] ] ) {

    / / s o m e t h i n g s t r i n g s p e c i f i c}

    -description

    NSObject implementsdescription

    - ( N S S t r i n g * ) d e s c r i p t i o n ;

    Objects represented in format strings using

    %@

    When an object appears in a format string, it is

    asked for its description[ N S S t r i n g s t r i n g W i t h F o r m a t :

    @ T h e a n s w e r i s : % @ ,

    m y O b j e c t ] ;

    You can log an objects description with:

    N S L o g ( [ a n O b j e c t d e s c r i p t i o n ] ) ;

    Your custom subclasses can override description

    to return more specific information

    Identity versus Equality

    Identitytesting equality of the pointer valuesi f ( o b j e c t 1 = = o b j e c t 2 ) {N S L o g ( @ " S a m e e x a c t o b j e c t i n s t a n c e " ) ;}

    Equalitytesting object attributesi f ( [ o b j e c t 1 i s E q u a l : o b j e c t 2 ] ) {N S L o g ( @ " L o g i c a l l y e q u i v a l e n t , b u t m a y

    b e d i f f e r e n t o b j e c t i n s t a n c e s " ) ;

    }

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    14/6514 14

    Delegates and Memory Allocation

    Customizing Behaviour with Delegation?

    The idea of delegation is that an object can have a

    single delegateobject that it will call when certainevents occur.

    A delegate is an object that usually reacts to someevent in another object and/or can affect how anotherobject behaves.

    The objects work together for the greater good ofcompleting a task.

    Typically a delegate object will be shared by manyother objects that have a more specific task to carry out.

    The delegate itself will be more abstract and should bevery reusable for different tasks.

    The object which contains the delegate typically sends

    the delegate a message when a triggered event occurs,giving the delegate an opportunity to carry out itsspecified task.

    Memory Allocation

    Objects are created dynamically through the keyword,

    alloc

    Objects are dynamically deallocated using the wordsrelease and autorelease

    autorelease deallocates the object once it goes outof scope.

    NOTE: None of these words are built-in

    Example for De-allocation

    - (void)dealloc {[helloLabel release];[nameField release];[super dealloc];}

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    15/6515 15

    Foundation Classes

    Foundation Framework

    Value and collection classes

    User defaults Archiving Notifications Undo manager Tasks, timers, threads File system, pipes, I/O, bundles.

    NSObject

    Root class Implements many basics

    Memory management Introspection Object equality

    NSString

    General-purpose Unicode string support

    Unicode is a coding system which represents all of theworlds languages

    Consistently used throughout Cocoa Touch instead of char * Without doubt the most commonly used class Easy to support any language in the world with Cocoa.

    String Constants

    In C constant strings are simple

    In ObjC, constant strings are @just assimple

    Constant strings are NSString instances

    NSString *aString = @Hello World!;

    Format Strings

    Similar to printf, but with %@ added for objects

    NSString *aString = @Johnny;NSString *log = [NSString

    stringWithFormat: @Its %@,aString];

    log would be set to Its Johnny.

    Also used for logging

    NSLog(@I am a %@, I have %d items,[array className], [array count]);

    would log something like:

    I am a NSArray, I have 5 items

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    16/6516 16

    Foundation ClassesNSString

    Often ask an existing string for a new string with modifications

    - ( N S S t r i n g * ) s t r i n g B y A p p e n d i n g S t r i n g : ( N S S t r i n g * ) s t r i n g ;- ( N S S t r i n g * ) s t r i n g B y A p p e n d i n g F o r m a t : ( N S S t r i n g * ) s t r i n g ;- ( N S S t r i n g * ) s t r i n g B y D e l e t i n g P a t h C o m p o n e n t ;

    Example:

    N S S t r i n g * m y S t r i n g = @ H e l l o ; N S S t r i n g * f u l l S t r i n g ;f u l l S t r i n g = [ m y S t r i n g s t r i n g B y A p p e n d i n g S t r i n g : @ w o r l d ! ] ;

    fullString would be set to Hello world!

    Common NSString methods

    - ( B O O L ) i s E q u a l T o S t r i n g : ( N S S t r i n g * ) s t r i n g ;- ( B O O L ) h a s P r e f i x : ( N S S t r i n g * ) s t r i n g ;- ( i n t ) i n t V a l u e ;-( d o u b l e ) d o u b l e V a l u e ;

    Example:N S S t r i n g * m y S t r i n g = @ H e l l o ; N S S t r i n g * o t h e r S t r i n g = @ 4 4 9 ; i f ( [ m y S t r i n g h a s P r e f i x : @ H e ] ) {

    / / w i l l m a k e i t h e r e}i f ( [ o t h e r S t r i n g i n t V a l u e ] > 5 0 0 ) {

    / / w o n t m a k e i t h e r e }

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    17/65

    17 17

    Foundation Classes

    NSMutableString

    NSMutableString subclasses NSString Allows a string to be modified Common NSMutableString methods

    + ( i d ) s t r i n g ;- ( v o i d ) a p p e n d S t r i n g : ( N S S t r i n g * ) s t r i n g ;- ( v o i d ) a p p e n d F o r m a t : ( N S S t r i n g * ) f o r m a t , . . . ;

    N S M u t a b l e S t r i n g * n e w S t r i n g = [ N S M u t a b l e S t r i n g s t r i n g ] ;

    [ n e w S t r i n g a p p e n d S t r i n g : @ H i ] ; [ n e w S t r i n g a p p e n d F o r m a t : @ , m y f a v o r i t e n u m b e r i s : % d , [ s e l f f a v o r i t e N u m b e r ] ] ;

    Collections

    Array - ordered collection of objects Dictionary - collection of key-value pairs Set - unordered collection of unique objects

    Common enumeration mechanism Immutable and mutable versions

    Immutable collections can be shared without side effect Prevents unexpected changes Mutable objects typically carry a performance overhead

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    18/65

    18 18

    Foundation ClassesNSArray

    Common NSArray methods

    + a r r a y W i t h O b j e c t s : ( i d ) f i r s t O b j , . . . ; / / n i l t e r m i n a t e d ! ! !- ( u n s i g n e d ) c o u n t ;- ( i d ) o b j e c t A t I n d e x : ( u n s i g n e d ) i n d e x ;- ( u n s i g n e d ) i n d e x O f O b j e c t : ( i d ) o b j e c t ;

    NSNotFound returned for index if not found

    N S A r r a y * a r r a y = [ N S A r r a y a r r a y W i t h O b j e c t s : @ R e d , @ B l u e , @ G r e e n , n i l ] ;

    i f ( [ a r r a y i n d e x O f O b j e c t : @ P u r p l e ] = = N S N o t F o u n d ) {N S L o g ( @ N o c o l o r p u r p l e ) ; }

    Be careful of the nil termination!!!

    NSMutableArray

    NSMutableArray subclasses NSArray

    So, everything in NSArray

    Common NSMutableArray Methods

    + ( N S M u t a b l e A r r a y * ) a r r a y ;- ( v o i d ) a d d O b j e c t : ( i d ) o b j e c t ;- ( v o i d ) r e m o v e O b j e c t : ( i d ) o b j e c t ;- ( v o i d ) r e m o v e A l l O b j e c t s ;

    - ( v o i d ) i n s e r t O b j e c t : ( i d ) o b j e c t a t I n d e x : ( u n s i g n e d ) i n d e x ;

    NSMutableArray *array = [NSMutableArray array];

    [array addObject:@Red];[array addObject:@Green];[array addObject:@Blue];[array removeObjectAtIndex:1];

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    19/65

    19 19

    Foundation Classes

    NSDictionary - Common NSDictionary methods

    + d i c t i o n a r y W i t h O b j e c t s A n d K e y s : ( i d ) f i r s t O b j e c t , . . . ;

    - ( u n s i g n e d ) c o u n t ;- ( i d ) o b j e c t F o r K e y : ( i d ) k e y ;

    nil returned if no object found for given key

    N S D i c t i o n a r y * c o l o r s = [ N S D i c t i o n a r y d i c t i o n a r y W i t h O b j e c t s A n d K e y s : @ R e d ,@ C o l o r 1 , @ G r e e n , @ C o l o r 2 , @ B l u e , @ C o l o r 3 , n i l ] ;

    N S S t r i n g * f i r s t C o l o r = [ c o l o r s o b j e c t F o r K e y : @ C o l o r 1 ] ;

    i f ( [ c o l o r s o b j e c t F o r K e y : @ C o l o r 8 ] ) { / / w o n t m a k e i t h e r e }

    NSMutableDictionary - NSMutableDictionary subclasses NSDictionary

    Common NSMutableDictionary methods

    + ( N S M u t a b l e D i c t i o n a r y * ) d i c t i o n a r y ;- ( v o i d ) s e t O b j e c t : ( i d ) o b j e c t f o r K e y : ( i d ) k e y ;- ( v o i d ) r e m o v e O b j e c t F o r K e y : ( i d ) k e y ;- ( v o i d ) r e m o v e A l l O b j e c t s ;

    N S M u t a b l e D i c t i o n a r y * c o l o r s = [ N S M u t a b l e D i c t i o n a r y d i c t i o n a r y ] ;

    [ c o l o r s s e t O b j e c t : @ O r a n g e f o r K e y : @ H i g h l i g h t C o l o r ] ;

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    20/65

    20 20

    Foundation Classes

    NSSet

    Unordered collection of objects

    Common NSSet methods

    + s e t W i t h O b j e c t s : ( i d ) f i r s t O b j , . . . ; / / n i l t e r m i n a t e d- ( u n s i g n e d ) c o u n t ;- ( B O O L ) c o n t a i n s O b j e c t : ( i d ) o b j e c t ;

    NSMutableSet

    NSMutableSet subclasses NSSet

    Common NSMutableSet methods

    + ( N S M u t a b l e S e t * ) s e t ;- ( v o i d ) a d d O b j e c t : ( i d ) o b j e c t ;- ( v o i d ) r e m o v e O b j e c t : ( i d ) o b j e c t ;- ( v o i d ) r e m o v e A l l O b j e c t s ;- ( v o i d ) i n t e r s e c t S e t : ( N S S e t * ) o t h e r S e t ;- ( v o i d ) m i n u s S e t : ( N S S e t * ) o t h e r S e t ;

    d i l

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    21/65

    21 21

    Foundation ClassesEnumeration

    Consistent way of enumerating over objects in collections Use with NSArray, NSDictionary, NSSet, etc.

    N S A r r a y * a r r a y = . . . ; / / a s s u m e a n a r r a y o f P e o p l e o b j e c t s

    / / o l d s c h o o lP e r s o n * p e r s o n ;i n t c o u n t = [ a r r a y c o u n t ] ;f o r ( i = 0 ; i < c o u n t ; i + + ) {

    p e r s o n = [ a r r a y o b j e c t A t I n d e x : i ] ;N S L o g ( [ p e r s o n d e s c r i p t i o n ] ) ;

    }

    / / n e w s c h o o lf o r ( P e r s o n * p e r s o n i n a r r a y ) {

    N S L o g ( [ p e r s o n d e s c r i p t i o n ] ) ;}

    NSNumber

    In Objective-C, you typically use standard C number types NSNumber is used to wrap C number types as objects Subclass of NSValue No mutable equivalent! Common NSNumber methods

    + ( N S N u m b e r * ) n u m b e r W i t h I n t : ( i n t ) v a l u e ;+ ( N S N u m b e r * ) n u m b e r W i t h D o u b l e : ( d o u b l e ) v a l u e ;- ( i n t ) i n t V a l u e ;

    - ( d o u b l e ) d o u b l e V a l u e ;

    d i Cl

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    22/65

    22 22

    Foundation Classes

    Other Classes

    NSData / NSMutableData Arbitrary sets of bytes

    NSDate / NSCalendarDate Times and dates

    Getting some objects

    Use class factory methods

    N S S t r i n g s + s t r i n g W i t h F o r m a t :

    N S A r r a y s + a r r a y N S D i c t i o n a r y s + d i c t i o n a r y

    Or any method that returns an object except alloc/init or copy.

    More ObjC Info?

    http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC Concepts in Objective C are applicable to any other OOP language

    Thats it! Now we can move over to write our first Hello World application.

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    23/65

    23

    Create View Based Project Hello World Example

    This Hello World application will demonstrate how to work with Interface

    Builder to create a simple layout for an application. In fact, it wont require

    any code to be written at all. In this tutorial you will learn:

    Create a New View Based Project

    Opening the iPhone Simulator

    Adding UI Elements to applications home screen

    Executing the code

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    24/65

    24

    Create View Based Project Hello World Example

    Create a New View Based Project

    Open Xcode.

    Click on File > New Project.

    Make sure thatApplication is

    highlighted under iPhone OS.

    SelectView-Based Application.

    Name this project ViewBased.

    C eate Vie Based P oject Hello Wo ld E ample

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    25/65

    25

    Create View Based Project Hello World Example

    25

    Default Files

    At this point, Apple has added some default files to get us started.

    CoreGraphics.framework, Foundation.framwork, UIKit.frameworkThis is a set of library functionsprovided by Apple that we use in our application. We use these as includes similar to any other language thatincludes library functions.

    .app This is your app that gets installed on the iPhone. _Prefix.pch This is another include file that gets compiled separately from your other files so you

    dont need to include it on each file. It contains some code to include the data inside the frameworks. AppDelegate.h This is a header file that contains all of our definitions for variables that we will

    be using. Its very similar to a header file in C or C++. AppDelegate.m Consider this file our starting point for execution. The main.m file invokes this

    object. Info.plist This contains various meta information about your program. There isnt really a need to edit this until

    we are ready to start testing on the iPhone. main.m Like most programming language, this file contains our main function. This is where execution

    begins. The main function basically instantiates our object and starts the program. In usual circumstances, oneshouldnt need to edit this file.

    MainWindow.xib This contains the visual information of our main window. If double clicked, it will open in aprogram called Interface Builder. We will get to this a little later. Just on thing to note is this file does not contain

    any code. RootViewController.h, RootViewController.m- These are files for a view controller that gets added to ourmain window. For example, Apple has already created a simple interface when you clicked on

    Navigation-Based Application. Since most navigation-based applications use a Table View, Apple also provides aTable View for us to use.

    RootViewController.xib This is a view that would be used by us to add UI components. We will be displayingour Hello World text in one such component added.

    Create View Based Project Hello World Example

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    26/65

    26

    Create View Based Project Hello World Example

    26

    Apple have added the main window for us to edit along with the code to launch the window. In iPhone terms, each window is called aView. Since only one view can be seen at a time, a

    new view needs to be created for each screen in an application.

    In this example, we will be sticking to editing the view Apple has provided us. Go ahead andclickBuild and Go.

    Run Simulator

    Click on the Build and Go button at the top

    of Xcode. The program should compile and launch the

    iPhone Simulator (see picture). Make sure the drop-down on the top left

    says Simulator | Debug, this tells Xcodethat target is under test for iPhonesimulator.

    -> Now lets add some UI components to our view byediting the nib file ViewBasedViewController.xibin the Interface Builder.

    C i d j ll ld l

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    27/65

    27

    Create View Based Project Hello World Example

    27

    Adding UI Elements using interface builder

    Adding UI Elements to Home Screen

    Double click on ViewBasedViewController.xib. This is a nib file that opens with Interface Builder. ViewBasedViewController.xib contains information

    regarding the layout and controls of our view. (see picture).

    Interface builder

    A few notes about interface builder

    LibraryThe right-most window contains all of your

    controls that you can add to your view. For this tutorial

    we will be using a Label.

    The next window to the left of that contains objects

    that we will connect our interface to.View represents

    the view of this nib file (basically the interface). Files

    Owner is the object that links the interface to thecode.

    View - This is your user interface for your iPhone

    application. This window is where you will drop controls

    from the right-most window.

    AttributesThis is where we will set the styling

    properties of our controls.

    C t Vi B d P j t H ll W ld E l

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    28/65

    28

    Create View Based Project Hello World Example

    Add a Label

    Drag a Label from the from the library box on to your view window. You will see

    some blue lines to guide you. These are in place to help line up controls as well as

    center them.

    Once you have added the Label to theView,move it around until its in a position

    that you are happy with.

    After you have added the Label, click on it, and then clickTools ->Attributes

    Inspector (see pic).

    In the Text field type in Hello World.

    Now press Apple-S to save this file. We are done with Interface Builder so you canclose it. The application is now ready to be executed.

    Execute code

    Go ahead and clickBuild and Go again to launch the iPhone

    Simulator. Your screens should look something like this. This is a simple iPhone application designed solely with the help of

    Interface Builder. The next step in iPhone application development is to Connect the

    Code to An Interface Builder View. That would be taken up inthe next example.

    C t C d t A I t f B ild

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    29/65

    29

    This example would demonstrate how to create an UI using Interface Builder and connect it to thecode. We will create UITextField, UILabel, and a Button.Heres how the application will work:

    The user will tap inside a text box which brings up the iPhones keyboard.

    The user will type their name using the keyboard.

    The user will press a button.

    The label will update with a greeting containing that users name.

    If the user fails to enter in text, the label will say something like Please Enter Your Name.

    This example would incorporate the following activities:

    Create a new View-Based application

    Create a simple user interface

    Write code to connect to an interface

    Connect the interface to the code Update the interface based on user interaction.

    We are learned, how to create a view based application in Hello World Example. Here, we woulduse same as our base application for further development.

    29

    Connect Code to An Interface Builder

    C t C d t A I t f B ild

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    30/65

    30

    Create a simple user interface

    Modify the Hello World Exampleas given in following steps:

    Open Interface Builder.

    OpenAttributes Inspector for Label andchange the text Hello World to Enter yourname above (or below, as per the Label position with respect to Text Field for name).

    Now we need to add other screen elements, viz. , a Text Field, and a Button.

    Add a Text Field

    The basic steps are same as in case of adding a Label. The first thing to do is drag a Text Fieldfrom the library box on to your view window. A set of blue lines will help to line up controls aswell as center them.

    Once you have added the Text Field to theView,move it around until its in a suitable position.Next, stretch each side of the text box so that it spans across almost the entire view area. (The

    blue lines on the right and left will let you know when to stop.) Now we are going to set some of the attributes of the Text Field. If theAttributes Inspector

    doesnt appear, click on the Text Field and then clickTools ->Attributes Inspector.

    30

    Connect Code to An Interface Builder

    C t C d t A I t f B ild

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    31/65

    31

    Add a Text Field Attributes

    In the Placeholder field type in Name. This is the defaulttext that appears in the Text Field before a user typesanything.

    For Capitalize select WordsThis tells XCode that wewant to capitalize each word.

    For Return Key Select Done. This makes the return keyon the keyboard say Done rather than return.

    Also, make sure Clear When Edit Begins is checked

    31

    Connect Code to An Interface Builder

    Add a Button

    Drag a Button from the library ontoyour view.

    Stretch it and position it however youwould like.

    Add some text to the Button. Click onthe button and then clickTools ->Attributes Inspector.

    In the Titlefield, type Display.

    C t C d t A I t f B ild

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    32/65

    3232

    Connect Code to An Interface Builder

    We are now done creating our interface.It should look something like this.

    C t C d t A I t f B ild

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    33/65

    33

    Example 2 : Connection CodeThe files that link an interface to some code are called View Controllers. Lets open up

    ViewBasedViewController.h. This is the file where we will declare all of our interface

    variables. Add the following code to youViewBasedViewController.h.

    33

    Connect Code to An Interface Builder

    Connect Code to An Interface Builder

    http://icodeblog.com/wp-content/uploads/2008/07/viewbasedcontrollerh.png
  • 7/31/2019 iPhone iLearn Beginners Developer Course

    34/65

    34

    Example 2 : Connection CodeCode Explanation:

    Interface Builder uses IBOutlets and IBActions to connect to the code.

    IBOutlet UITextField *textNamecreates an outlet to connect to the text field we created in

    interface builder. We use this variable to get information from the text field.

    IBOutlet UILabel *lblHelloAn outlet that connects our label on our interface to the code.

    This variable is used to set the value of the label.

    Now that we have declared these variables, we need to make them properties.

    The purpose of making declaredVariables as Properties is to allow us to set certain attributes

    that are associated with the variables.

    Retain tells the compiler that we will handle the memory management of this object (dont forget

    to release it when you are done). Otherwise it will get cleaned after being instantiated.

    - (IBAction) updateText:(id) sender; =>This is the function that will get called when the

    user presses the button that was created in Interface Builder. We are simply declaring it here in

    the header file. We will implement this function in later slides. Now, we need to connect the

    interface to the code.

    34

    Connect Code to An Interface Builder

    Connect Code to An Interface Builder

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    35/65

    35

    Connect the interface to the code

    Double click on ViewBasedViewController.xib again to open up Interface Builder.

    Connect theView Click anywhere on the background of your view (anywhere that is not the Text Field, Label,

    or Button).

    ClickTools -> Connections Inspector. Next to New Referencing Outlet, you will see acircle.

    Click in that circle and drag the blue line to

    the Files Owner object and release it.

    The word view should pop up. Click on it to make connection.

    35

    Connect Code to An Interface Builder

    Connect Code to An Interface Builder

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    36/65

    36

    Example 2 : Connect Interface to CodeConnect the Text Field

    Click on the Text Field in yourView window toselect it.

    ClickTools -> Connections Inspector. You willnow see a circle next to New ReferencingOutlet.

    Click in that circle and drag it over to the FilesOwner object.

    A message will pop up with txtName. Click ontxtName and the connection is made. Youshould now see:

    36

    Connect Code to An Interface Builder

    Connect Code to An Interface Builder

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    37/65

    37

    Connect the Label

    Click on the Label in yourView windowto select it.

    ClickTools -> Connections Inspector.You will now see a circle next to NewReferencing Outlet.

    Click in that circle and drag it over to theFiles Owner object. A message will pop

    up with lblHello. Click on lblHello and the connection is

    made. You should now see:

    37

    Connect Code to An Interface Builder

    Connect Code to An Interface Builder

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    38/65

    38

    Connect the Button

    Click on the Button in yourView windowto select it.

    ClickTools -> Connections Inspector.You will now see a circle next to TouchUp Inside. This is the trigger that getscalled when a user presses the button.

    Click in that circle and drag it over to theFiles Owner object. A message will popup with updateText.

    Click on updateText and the connectionis made. You should now see:

    38

    Connect Code to An Interface Builder

    Now all of the connections should beset up. Go ahead and close

    Interface Builder.

    Update UI on events

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    39/65

    39

    Open up the fileViewBasedViewController.m . This is the file where we will implement the

    updateText function. Add the following code. Code is explained in following slides.

    39

    Update UI on events

    Update UI on events

    http://icodeblog.com/wp-content/uploads/2008/07/viewbasedcontroller.png
  • 7/31/2019 iPhone iLearn Beginners Developer Course

    40/65

    40

    Line wise Code Explanation:

    @synthesize txtName,lblHello; => Most of the time when creating (private) variables, getterand setter methods need to be specified. Theses functions get the value of a variable and set thevalue of a variable. What synthesize does is creates these methods.

    Next we will implement our updateText method. Here, it is started by creating a temporary string.This is the string that we will insert into the text of the label.

    The next line checks to see if the user has entered any text into the Text Field. txtName.text returnsan NSString.

    We are simply calling the length method on a string. If the length is 0, then obviously the user hasnot entered any text. If this is the case, we set the temporary string to Please enter your name:instructing the user to enter in their name.

    If the user has entered in some text, a new string is allocated. The initWithFormat method is similarto printfin C. So, the string Hello %@! is used. The %@ in the string means we will besubstituting it for a string. To get the value of the Text Field we again use the txtName.textproperty.

    Finally, the value of the Label is set by calling lblHello.text. This calls the text property of labeland sets the text to our temporary string variable.

    The last line [text release]; releases the temporary text field from memory. This is necessary towrite an efficient iPhone application. If this step is omitted, it would lead to iPhone apps crash.

    40

    Update UI on events

    Thats it!

    ClickBuild and Go.

    Executing code

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    41/65

    41

    ClickBuild and Go. The application should launch in the iPhone Simulator. When the inside of the TextField clicked, it should bring up the iPhones keyboard (you can also type with your keyboard). Enter inyour name and click Display. Here are a few screens of what your app should look like. (followingslides...)

    41

    Executing code

    Case1:-UserClick

    sDisplaywithouttypingintheirname.

    Case2

    :-Usertyp

    esintheirnameand

    clicksDisplay.

    Analysis

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    42/65

    42

    After studying examples, we are now aware

    how to develop a view based application.

    how to add components to UI through Interface builder.

    Write code to connect to an interface.

    Connect the interface to the code A few ways to update the interface based on user interaction.

    In the next example, we would learn about a simple navigation basedapplication.

    42

    Analysis

    Navigation Based Application

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    43/65

    43

    This example, again, is a Hello World application. However this time, the application design has

    been modified. In this example we would learn how to:

    Create a New Navigation-Based Application.

    Update the UITableView Cells to Display Hello World Text.

    43

    Navigation Based Application

    Create Project

    Create a New Navigation-BasedApplication

    Click Xcode > New Project anda window should pop up (seepicture).

    Make sureApplication isselected under iPhone OS.

    Select Navigation-Based

    Application and Choose. Name this project as Hello

    World.

    Navigation Based Application

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    44/65

    4444

    Click on the Build and Go button at the

    top of Xcode.

    It shows is the Table View that Apple hasadded for us (see picture).

    Table View is added by default forNavigation based Application.

    We would add a row to this table view,

    and display Hello World in it.

    Update UITableView Cells

    Open RootViewController.m file. This is the view controller that Apple added to our main view. This files, in this case, would contain functions that have been overridden from the Table View super class.

    Since we are editing a table, all of these functions will be related to editing a table. Find the function called numberOfRowsInSection.

    This function tells the application how many rows are in our table. Currently, it returns 0. Lets change that to return1. This will tell the application that we want 1 row in our table.

    Create a New Navigation-Based Application (continued)

    Navigation Based Application

    Navigation Based Application

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    45/65

    4545

    Navigation Based Application

    Update UITableView Cells (continued)

    Locate the function cellForRowAtIndexPath.

    This function gets called once for every row. This is where we define what content to display in a given

    row. In this case we want the row to be a string that says Hello World.

    Navigation Based Application

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    46/65

    4646

    Navigation Based Application

    Update UITableView Cells (continued)

    cellForRowAtIndexPathfunction creates a new cell object and returns it.

    The code between the if(cell==null) block checks to see if we have created a cell before, if not build anew cell, otherwise use the one we created before.

    Right before the // Set up the cell comment add the following code: [cell setText:@"Hello World"];.

    We are basically calling the setTextmethod of the cell object and pass in the string Hello World. Please

    refer toApples Objective-C overview for string syntax details.

    Thats it! Click the Build and Go buttonagain to launch the iPhone simulator.

    Navigation Based Application

    http://icodeblog.com/wp-content/uploads/2008/07/helloworld.pnghttp://icodeblog.com/wp-content/uploads/2008/07/helloworld.pnghttp://icodeblog.com/wp-content/uploads/2008/07/helloworld.pnghttp://icodeblog.com/wp-content/uploads/2008/07/helloworld.pnghttp://icodeblog.com/wp-content/uploads/2008/07/helloworld.pnghttp://icodeblog.com/wp-content/uploads/2008/07/helloworld.pnghttp://icodeblog.com/wp-content/uploads/2008/07/helloworld.pnghttp://icodeblog.com/wp-content/uploads/2008/07/helloworld.pnghttp://icodeblog.com/wp-content/uploads/2008/07/helloworld.pnghttp://icodeblog.com/wp-content/uploads/2008/07/helloworld.pnghttp://icodeblog.com/wp-content/uploads/2008/07/helloworld.pnghttp://developer.apple.com/iphone/gettingstarted/docs/objectivecprimer.actionhttp://icodeblog.com/wp-content/uploads/2008/07/helloworld.pnghttp://developer.apple.com/iphone/gettingstarted/docs/objectivecprimer.actionhttp://developer.apple.com/iphone/gettingstarted/docs/objectivecprimer.actionhttp://developer.apple.com/iphone/gettingstarted/docs/objectivecprimer.action
  • 7/31/2019 iPhone iLearn Beginners Developer Course

    47/65

    47

    Execute

    Click the Build and Go.

    You should now see a screen as shown (see picture).

    This example would now be used as a base for creatinga Navigation Based application involving more then oneview.

    47

    Navigation Based Application

    Adding Sub-View to UITableView

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    48/65

    48

    In the previous example, we learned about navigation based application. Using that knowledge, letus now design a navigation based application, with more then one view.

    In this application we would learn to :

    Add a Sub-view to a UITableView. This sub-view will consist of three key elements.

    A Header File (*.h file extension)

    An Implementation File (*.m file extension)

    An Interface File (*.xib extension)

    48

    Adding Sub View to UITableView

    Create Project

    Create a New Navigation-Based Application

    This has already been explained in last example. Name this project as BasicNavigation. Once again note that Table View is added by default for Navigation based Application.

    Adding Sub-View to UITableView

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    49/65

    49

    Adding a Sub-view (Designed in Interface Builder) to a UITableView

    Open XCode.

    Right-click on the Classes folder within the project area on the left,

    and selectAdd,

    then New File.

    You will be presented with the

    following dialogue(see picture).

    49

    Adding Sub View to UITableView

    Adding Sub-View to UITableView

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    50/65

    50

    Adding a Sub-view to a UITableView (continued)

    Make sure Cocoa Touch Classes is selected on the left and UIViewController

    subclass is selected as the file template, and click Next (see picture in last slide).

    A dialog box is displayed (see picture).

    Name this file

    SubViewOneController.m.

    Make sure the checkbox for

    Also create

    SubViewOneController.h

    is checked.

    ClickFinish.

    50

    Adding Sub View to UITableView

    Adding Sub-View to UITableView

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    51/65

    51

    Adding a Sub-view to a UITableView (continued)

    The contents of the project window should now look similar to the illustration as

    shown (see picture).

    51

    Adding Sub View to UITableView

    Adding Sub-View to UITableView

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    52/65

    52

    Adding Contents to Sub-View

    Now we would create a user interface to go with our view controller code.

    Steps to create a User Interface has been given in previous examples.

    Drag a UILabel and a UIButton such that our view mimics the following interface.

    52

    Adding Sub View to UITableView

    Adding Sub-View to UITableView

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    53/65

    53

    Connecting Sub-View to UITableView

    At this point weve created our first sub-view, however were not going to be able to get to it from

    application just yet.

    Before moving on, we should go back into XCode and click on Build before moving onto the nextpart. The build should compile and the results should report back successfully at this point.

    Navigating to a Sub-View from a UITableView

    The code below will do the following:

    Sets up an array of views and supplies that array to our UITable as a data source

    Adds multiple sub views to our application using the sub view weve designed and written thus far

    Allows us to navigate to different sub views upon view selection from our UITableView

    Open the file RootViewController.h and add the following code:

    53

    Adding Sub View to UITableView

    Adding Sub-View to UITableView

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    54/65

    54

    Navigating to a Sub-View from a UITableView

    Open the file RootViewController.m and add the following code:

    54

    Adding Sub View to UITableView

    Adding Sub-View to UITableView

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    55/65

    55

    Navigating to a Sub-View from a UITableView (Add Code )

    55

    dd g Sub e to U ab e e

    That completes our navigation code.

    Adding Sub-View to UITableView

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    56/65

    5656

    g

    Click theBuild and Gobutton in XCode. Our applicationthus far should now allow us to select a sub view andnavigate to it. The application should look as shown here.

    E x e c u t e

    Windows Based Application

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    57/65

    57

    Windows Based Application

    The difference between View-based and Window-based templates:

    A View template gives you a Window template with aUIView already added and configured to display in thewindow, while the Window template gives you a blankslate with no default view set up.

    UIWindow Class

    Windows in this context essentially provide a surfacearea on the screen onto which the application canpresent information and controls to the user.

    The user does not see or interact directly with theUIWindow object.

    These windows may be created programmatically,but are typically created automatically by Interface

    Builder when you design your user interface.

    View Types

    The Window: The UIWindow is the root view of theview hierarchy and provides the surface on which allsubviews draw their content.

    Container Views: Container views enhance thefunctionality of other view objects. The UIScrollViewclass, for example, provides scrollbars and scrollingfunctionality for the UITableView and UITextViewclasses.

    Controls: The controls category encompasses viewsthat both present information and respond to userinteraction.

    Display Views: Display views do not respond touser interaction. Examples of views in this categoryinclude the UILabel and UIImageView classes.

    Alert Views: The UIAlertView class displays a bluepopup box on the screen and the UIActionSheet causesa panel to slide up from the bottom of the screen.

    Deploying Application on iPhone Device

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    58/65

    5858

    p y g pp

    Certificate & Provisioning files

    To deploy the application on iPhone device you must have valid certificate orauthorization to compile and run your application.

    In iPhone terminology they are called as

    Steps required to get the certificate.

    Go to http://developer.apple.com/iphone/index.action and register your self

    with valid user.

    login & go to link @iPhone Provision Portal

    Follow the steps as described on the portal to generate certificate from your

    Mac machine and Provision file required to run your application in iPhone

    device.

    Deploying Application on iPhone Device

    http://developer.apple.com/iphone/index.actionhttp://developer.apple.com/iphone/index.action
  • 7/31/2019 iPhone iLearn Beginners Developer Course

    59/65

    5959

    p y g pp

    Generating a Certificate

    To request an iPhone Development Certificate, you first needto generate a Certificate Signing Request (CSR) utilizing the

    Keychain Access application in Mac OS X Leopard. The creationof a CSR will prompt Keychain Access to simultaneouslygenerate your public and private key pair establishing youriPhone Developer identity. Private key is stored in the loginKeychain by default & can be viewed in the Keychain Accessapplication under the Keys category. To generate a CSR.

    In your Applications folder, open the Utilities folder and launchKeychain Access.

    In the Preferences menu, set Online Certificate Status Protocol (OSCP) and Certificate RevocationList(CRL) to Off.

    Choose Keychain Access -> Certificate Assistant -> Request a Certificate from a Certificate Authority.Note: If you have a noncompliant private key highlighted in the Keychain during

    this process, the resulting Certificate Request will not be accepted by theProvisioning Portal. Confirm that you are selecting Request a Certificate From aCertificate Authority... and not selecting Request a Certificate From a Certificate

    Authority with

    In the User Email Address field, enter your email address. Please ensure that the email addressentered matches the information that was submitted when you registered as an iPhone Developer.

    Deploying Application on iPhone Device

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    60/65

    6060

    p y g pp

    Generating a Certificate (contd)

    In the Common Name field enter yourname. Please ensure that the nameentered matches the information thatwas submitted when you registeredas an iPhone Developer.

    No CA (Certificate Authority) EmailAddress is required. The Requiredmessage will be removed after

    completing the following step.

    Select the Saved to Disk radio buttonand if prompted, select Let mespecify key pair information and click

    Continue.

    If Let me specify key pair was selected, specify a file name and click Save. In the following screen

    select 2048 bits for the Key Size and RSA for the Algorithm. Click Continue.

    The Certificate Assistant will create a CSR file on your desktop.

    Uploading Certificate for Approval and Approving Certificate.

    After creating a CSR, log in to the iPhone Provisioning Portal and navigate to Certificates >Development and click Add Certificate.

    Deploying Application on iPhone Device

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    61/65

    6161

    p y g pp

    Generating a Certificate (contd)

    Click the Choose file button, select your

    CSR and click Submit. If the Key Size wasnot set to 2048 bits during the CSR creationprocess, the Portal will reject the CSR.

    Once your CSR is approved or rejected, youwill get information by email.

    Now Download and Install Development

    Certificates by simple double clicking that fileon Mac machine.

    Deploying Application on iPhone Device

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    62/65

    6262

    p y g pp

    Generating a Certificate (contd)

    Click the Choose file button, select your

    CSR and click Submit. If the Key Size wasnot set to 2048 bits during the CSR creationprocess, the Portal will reject the CSR.

    Once your CSR is approved or rejected, youwill get information by email.

    Now Download and Install Development

    Certificates by simple double clicking that fileon Mac machine.

    Deploying Application on iPhone Device

  • 7/31/2019 iPhone iLearn Beginners Developer Course

    63/65

    6363

    Run the application

    One of the main factorsthat determine how

    Xcode builds yourapplication is the SDKused to build it. iPhoneSDKs come in twofamilies: one to targetdevices and one totarget iPhone

    Setting Your Code Signing Identity

    When you build your application to run it on a device, Xcodesigns it with a development certificate (also known as a code

    signing identity) stored on your keychain. To learn how to obtainand install development certificates, seePreparing Your Mac foriOS Development.

    The Code Signing Identity build setting specifies the code signingidentity Xcode uses to sign your binary. Xcode looks for codesigning identities in your default keychain. Below snap shotshows an example set of options for the Code Signing Identitybuild setting obtained from the login (default) keychain (the

    name of the default keychain appears bold in the Keychains list).

    Specify the SDK Xcode uses to build yourapplication in your projects Base SDK buildsetting, which you can set in the Generalpane of the Project Info window or in the

    Build pane of the Project or Target Infowindow.

    Simulator. Applications built using an SDK from theiPhone Device SDK family can run only on iOS-

    based devices. Conversely, applications built usingan SDK from the iPhone Simulator SDK family runonly in iPhone Simulator.

    Deploying Application on iPhone Device

    http://developer.apple.com/iphone/library/documentation/xcode/conceptual/iphone_development/128-Managing_Devices_and_Digital_Identities/devices_and_identities.htmlhttp://developer.apple.com/iphone/library/documentation/xcode/conceptual/iphone_development/128-Managing_Devices_and_Digital_Identities/devices_and_identities.htmlhttp://developer.apple.com/iphone/library/documentation/xcode/conceptual/iphone_development/128-Managing_Devices_and_Digital_Identities/devices_and_identities.htmlhttp://developer.apple.com/iphone/library/documentation/xcode/conceptual/iphone_development/128-Managing_Devices_and_Digital_Identities/devices_and_identities.htmlhttp://developer.apple.com/iphone/library/documentation/xcode/conceptual/iphone_development/128-Managing_Devices_and_Digital_Identities/devices_and_identities.htmlhttp://developer.apple.com/iphone/library/documentation/xcode/conceptual/iphone_development/128-Managing_Devices_and_Digital_Identities/devices_and_identities.htmlhttp://developer.apple.com/iphone/library/documentation/xcode/conceptual/iphone_development/128-Managing_Devices_and_Digital_Identities/devices_and_identities.html
  • 7/31/2019 iPhone iLearn Beginners Developer Course

    64/65

    6464

    These are the possible values for the Code Signing Identity build setting

    Dont Code Sign.Choose this option if you dont want to sign your binary. Note that with this value,

    when you build your applications using the iPhone Device SDK the build fails.

    Automatic Profile Selectors.These choices look for an identity whose name starts with iPhoneDeveloper or iPhone Distribution.

    A code signing identity. A specific code signing identity. The code signing identities in your defaultkeychain are listed under the name of the provisioning profile under which you downloaded themfrom the iPhone Provisioning Portal. Expired or otherwise invalid identities are dimmed and cannot be

    chosen.

    Xcode project templates are configured to use the iPhone Developer automatic selector.

    Refer to http://mobiforge.com/developing/story/deploying-iphone-apps-real-devices for moreinformation on How to deploy iPhone App on device

    Build and run the application on device Happy Coding.

    Thank You!

    http://mobiforge.com/developing/story/deploying-iphone-apps-real-deviceshttp://mobiforge.com/developing/story/deploying-iphone-apps-real-deviceshttp://mobiforge.com/developing/story/deploying-iphone-apps-real-deviceshttp://mobiforge.com/developing/story/deploying-iphone-apps-real-deviceshttp://mobiforge.com/developing/story/deploying-iphone-apps-real-deviceshttp://mobiforge.com/developing/story/deploying-iphone-apps-real-deviceshttp://mobiforge.com/developing/story/deploying-iphone-apps-real-deviceshttp://mobiforge.com/developing/story/deploying-iphone-apps-real-deviceshttp://mobiforge.com/developing/story/deploying-iphone-apps-real-deviceshttp://mobiforge.com/developing/story/deploying-iphone-apps-real-devices
  • 7/31/2019 iPhone iLearn Beginners Developer Course

    65/65

    Happy Learning!