• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
iPhone Programming [2/17] : Introduction to iOS Programming
 

iPhone Programming [2/17] : Introduction to iOS Programming

on

  • 812 views

Presentation for developing iPhone Applications

Presentation for developing iPhone Applications

Statistics

Views

Total Views
812
Views on SlideShare
812
Embed Views
0

Actions

Likes
2
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    iPhone Programming [2/17] : Introduction to iOS Programming iPhone Programming [2/17] : Introduction to iOS Programming Presentation Transcript

    • Introduction to iOS Programming Dr. Thanisa Kruawaisayawan www.imcinstitute.com
    • 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
    • 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
    • iOS > Application > Single View Application 4
    • Product Name: HelloWorld 5
    • Workspace Window of Xcode 6Source: http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iPhone101/Articles/01_CreatingProject.html
    • 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
    • iOS Application Life Cycle Souce: http://developer.bada.com/article/A-Comparison-between-iOS-and-bada-Application-Development-Part1 8
    • 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
    • AppDelegate.m 10
    • Run and See Result 11
    • 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
    • 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
    • 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
    • iOS Simulator > Reset Content and Settings... 15
    • XIB and NIB Files 16
    • ViewController.xib • Drag Label onto screen 1 3 2 17
    • ViewController.xib• Change text to Hello World and click Run 2 1 18
    • HelloWorld (Result) 19
    • HelloWorld2 (Result)• When a user clicks a button “Say Hello”, “Hello World” is shown on the screen. 20
    • ViewController.xib• Drag Round Rect Button onto screen and delete text of Label 21
    • ViewController.h 22
    • ViewController.m 23
    • ViewController.xib• Link label to Label 24
    • ViewController.xib• Link sayHello to Button 25
    • Exercise: HelloWorld3• Add another button to clear the message “Hello World” 26
    • 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
    • ViewController.xib to ViewController.h• Control + Drag from an Object in IB to Header File 28
    • ViewController.xib• Can “Say Hello” and “Clear” buttons call to the same action? 29
    • 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
    • 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
    • HelloWorld-Info.plist 32
    • App Icon and Launch Image for Your Application 33
    • 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
    • HelloWorld Summary• Drag “Icon.png” and “Icon@2x.png” 35
    • HelloWorld-Info.plist 36
    • HelloWorld Summary• Drag “Default.png” and “Default@2x.png” 37
    • How to Change iOS Simulator 38
    • 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
    • Exercise (Results) 40
    • Exercise: Sample of Data • @"Ant" • @"Insect" • @"Bee" • @"Jelly Fish" • @"Cat" • @"Kingkong" • @"Dog" • @"Leopard" • @"Elephant" • @"Mountain Lion" • @"Fish" • @"Nightingale" • @"Goldfish" • @"Owl" • @"Hippo" • @"Pig" 41
    • 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
    • Memory Management & String (Before iOS 5)Scenario: create an NSString object1. 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
    • Memory Management & String (Before iOS 5) 1 Retain Count str1 Hello NSString Obj 44
    • 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
    • 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
    • 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
    • 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
    • Memory Management & Array (Before iOS 5) 1 Retain Count 1 Retain Count Obj Array 49
    • Memory Management & Array (Before iOS 5) 2 Retain Count 1 Retain Count Obj Array 50
    • Memory Management & Array (Before iOS 5) 2 Retain Count 1 Retain Count Obj1 Array 1 Retain Count Obj2 51
    • Memory Management & Array (Before iOS 5) 2 Retain Count 1 Retain Count Obj1 Array 2 Retain Count If we release array ? Obj2 52
    • Memory Management & Array (Before iOS 5) 2 Retain Count RELEASE | -1 0 Retain Count Obj1 Array 2 Retain Count RELEASE | -1 Obj2 53
    • Memory Management & Array (Before iOS 5) 1 Retain Count 0 Retain Count Obj1 Array 1 Retain Count Obj2 54
    • 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
    • Memory Management & Array (Before iOS 5) 1 Retain Count 1 Retain Count Obj Array 56
    • Memory Management & Array (Before iOS 5) 2 Retain Count 1 Retain Count Obj Array RELEASE | -1 57
    • Memory Management & Array (Before iOS 5) 1 Retain Count 1 Retain Count Obj Array 58
    • Memory Management & Array (Before iOS 5) 1 Retain Count 1 Retain Count Obj1 Array 1 Retain Count Obj2 59
    • Memory Management & Array (Before iOS 5) 1 Retain Count 1 Retain Count Obj1 Array 2 Retain Count Obj2 RELEASE | -1 60
    • Memory Management & Array (Before iOS 5) 1 Retain Count 1 Retain Count Obj1 Array 1 Retain Count If we release array ? Obj2 61
    • Memory Management & Array (Before iOS 5) 0 Retain Count RELEASE | -1 0 Retain Count Obj1 Array 0 Retain Count RELEASE | -1 Obj2 62
    • Memory Management & Array (Before iOS 5) 0 Retain Count 0 Retain Count Obj1 Array 0 Retain Count Obj2 63
    • AutoReleasePool• What method returns object in autorelease pool ? NSXyz *a = [NSXyz xyzWith...];• This is called “Convenience method”. 64
    • @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
    • @synthesize• @synthesize name; • Example: @synthesize str1; -(NSString *) str1 { return str1; } -(void) setStr1:(NSString *) str2 { if (str1 != str2) { [str2 retain]; [str1 release]; [ str1 release]; str1 = str2; str1 = [str2 copy]; str1 = str2; } } retain copy assign 66
    • Some New Features Starting from Xcode 4.2• Automatic Reference Counting (ARC)• Default Compiler for iOS development • Xcode 4.5 : LLVM 4.1 • Xcode 4.4 : LLVM 4.0 • Xcode 4.3 : LLVM 3.1 • Xcode 4.2 : LLVM (Low Level Virtual Machine) 3.0 • Xcode 4.1 : LLVM-GCC • Xcode 4.0 : GCC (GNU Compiler Collection)• Storyboarding 67
    • Automatic Reference Counting (ARC) Source: http://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html 68
    • 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
    • Manual Reference Counting Source: http://teamtreehouse.com/library/ios-5-foundations/automatic-reference-counting/getting-started/play 70
    • Automatic Reference Counting 71
    • Comparing Manual and Automatic Reference Counting 72
    • Strong and Weak properties• @property (retain) MyClass *myObject; => @property (strong) MyClass *myObject;• @property (assign) MyClass *myObject; => @property (weak) MyClass *myObject; 73
    • Convert Old Codes to Objective-C ARC Codes• Open HelloWorld project developed in Xcode 4.0 74
    • Changes in HelloWorldAppDelegate.h After Converting Before Converting 75
    • Changes in HelloWorldAppDelegate.m After Converting Before Converting 76
    • Changes in main.m After Converting Before Converting 77
    • Strong propertyNSString *firstName = textField.text; Source: http://www.raywenderlich.com/5677/beginning-arc-in-ios-5-part-1 78
    • Weak property __weak NSString *weakName = textField.text; 79
    • 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
    • New a Single View Application > HelloWorld_Storyboard 81
    • MainStoryboard.storyboard 82
    • MainStoryboard.storyboard 83
    • References• Boonyanit Mathayomchan, Ph.D., “iPhone Application Development (BASIC)”, 2011• Chotipat Pornavalai, iPhone Basic #1, Mini Master of iOS Application #1• Dave Mark, et.al, “Beginning iOS5 Development: Exploring the iOS SDK”, 2012• Joe Conway & Aaron Hillegass, “iPhone Programming: The Big Nerd Ranch Guide”, 2010 84