Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to iOS Programming

Introduction to iOS Programming

My slides from 'Introduction to iOS Programming' workshop at P.A. AZIZ College of Engineering & Technology (PAACET), Thiruvananthapuram.

Gaurav Verma

April 10, 2013
Tweet

More Decks by Gaurav Verma

Other Decks in Programming

Transcript

  1. COURSE OBJECTIVES To introduce iOS Platform to the participants, iOS

    Platform Overview Objective-C Basics TableView Based Applications Utility Applications Tab Bar Applications Modal View Controllers Master-Detail Applications Embedding a Web Browser Parsing JSON Where to go from here ? Resources
  2. COURSE OBJECTIVES To introduce iOS Platform to the participants, iOS

    Platform Overview Objective-C Basics TableView Based Applications Utility Applications Tab Bar Applications Modal View Controllers Master-Detail Applications Embedding a Web Browser Parsing JSON Where to go from here ? Resources
  3. COURSE OBJECTIVES To introduce iOS Platform to the participants, iOS

    Platform Overview Objective-C Basics TableView Based Applications Utility Applications Tab Bar Applications Modal View Controllers Master-Detail Applications Embedding a Web Browser Parsing JSON Where to go from here ? Resources
  4. COURSE OBJECTIVES To introduce iOS Platform to the participants, iOS

    Platform Overview Objective-C Basics TableView Based Applications Utility Applications Tab Bar Applications Modal View Controllers Master-Detail Applications Embedding a Web Browser Parsing JSON Where to go from here ? Resources
  5. COURSE OBJECTIVES To introduce iOS Platform to the participants, iOS

    Platform Overview Objective-C Basics TableView Based Applications Utility Applications Tab Bar Applications Modal View Controllers Master-Detail Applications Embedding a Web Browser Parsing JSON Where to go from here ? Resources
  6. COURSE OBJECTIVES To introduce iOS Platform to the participants, iOS

    Platform Overview Objective-C Basics TableView Based Applications Utility Applications Tab Bar Applications Modal View Controllers Master-Detail Applications Embedding a Web Browser Parsing JSON Where to go from here ? Resources
  7. COURSE OBJECTIVES To introduce iOS Platform to the participants, iOS

    Platform Overview Objective-C Basics TableView Based Applications Utility Applications Tab Bar Applications Modal View Controllers Master-Detail Applications Embedding a Web Browser Parsing JSON Where to go from here ? Resources
  8. COURSE OBJECTIVES To introduce iOS Platform to the participants, iOS

    Platform Overview Objective-C Basics TableView Based Applications Utility Applications Tab Bar Applications Modal View Controllers Master-Detail Applications Embedding a Web Browser Parsing JSON Where to go from here ? Resources
  9. COURSE OBJECTIVES To introduce iOS Platform to the participants, iOS

    Platform Overview Objective-C Basics TableView Based Applications Utility Applications Tab Bar Applications Modal View Controllers Master-Detail Applications Embedding a Web Browser Parsing JSON Where to go from here ? Resources
  10. COURSE OBJECTIVES To introduce iOS Platform to the participants, iOS

    Platform Overview Objective-C Basics TableView Based Applications Utility Applications Tab Bar Applications Modal View Controllers Master-Detail Applications Embedding a Web Browser Parsing JSON Where to go from here ? Resources
  11. COURSE OBJECTIVES To introduce iOS Platform to the participants, iOS

    Platform Overview Objective-C Basics TableView Based Applications Utility Applications Tab Bar Applications Modal View Controllers Master-Detail Applications Embedding a Web Browser Parsing JSON Where to go from here ? Resources
  12. About iOS • iOS was first released in 2007. •

    Apple does not license iOS for installation on non-Apple hardware.
  13. About iOS • iOS was first released in 2007. •

    Apple does not license iOS for installation on non-Apple hardware. • More than 700,000 iOS applications which have collectively been downloaded more than 30 billion times.
  14. About iOS • iOS was first released in 2007. •

    Apple does not license iOS for installation on non-Apple hardware. • More than 700,000 iOS applications which have collectively been downloaded more than 30 billion times. • Accounts for more than 65% of mobile web data consumption.
  15. About iOS • uses multi-touch gestures like swipe, tap, pinch,

    reverse pinch. • Derived from OS X, with which it shares the Darwin foundation.
  16. About iOS • uses multi-touch gestures like swipe, tap, pinch,

    reverse pinch. • Derived from OS X, with which it shares the Darwin foundation. • Current Major Version : iOS6
  17. About iOS • uses multi-touch gestures like swipe, tap, pinch,

    reverse pinch. • Derived from OS X, with which it shares the Darwin foundation. • Current Major Version : iOS6 • Earlier called : iPhone OS. Rebranded as iOS in June 2010.
  18. Setup • To Get Started : • Download the latest

    version of Xcode (Free). XCode includes the iOS SDK. • Enroll as an Apple Developer in the iOS Developer Program. ( You do not need to enroll in the program to write apps and test them in iOS Simulator. You do need to enroll, however, to test apps on devices and to distribute apps.)
  19. Tools • Xcode provides tools to manage your entire development

    workflow : • creating your app • designing your user interface • Testing & Optimizing • Archive and submit your app to the App Store
  20. Language • Objective-C is an elegant object-oriented language that powers

    all iOS apps. • Objective-C is a simple language with syntax and conventions that are easy to learn. • Objective-C is a thin layer on top of C, and moreover is a strict superset of C.
  21. Basic Tasks • Create objects • Compare objects • Query

    objects for information about them • Access objects in collections such as arrays
  22. Frameworks • A framework contains a library of methods that

    your app can call. • An app consists of code that you write and frameworks provided by Apple. • The system frameworks are the only way to access the underlying hardware.
  23. Foundation Framework • Your apps, as well as UIKit and

    other frameworks, are built on the Foundation framework infrastructure. • Provides many primitive object classes and data types • Establishes conventions (for tasks such as deallocation) that make your code more consistent and reusable
  24. Foundation Framework • Create and manage collections, such as arrays

    and dictionaries • Access images and other resources stored in your app • Create and manage strings • Post and observe notifications • Create date and time objects • Automatically discover devices on IP networks • Manipulate URL streams • Execute code asynchronously
  25. UIKit Framework • All iOS apps are based on UIKit.

    • Use UIKit to : • Construct and manage your user interface • Handle touch- and motion-based events • Present text and web content • Optimize your app for multitasking • Create custom user interface elements
  26. Other Frameworks • Core Data • Core Graphics • Core

    Animation • OpenGL ES Framework • Core Location
  27. Other Frameworks • Core Data • Core Graphics • Core

    Animation • OpenGL ES Framework • Core Location • Social Framework
  28. Other Frameworks • Core Data • Core Graphics • Core

    Animation • OpenGL ES Framework • Core Location • Social Framework • StoreKit Framework and more ...
  29. Human Interface Design Follow the principles and conventions spelled out

    in iOS Human Interface Guidelines to design a superlative user interface and user experience for your product.
  30. Classes & Objects • Classes provide the building blocks to

    allow encapsulation of data and methods that act on that data. • Objects are specific instances of a class, and they contain their own instance data and pointers to the methods implemented by the class. • Classes are specified in two pieces: the interface (.h) file and the implementation (.m) file.
  31. Object Typing UILabel(*label; id(label; • Objective-C supports both strongly typed

    and weakly typed declarations. • The id class is a generic C type that Objective-C uses to represent an arbitrary object;
  32. Object Typing UILabel(*label; id(label; • Objective-C supports both strongly typed

    and weakly typed declarations. • The id class is a generic C type that Objective-C uses to represent an arbitrary object; • All objects therefore are of type id.
  33. Object Typing UILabel(*label; id(label; • Objective-C supports both strongly typed

    and weakly typed declarations. • The id class is a generic C type that Objective-C uses to represent an arbitrary object; • All objects therefore are of type id. • But, you should always use a pointer type when instance variables are themselves objects
  34. Properties @property((weak,(nonatomic)(IBOutlet(UILabel(*label; @synthesize(label; • @property compiler directive is a convenience

    to avoid the declaration and implementation of accessor methods for member variables.
  35. Properties @property((weak,(nonatomic)(IBOutlet(UILabel(*label; @synthesize(label; • @property compiler directive is a convenience

    to avoid the declaration and implementation of accessor methods for member variables. • IBOutlet denotes that this property is an Interface Builder outlet.
  36. Properties @property((weak,(nonatomic)(IBOutlet(UILabel(*label; @synthesize(label; • @property compiler directive is a convenience

    to avoid the declaration and implementation of accessor methods for member variables. • IBOutlet denotes that this property is an Interface Builder outlet. • assign, retain, copy, weak, and strong custom attributes are available.
  37. Declaring Methods H((IBAction)pushButton:(id)sender;(//(Inside(.h(file H((IBAction)pushButton:(id)sender{(//(Inside(.m(file ((//Do(Something } • The minus sign

    in front of the method indicates the method type, in this case an instance method. • A plus sign would indicate a class method.
  38. Calling Methods on nil • In Objective-C, the nil object

    is functionally equivalent to the NULL pointer found in many other C-derived languages.
  39. Calling Methods on nil • In Objective-C, the nil object

    is functionally equivalent to the NULL pointer found in many other C-derived languages. • It is permissible to call methods on nil without causing your application to crash.
  40. Calling Methods on nil • In Objective-C, the nil object

    is functionally equivalent to the NULL pointer found in many other C-derived languages. • It is permissible to call methods on nil without causing your application to crash. • If you call a method on the nil object type, you will get nil returned.
  41. Memory Management • In Objective-C for the Mac, you have

    the option of enabling garbage collection;
  42. Memory Management • In Objective-C for the Mac, you have

    the option of enabling garbage collection; •However, on the iPhone and iPad, you are restricted to using reference counting.
  43. Memory Management • In Objective-C for the Mac, you have

    the option of enabling garbage collection; •However, on the iPhone and iPad, you are restricted to using reference counting. •Automatic Reference Counting (ARC) helps. Please use it.
  44. Autorelease Pool int(main(int(argc,(char(*argv[]) { (((@autoreleasepool({ ((((((return(UIApplicationMain(argc,(argv,(nil, ((((((((((((((((NSStringFromClass([AppDelegate(class])); (((} } •Autorelease

    Pool helps in transferring control of the object life cycle from one owning object to another without immediately deallocating the object.
  45. Autorelease Pool int(main(int(argc,(char(*argv[]) { (((@autoreleasepool({ ((((((return(UIApplicationMain(argc,(argv,(nil, ((((((((((((((((NSStringFromClass([AppDelegate(class])); (((} } •Autorelease

    Pool helps in transferring control of the object life cycle from one owning object to another without immediately deallocating the object. •Defers sending an explicit release message to an object until “later”.
  46. alloc, retain, copy, and release Cycle alloc +1 retain +1

    release -1 release -1 • You should not release objects you do not own.
  47. alloc, retain, copy, and release Cycle alloc +1 retain +1

    release -1 release -1 • You should not release objects you do not own. • You should always make sure your calls to retain are balanced by your calls to release.
  48. alloc, retain, copy, and release Cycle alloc +1 retain +1

    release -1 release -1 • You should not release objects you do not own. • You should always make sure your calls to retain are balanced by your calls to release. • When releasing the object, you have the option of sending it either a release message or an autorelease message.
  49. alloc, retain, copy, and release Cycle alloc +1 retain +1

    release -1 release -1 • Sending a release message will immediately free the memory the object uses if that release takes the object’s retain count to zero
  50. alloc, retain, copy, and release Cycle alloc +1 retain +1

    release -1 release -1 • Sending a release message will immediately free the memory the object uses if that release takes the object’s retain count to zero • Sending an autorelease message adds the object to the local autorelease pool.
  51. Automatic Reference Counting •Introduced in iOS 5, Automatic Reference Counting

    (ARC) is a compiler-level feature that simplifies the process of managing the lifetimes of Objective-C objects.
  52. Automatic Reference Counting •Introduced in iOS 5, Automatic Reference Counting

    (ARC) is a compiler-level feature that simplifies the process of managing the lifetimes of Objective-C objects. •Instead of you having to remember when to use retain, release, and autorelease, ARC evaluates the requirements of your objects and automatically inserts the appropriate method calls for you at compile time.
  53. Automatic Reference Counting •Introduced in iOS 5, Automatic Reference Counting

    (ARC) is a compiler-level feature that simplifies the process of managing the lifetimes of Objective-C objects. •Instead of you having to remember when to use retain, release, and autorelease, ARC evaluates the requirements of your objects and automatically inserts the appropriate method calls for you at compile time. •The compiler also generates appropriate dealloc methods for you.
  54. Automatic Reference Counting Introduced in iOS 5, Automatic Reference Counting

    (ARC) is a compiler-‐level feature that simplifies the process of managing the lifetimes of Objective-‐C objects. Instead of you having to remember when to retain or release an object, ARC evaluates the lifetime requirements of your objects and automatically inserts the appropriate method calls at compile time.
  55. Automatic Reference Counting With ARC enabled in your project, this

    code will be transformed during a pre-compilation step into: NSObject(*anObject(=([[NSObject(alloc](init]; //(...(code(which(calls(methods(on(the(object [anObject(release];
  56. The dealloc Method H((void)dealloc({ ((((((//Do(Something } The dealloc method is

    called when an object is released. You should never call this method directly, but instead send a release message to the object.
  57. MVC Design Pattern Model : The model manages the application

    state (and associated data) and is usually persistent. It is entirely decoupled from the UI or presentation of the application state to the user.
  58. MVC Design Pattern View : The view is what the

    user sees, and it displays the model for the user. It allows the user to manipulate it and respond and generate events.
  59. MVC Design Pattern Controller : The controller coordinates updates of

    the view and the model when user interac- tion with the view makes changes to the model, and vice versa.
  60. Delegation Design Pattern An object that implements a delegate protocol

    is one that acts on behalf of another object.
  61. Datasource Design Pattern A data source is like a delegate

    except that, instead of being delegated control of the user interface, it is delegated control of data.
  62. Datasource Design Pattern Data sources are responsible for the persistence

    of the objects they hand out to user-interface objects.
  63. Table View Table views are one of the most commonly

    used iPhone UI elements. You can use a table view to display a scrollable list of information. (which can include text and/or images).
  64. Anatomy of TableView Cell The contentView contains the cell's main

    content, and contains three subviews: textLabel, detailTextLabel, and imageView. UITableViewCell has properties for these views so you can set them directly.
  65. Anatomy of TableView Cell The contentView contains the cell's main

    content, and contains three subviews: textLabel, detailTextLabel, and imageView. UITableViewCell has properties for these views so you can set them directly. UITableViewCellAccessoryDisclosureIndicator UITableViewCellAccessoryDetailDisclosureButton UITableViewCellAccessoryCheckmark
  66. Utility Application Utility applications perform simple tasks: they have a

    one- page main view and another window that is brought into view with a flip animation.
  67. Utility Application Utility applications perform simple tasks: they have a

    one- page main view and another window that is brought into view with a flip animation.
  68. Tab Bar Application If you need to provide a number

    of different views on the same data set, or separately present a number of different tasks relating to your application, Apple recommends using a tab bar application.
  69. Tab Bar Application If you need to provide a number

    of different views on the same data set, or separately present a number of different tasks relating to your application, Apple recommends using a tab bar application. You can use system-supplied icons or custom icons.
  70. Navigation Controller One of the most commonly used iPhone design

    interface patterns. Clicking on a cell in the table view makes the current view slide to the left and a new view is displayed.
  71. Navigation Controller One of the most commonly used iPhone design

    interface patterns. Clicking on a cell in the table view makes the current view slide to the left and a new view is displayed. You return to the original view by clicking on the Back button.
  72. Navigation Controller One of the most commonly used iPhone design

    interface patterns. Clicking on a cell in the table view makes the current view slide to the left and a new view is displayed. You return to the original view by clicking on the Back button.
  73. Navigation Controller One of the most commonly used iPhone design

    interface patterns. Clicking on a cell in the table view makes the current view slide to the left and a new view is displayed. You return to the original view by clicking on the Back button.
  74. Modal View Controller A view slides in from the bottom

    of the screen and is usually dismissed with a button at the top of the screen.
  75. Modal View Controller A view slides in from the bottom

    of the screen and is usually dismissed with a button at the top of the screen. When dismissed, it slides back down the screen, disappearing at the bottom.
  76. Modal View Controller A view slides in from the bottom

    of the screen and is usually dismissed with a button at the top of the screen. When dismissed, it slides back down the screen, disappearing at the bottom.
  77. Modal View Controller A view slides in from the bottom

    of the screen and is usually dismissed with a button at the top of the screen. When dismissed, it slides back down the screen, disappearing at the bottom.
  78. Embedding a Web Browser There are a number of cases

    where you might want to load a URL and display a web page, but keep users inside your application rather than closing it and opening Safari.
  79. Embedding a Web Browser There are a number of cases

    where you might want to load a URL and display a web page, but keep users inside your application rather than closing it and opening Safari. If this is what you need to do, you should be using a UIWebView.
  80. Embedding a Web Browser There are a number of cases

    where you might want to load a URL and display a web page, but keep users inside your application rather than closing it and opening Safari. If this is what you need to do, you should be using a UIWebView. Works with local files too.
  81. PhoneGap PhoneGap is a free and open source framework that

    allows you to create mobile apps using standardized web APIs for the various platforms (including iOS).
  82. PhoneGap PhoneGap is a free and open source framework that

    allows you to create mobile apps using standardized web APIs for the various platforms (including iOS).
  83. JSON Javascript Object Notation is a lightweight data- interchange format.

    • It is easy for humans to read and write. • It is easy for machines to parse and generate.
  84. JSON Javascript Object Notation is a lightweight data- interchange format.

    • It is easy for humans to read and write. • It is easy for machines to parse and generate. JSON is built on two structures:
  85. JSON Javascript Object Notation is a lightweight data- interchange format.

    • It is easy for humans to read and write. • It is easy for machines to parse and generate. JSON is built on two structures: • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  86. JSON Javascript Object Notation is a lightweight data- interchange format.

    • It is easy for humans to read and write. • It is easy for machines to parse and generate. JSON is built on two structures: • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array. • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.
  87. JSON - XML <?xml(version="1.0"?><books><book><title>The(God( Delusion</title><author>Richard(Dawkins</author></ book><book><title>The(Code(Book</title><author>Simon( Singh</author></book><book><title>Cosmos</ title><author>Carl(Sagan</author></book><book><title>The( Elegant(Universe</title><author>Brian(Greene</author></ book></books>

    {"books":([{"title":("The(God(Delusion","author":( "Richard(Dawkins"},{"title":("The(Code(Book","author":( "Simon(Singh"},{"title":("Cosmos","author":("Carl( Sagan"},{"title":("The(Elegant(Universe","author":( "Brian(Greene"}]}
  88. JSON - XML <?xml(version="1.0"?><books><book><title>The(God( Delusion</title><author>Richard(Dawkins</author></ book><book><title>The(Code(Book</title><author>Simon( Singh</author></book><book><title>Cosmos</ title><author>Carl(Sagan</author></book><book><title>The( Elegant(Universe</title><author>Brian(Greene</author></ book></books>

    {"books":([{"title":("The(God(Delusion","author":( "Richard(Dawkins"},{"title":("The(Code(Book","author":( "Simon(Singh"},{"title":("Cosmos","author":("Carl( Sagan"},{"title":("The(Elegant(Universe","author":( "Brian(Greene"}]} 319
  89. JSON - XML <?xml(version="1.0"?><books><book><title>The(God( Delusion</title><author>Richard(Dawkins</author></ book><book><title>The(Code(Book</title><author>Simon( Singh</author></book><book><title>Cosmos</ title><author>Carl(Sagan</author></book><book><title>The( Elegant(Universe</title><author>Brian(Greene</author></ book></books>

    {"books":([{"title":("The(God(Delusion","author":( "Richard(Dawkins"},{"title":("The(Code(Book","author":( "Simon(Singh"},{"title":("Cosmos","author":("Carl( Sagan"},{"title":("The(Elegant(Universe","author":( "Brian(Greene"}]} 319 223
  90. Parsing JSON • Until the arrival of iOS 5, there

    was no native support for parsing JSON.
  91. Parsing JSON • Until the arrival of iOS 5, there

    was no native support for parsing JSON. • From iOS 5, NSJSONSerialization class can be used to parse valid JSON documents.
  92. Parsing JSON • Until the arrival of iOS 5, there

    was no native support for parsing JSON. • From iOS 5, NSJSONSerialization class can be used to parse valid JSON documents. NSError(*error(=(nil; id(object(=(nil; NSData(*json(=([NSData(dataWithContentsOfFile:path]; object(=([NSJSONSerialization(JSONObjectWithData:json( options:0(error:&error]; if((error()({ ((((//code(to(handle(error(conditions(... }
  93. WHERE TO GO FROM HERE Enterprise Mobility Game Development Edu

    Applications Hardware Integration Location Based Applications
  94. WHERE TO GO FROM HERE Enterprise Mobility Game Development Edu

    Applications Travel Applications Hardware Integration Location Based Applications
  95. WHERE TO GO FROM HERE Enterprise Mobility Game Development Edu

    Applications Social Applications Travel Applications Hardware Integration Location Based Applications
  96. WHERE TO GO FROM HERE Enterprise Mobility Game Development Edu

    Applications Social Applications Travel Applications Medical Applications Hardware Integration Location Based Applications
  97. WHERE TO GO FROM HERE Enterprise Mobility Game Development Edu

    Applications Social Applications Travel Applications Medical Applications Hardware Integration and more options to explore... Location Based Applications
  98. RESOURCES • iOS Dev Center ( http://developer.apple.com/iphone/ ) • CocoaControls

    ( http://www.cocoacontrols.com ) • ManiacDev ( http://maniacdev.com/ ) • PhoneGap ( http://www.phonegap.com ) • iPhone SDK Forum ( http://www.iphonedevsdk.com/forum/ ) • Stanford iPhone Application Programming Course ( http://www.stanford.edu/class/cs193p/cgi-bin/index.php ) • O’Reilly Book ( http://oreilly.com/iphone/ )