iPhone Programming [2/17] : Introduction to iOS Programming
1. Introduction to iOS Programming
Dr. Thanisa Kruawaisayawan
www.imcinstitute.com
2. Get Started
• What are needed?
• Tools:
• Computer with Mac OSX (Intel based and Mountain Lion)
• Xcode 4.5.2
• iOS Developer Program (optional)
• Knowledge and Creativity
• Time
2
3. iOS Developer Programs
• iOS Developer Program ($99/year)
• To distribute apps on the App Store as an individual, sole proprietor, company or
organization (up to 100 registered devices)
• iOS Developer Enterprise Program ($299/year)
• To develop proprietary apps for internal distribution within your company,
organization, government entity or educational institution
• iOS Developer University Program (Free)
• To introduce iOS development into curriculum (up to 200 students)
3
6. Workspace Window of Xcode
6
Source: http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iPhone101/Articles/01_CreatingProject.html
7. What Files Are In The Project?
• Source Files
• AppDelegate.h & AppDelegate.m
• ViewController.h & ViewController.m
• ViewController.xib
• Supporting Files
• HelloWorld-Info.plist
• main.m
7
8. iOS Application Life Cycle
Souce: http://developer.bada.com/article/A-Comparison-between-iOS-and-bada-Application-Development-Part1 8
9. main() => UIApplicationMain()
• The @autoreleasepool statement supports the Automatic Reference Counting
(ARC) system. ARC provides automatic object-lifetime management for your
app.
• The call to UIApplicationMain creates an instance of the
UIApplication and AppDelegate classes.
9
12. IB, XIB, and NIB Files
• Interface Builder (IB) is a GUI builder and object editor.
• Developer creates and configures objects, and then saves them to an archive.
• This archive or container is called “XIB” or “NIB”.
• We can have more than one xib/nib file in the project
ViewController.xib
12
13. XIB and NIB Files
• A XIB file is an XML representation of objects and their instance variables,
and it is compiled into a NIB file when application is built.
• XIB file is easier to work with, but NIB file is smaller and easier to parse.
• NIB file is copied into application bundle (a directory containing the
executable and any resources)
13
14. Application Bundle on the Simulator
• ~/Library/Application Support/iPhone Simulator/6.0/Applications
• Right Click at HelloWorld.app > Show Package Contents
chflags nohidden ~/Library/
14
27. Assistant View
• Prior to Xcode 4, outlets and actions needed to be created in the view
controller’s header file before connecting them in Interface Builder (IB).
• Xcode 4’s assistant view gives us a much faster and more intuitive approach
that lets us create and connect outlets and actions simultaneously.
27
30. Exercise: HelloWorld4
• Add text field to get the name of user to say “Hello “ user or say “Hello World”
when there is no name in the text field
The same
method for
two
buttons?
30
31. Info Property List File
• Name: <Project Name>-Info.plist
• It contains a list of key-value pairs which is used to specify things such as
• Icon to display on the home screen
• Default language of the application
31
33. App Icon and Launch Image for Your Application
33
34. Prepare your files
Launch image
Display type App icon size Icon name Image name
size
Non-Retina
57 x 57 Icon.png 320 x 480 Default.png
display
Retina Display 114 x 114 Icon@2x.png 640 x 960 Default@2x.png
34
39. Code Completion
• Type initial character, then Pop-up Windows comes up automatically
• Keep typing to complete
• Press TAB to move to next Camel string
• Press Enter to complete that selection
• Press Arrow up/down to navigate
39
42. Warning !!
• iOS programming doesn’t have garbage collection (unlike Mac OS
programming)
• So, we have to keep track of what we need.. what we no longer need.
42
43. Memory Management & String (Before iOS 5)
Scenario: create an NSString object
1. Create a pointer to NSString object
NSString *str1;
2. Allocate NSString object and set the pointer to object
str1 = [NSString alloc];
3. Initialize the object that the pointer points to
str1 = [str1 initWithFormat:@"Hello"];
43
45. Memory Management & String (Before iOS 5)
• To get current retain count :
[obj retainCount];
Retain Count
• To increase retain count :
[obj retain];
Hello NSString Obj
• To decrease retain count :
[obj release];
45
46. Memory Management & String (Before iOS 5)
There is another NSString object named str2 which points to a new object
with message “Ha ha”.
NSString *str2 = [[NSString alloc] initWithFormat:@"Ha ha"];
Then reallocate str1 to point to the same object as str2
str1 = str2;
46
47. Memory Management & String (Before iOS 5)
1 Retain Count
This memory
space is wasted
Hello NSString Obj
str1
str2
Ha ha NSString Obj
Q: What is the retain count for the object with message “Ha ha”?
47
48. Memory Management & String (Before iOS 5)
0 Retain Count
• How can we do the proper set?
Hello
2 Retain Count
str1
str2 Ha ha NSString Obj
48
55. Memory Management & Array (Before iOS 5)
In many cases
• You want to have an array.
• You add objects to the array.
• When the array is destroyed, you want the members to be destroyed as well..
55
64. AutoReleasePool
• What method returns object in autorelease pool ?
NSXyz *a = [NSXyz xyzWith...];
• This is called “Convenience method”.
64
65. @property (Before Xcode 4.2)
@property (attributes) type name;
• The attributes are divided into groups and can take the following values :
• Access type
• readwrite (default) : will generate getter and setter
• readonly : will only generate getter
• Memory management
• assign (default) : setter will not retain memory, just assign pointer
• retain : setter will retain memory
• copy : setter will copy memory zone (thus calling alloc and thus retain the new copy)
• Thread management
• nonatomic : if nonatomic is not mentioned, the setter will be thread-safe by default
(synchronized method)
65
69. ARC
• ARC is a compiler feature that provides automatic memory management of
Objective-C objects. ARC works by adding code at compile time to ensure
that objects live as long as necessary, but no longer. Conceptually, it follows
the same memory management conventions as manual reference counting by
adding the appropriate memory management calls for you.
• LLVM 3.0 compiler that Apple started shipping with iOS 5 is so smart that it
will release objects for us, using ARC. That means no more dealloc methods,
and no more worrying about calling release or autorelease.
• ARC applies to only Objective-C objects, not to Core Foundation objects or
to memory allocated with malloc.
69
80. Storyboarding
• In Xcode 4.2, the Interface Builder user interface for iOS applications is based
on the storyboarding of view controllers.
• Storyboarding enables you to use Interface Builder to specify not only all the
screens in your application, but the transitions between them and the controls
used to trigger the transitions.
• Thus you can lay out every possible path through your application graphically,
greatly reducing the amount of code you need to write for a complex
multiscreen application.
80
81. New a Single View Application > HelloWorld_Storyboard
81