Hello world ios v1

16
+ Software Development Community Mobile Device Programming Subgroup Hello World Series – Part I - IOS Paul Hancock - 29 April 2012

description

Introduction to izOS Programming using a Hello World Example

Transcript of Hello world ios v1

Page 1: Hello world ios v1

+

Software Development CommunityMobile Device Programming Subgroup

Hello World Series – Part I - IOS

Paul Hancock - 29 April 2012

Page 2: Hello world ios v1

+Agenda

What do you need to get started?

Objective C explained in 30 seconds

Target Considerations: iPad vs. iPhone vs. Universal

Hello World! … take I Adding graphical elements to the main window programmatically

Hello World! … take II Building graphical elements to the main window using WYSIWYG “Interface Builder”

Hello World! … take III Adding graphical elements onto subviews

Not covered in this presentation: Design patterns and concepts required to create non-trivial applications (Delegation,

Notifications, Core Data, Storyboards, etc.) Getting your app on the AppStore and becoming rich

Page 3: Hello world ios v1

+What you’ll need to get started

Intel-based Mac running OSX

Development Environment

Basic knowledge of object oriented programming concepts

Familiarity with event-driven programming models

Can spell MVC (Model-View-Controller)

15 minutes of spare time

Page 4: Hello world ios v1

+Development Environment IOS SDK / Xcode running on Mac OSX

Available from the AppStore for Free Everything you need, including iPhone/iPad simulators

Optional: IOS Devices Developer’s License ($99/year)

Developer certificate allows signing applications Allows developer to download/test/debug apps on actual

IOS devices Provides access to developer resources (help, etc.) Allows developer to put app on appstore and get rich

Page 5: Hello world ios v1

+Model-View-Controller

Every IOS application object should be one of these

Model Object Contains data without any knowledge of the application logic or GUI

View Object Interfaces to the user (buttons, scroll bars, etc.) without any

knowledge of how the data is organized

Controller Object Manages the application logic. Has knowledge of the Model objects

and the View objects. Acts as the glue to keeps Model objects and View objects in sync.

Least reusable classes

Page 6: Hello world ios v1

+MVC Design Pattern

View Controller Model

Update the View Fetch Data needed by View

Update DataSends Message

Controllers don’t tell Views what to display. Views ask Controllers for what they need

Page 7: Hello world ios v1

+Objective C in 30 seconds

A very simple language, like C

Superset of C Entry point is main(); originally implemented as C preprocessor (as was C++) Objective C keywords begin with “@”, for example @synthesize

Adds object-oriented paradigms Classes / Objects / Encapsulation / Inheritance / Polymorphism / etc. Allows only single inheritance (subclasses have exactly one superclass)

All objects ultimately derived from NSObject (“NS” stands for Next Step)

Cocoa Touch Framework (not part of Objective C) Rich set of frameworks/libraries to enable rapid and consistent development of IOS

applications

Further Reading: Programming in Objective-C (4th Edition) - Stephen G. Kochan Objective-C Programming: The Big Nerd Ranch Guide – Aaron Hillegass

Page 8: Hello world ios v1

+Objective C - Object/Method Notation

-(float)calculateAreaOfRectangleWithWidth:(float)x andHeight:(float)y{ return x*y;}

Object Method Definition within a Class Implementation

Method Invocation by an object

…you might be expecting something like….

float area;area = myObject->calculateAreaOfRectangle(23.7,12.0);

Page 9: Hello world ios v1

+Objective C - Object/Method Notation

-(float)calculateAreaOfRectangleWithWidth:(float)x andHeight:(float)y{ return x*y;}

Object Method Definition within a Class Implementation

float area;area = [myObject calculateAreaOfRectangleWithWidth:23.7 andHeight:12.0];

Receiver Selector “calculateAreaOfRectangleWithWidth:andHeight:”

Arguments

But no, you do it by sending a message to the object…I mean the receiver….

Page 10: Hello world ios v1

+Application Target ConsiderationsiPhone? iPhone Retna? iPad? The New iPad?

iPhone applications will run unmodified on iPads, but potentially with degraded (pixelated) appearance (not recommended)

CoreGraphics based on points, not pixels – vector graphics not impacted

Xcode allows multiple images to be bundled as resources so that they appear sharp on all displays

Device Screen Resolution

Graphics Resolution

iPhone 3Gs and earlier 320x480 pixels 320 x 480 pointsiPhone 4 and 4s (Retna) 640x960 pixels

iPad and iPad 2 768x1024 pixels768 x 1024 pointsThe New iPad (Retna) 1536x2048

pixels

Page 11: Hello world ios v1

+Real Estate Considerations

vs.

iPhone vs iPad Vastly different screen real estate calls for different approach iPhones use UINavigationController as the primary drill-down

mechanism iPads can leverage greater real estate and use UISplitViewController

UINavigationController

UISplitViewController

Page 12: Hello world ios v1

+Additional Resources

iOS Programming: The Big Nerd Ranch Guide (3rd Edition) Aaron Hillegass & Joe Conway Very structured and well written explanation of Cocoa architecture and

approach

iPad iOS 5 Development Essentials or iPhone iOS 5 Development Essentials Neil Smyth Best how-to guides from soup to nuts for getting an application onto

the AppStore

iOS 5 Programming Pushing the Limits: Developing Extraordinary Mobile Apps for Apple iPhone, iPad, and iPod Touch Rob Napier Not a beginner book

Page 13: Hello world ios v1

And Now….

Let’s fire up Xcode and write some apps!

Page 14: Hello world ios v1

+Hello World 1Implementation Summary

Programmatic approach (no WYSIWYG)

Created “Empty” project AppDelegate Class Instantiates the window

Added Application icons

Modified AppDelegate to Change window background color Created a UIButton (declared in .h and initialized in .m) Set the size and position of the button Set the title to “Press Me” Set the action for pressing the button to call sayHello:

Page 15: Hello world ios v1

+Hello World 2Implementation Summary Implement the main window graphically with Interface Builder

Created “Empty” project AppDelegate Class Instantiates the window

Added application icons

Modified AppDelegate to Comment out the programmatic instantiation of the window Declare helloButton (using keyword IBOutlet), declare/implement sayHello action (using keyword

IBAction)

Created a new project file (IOS Interface->Window) Created MainWindow.xib

Added object (cube icon), set class to HelloWorld2AppDelegate (acts as a placeholder until runtime)

Changed background color, added button, set the title to “Press Me” with 46pt font Graphically make connections

Set File’s Owner Class to UIApplication, set File’s Owner Delegate to HelloWorld2AppDelegate Connect helloButton (in AppDelegate) to graphical button object, connect touch up inside action to

sayHello (in AppDelegate)

Set Application Main Interface to MainWindow

Page 16: Hello world ios v1

+Hello World 3Implementation Summary Implement graphical objects in a view, with a dedicated ViewController

rather than directly on window

Created “Single View Application” project AppDelegate Class ViewController Class XIB Basic connections made for you

Added application icons

Modified ViewController to Declare helloButton (using keyword IBOutlet), declare/implement sayHello

action (using keyword IBAction)

Edit xib Set background color, added button, set the title to “Press Me” with 46pt font Connect helloButton (in File’s Owner) to graphical button object, connect touch up

inside action to sayHello (in File’s Owner)