Professional Documents
Culture Documents
a Tutorial.
Jonathan Mohlenhoff
◦ iOS
◦ Model View Controller Design
◦ Objective-C
Requirements for Development
◦ Mac OS X
◦ iOS SDK
◦ iPhone
App Examples
◦ Hello, World!
◦ Sensor Interfaces
References
2
History
iPhone was unveiled January 9, 2007 – 4
years ago
After 30 months of development and $150
3
Sale Volume
In millions
From: http://en.wikipedia.org/wiki/IPhone 4
Worldwide Availability
5
Why iPhone for Urban Sensing
Powerful handheld computing device
Fully networked connectivity
A multitude of built in sensors
Worldwide proliferation
Popularity
Development support and environment
6
Hardware of iPhone 4
Type Spec
Supported OS iOS 4.3
Display 3.5in IPS LCD, 960x640
Storage 16 and 32GB Flash
Processor ARM Cortex-A8 Apple A4 / 1 Core,
1GHz
Graphics PowerVR SGX535 GPU
Memory 512 MB DRAM
Connectivity 2.4GHz, 802.11n WiFi
Penta-band UMTS/HSDPA (800,
850, 900, 1900, 2100 MHz)
5.76 Mbit/s HSUPA
CDMA Dual Band Model (800,
1900MHz, Bluetooth, USB2.0/Dock
7
Sensors
Sensor
Rear Camera 5.0MP, 720p@30fps
with flash
Front Camera 0.3MP (VGA),
480p@30fps
Stereo Mic
3-axis Gyroscope
Digital Compass
3-axis Accelerometer
Proximity Sensor
Ambient Light Sensor
Multi-Touch Capacitive Display
8
Software
Success for hardware has largely been with
the success of superior development tools
Runs Apple’s iOS Operating System
Applications are developed with iOS SDK in
Objective-C
Interfaces for sensors
UI Elements library
Model-View-Controller
9
iOS Overview
Acts as an intermediary between the underlying
hardware and the applications
Applications communicate with the hardware
Multitasking/threading
Interfaces to network connectivity
User input
File System
10
iOS Layers
Cocoa Touch Layer
◦ High-level application behavior
Media Layer
◦ Handles the presentation of
visual and audible content
Core Services Layer
◦ Hardware-specific and system-
specific tasks
Core OS Layer
◦ Low-level building blocks for
applications
11
Cocoa Touch Layer
High Level Features
Multitasking (iOS 4.0 and later)
◦ Request a time to complete task
◦ Support specific services that require regular
background execution time
◦ Use local notifications to generate user alerts
Data Protection
◦ Allows applications that work with sensitive user
data to take advantage of the built-in encryption
available on some devices
Gesture Recognizers
See iOS Reference Library for more information
12
Cocoa Touch Layer
Frameworks
Map Kit Framework
◦ Provides a scrollable map interface
◦ Provide directions or highlight points of interest
◦ Annotate map with images/content
UIKit Framework
◦ Infrastructure for implementing graphical, event-
driven application
◦ Interface to:
Accelerometer data
Built-in camera
Battery state
Proximity sensor
13
Media Layer
Graphics Technologies
◦ Core Graphics: native 2D vector and image-based rendering
◦ Core Animation: support for animating views and other
content
◦ OpenGL ES: 2D and 3D rendering
◦ Image I/O: reading and writing most image formats
Audio Technologies
◦ Play high-quality audio
◦ Record high-quality audio
◦ Trigger the vibration
◦ OpenAL Framework: cross-platform standard
◦ Core Audio: generate, record, mix, and play audio
14
Core Services Layer
XML Support
◦ Retrieve elements from an XML document
◦ Manipulating XML content
CFNetwork Framework
◦ Work with network protocols: BSD sockets, FTP, HTTP, SSL, Resolve
DNS Hosts
Core Data Framework
◦ Storage of object data in a SQLite database
◦ Support for grouping, filtering, and organizing data in memory
Core Foundation Framework
◦ Collection data types (arrays, sets …)
◦ String management
◦ Date and time management
◦ Threads and run loops
◦ Port and socket communication
15
Core OS Layer
Accelerate Framework
◦ Interfaces for performing math, big-number, and DSP
calculations
◦ Optimized for all of the hardware configurations
External Accessory Framework
◦ Dock connector of a device or wirelessly using Bluetooth
Security Framework
◦ Guarantee the security of data
System
◦ Kernel environment, drivers, and low-level UNIX
interfaces of the operating system
16
iOS Developer Tools
Intel-based Macintosh
iOS SDK
◦ Xcode
IDE
iOS Simulator
◦ Interface Builder
Tool to build the UI visually instead of programattically
iPhone
Apple Developer’s License to run on physical
17
VirtualBox
Emulate MAC to run OSX on PC
http://www.virtualbox.org/
http://lifehacker.com/5583650/run-mac-os
-x-in-virtualbox-on-windows
http://iphonesdkdev.blogspot.com/2010/04
/old-versions-of-iphone-sdk.html
$699 for MAC mini or $29 for Snow Leopard
MAC OSX and run in virtualbox (free)
18
Xcode – Project Window
19
Xcode – Running a project
20
Interface Builder
21
Interface Builder
Separate program and works together with
XCode
Used to program App View graphically
Keywords are added to code to integrate with
Interface Builder:
◦ IBOutlet nothing
◦ IBAction void
Contains all GUI elements
22
Model-View-Controller
Model: objects represent data such as
SpaceShips and Weapons in a game, ToDo
items and Contacts in a productivity
application, or Circles and Squares in a
drawing application.
View: objects know how to display data
23
Model-View-Controller
24
Model-View-Controller
View controllers play a very important role in
the design and implementation of iOS
applications
View controllers make it easier for you to
implement many of the standard interface
behaviors found in iOS applications
Provides a convenient way to organize your
application’s user interface and content.
Many reasons to use view controllers in your
application and very few reasons to avoid them.
25
Model-View-Controller
26
Objective-C
The Objective-C language is a simple computer
language designed to enable sophisticated
object-oriented programming.
Objective-C extends the standard ANSI C
language by providing syntax for defining
classes, and methods, as well as other
constructs that promote dynamic extension of
classes.
Many of the traditional object-oriented
concepts, such as encapsulation, inheritance,
and polymorphism, are all present in Objective-
C.
Reference: 27
http://developer.apple.com/library/ios/#referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/
Objective-C Classes
A construct for encapsulating some data with
the actions that operate on that data.
Requires an interface (*.h) and
implementation (*.m)
28
Objective-C
Methods and Messaging
Method whose Do not require you to
execution is scoped to create an instance
a particular instance of Like a static method or
the class function in C/C++
Before you call an
instance method, an
instance of the class
(object) must first be
created
Like methods in C++
29
Objective-C
Method Declaration Syntax
30
Objective-C
Calling a Method
Done by “messaging an object”
Message is the message signature with
parameters
All messages are dispatched dynamically,
Enclosed in Brackets
31
Objective-C
Calling a Method
Done by “messaging an object”
Message is the message signature with
parameters
All messages are dispatched dynamically,
32
Objective-C
Calling a Method
Done by “messaging an object”
Message is the message signature with
parameters
All messages are dispatched dynamically,
Method signatures
33
Objective-C
Calling a Method
Done by “messaging an object”
Message is the message signature with
parameters
All messages are dispatched dynamically,
parameters
34
Objective-C
Accessor Methods
Nested messages are allowed
[[myAppObject theArray] insertObject:[myAppObject objectToInsert] atIndex:0];
Set
- (void) setPropertyName:(type)
35
Objective-C
Class Implementation (*.m)
@implementation MyClass
- (id)initWithString:(NSString *)aName
{
self = [super init];
if (self) {
name = [aName copy];
}
return self;
}
@synthesize flag;
@property BOOL flag;
@synthesize nameObject;
// Copy the object during assignment.
@property (copy) NSString *nameObject; @synthesize rootView;
Custom Options
38
Objective-C
NSString Examples
NSString *myString = @"My String\n";
39
Objective-C
Memory Management
Retain and release methods are inherited from NSObject parent
class implements a method known as Reference Counting
Each object has internal counter that keeps track of the number
of references to object.
Once reached to zero, [object release] calls dealloc and memory
is freed.
Number of alloc and retains need to match number of releases
to properly manage memory for object
See reference for more information on memory management
practices and autorelease pool.
Reference: http://www.otierney.net/objective-c.html#memorymanagement 40
Objective-C, C++
Side-by-Side
Code
Create object on stack
Free memory
Calling Methods
C++ Objective-C
41
Reference: http://howtomakeiphoneapps.com/objective-c-tutorial/
Objective-C, C++
Differences
C++ Objective-C
NULL nil
#include #import
#ifndef
#define
#endif
bool BOOL
true YES
false NO
method message
Reference: http://www.otierney.net/objective-c.html
42
Hello, World!
Create the Project
1. Launch Xcode
application
2. Choose File > New
Project
3. Select the iOS >
Application > Window-
Based Application
template
4. Name the project
“HelloWorld”
43
Hello, World (cont.)
5. Add a new class to the project, “MyView”
a) Choose File > New File
b) Select the Cocoa Touch UIView subclass template,
click Next.
44
Hello, World (cont.)
c) In the File Name text field, enter “MyView.m”
d) Select the “Also create “MyView.h”” option and click
Finish.
6. Choose the destination for the application.
If the iPhone is plugged in, you can select destination to
Device, otherwise choose the Simulator.
45
Hello, World (cont.)
Now to write the code
◦ Modify the HellowWorldAppDelegate class to use the
MyView class:
1. Select the HelloWorld project
2. Select and open the HelloWorldAppDelegate.m
3. Now edit the file:
a) Add: #import “MyView.h”
b) Inside the method
“application:didFinishLaunchingWithOptions:” add:
46
Hello, World (cont.)
Add method to draw “Hello, World!” in MyView.m
(void)drawRect:(CGRect) rect
{
NSString *hello = @"Hello, World!";
CGPoint location = CGPointMake(10, 20);
UIFont *font = [UIFont systemFontOfSize:24.0];
[[UIColor whiteColor] set];
[hello drawAtPoint:location withFont:font];
}
47
Hello, World (cont.)
48
Hello, World!
Lets go through the example in with Xcode
49
Sensors API
Best reference for the sensor interfaces is the
Apple Developer Site:
http://developer.apple.com/library/ios/navig
ation/#
50
GPS
Cocoa Touch Layer :: CoreLocation ::
CLLocationManager Class
Example:
if( locationManager.locationServiceEnabled )
{
[locationManager startUpdatingLocation];
}
else
{
…
}
51
GPS
Delegate Method
52
Accelerometer
MediaLayer :: UIKit ::
UIViewController
Uses the protocol
UIAccelerometerDelegate
1. Declare Class
2. Start Accelerometer
3. Read Measurements
53
Accelerometer
Code
1) @interface AccelController : UIViewController <UIAccelerometerDelegate>
{
UIAccelerometer *accelerometer;
}
@end
- (void)viewDidLoad
{
2) accelerometer = [UIAccelerometer sharedAccelerometer];
accelerometer.updateInterval = 0.1;
accelerometer.delegate = self;
[super viewDidLoad];
}
54
Accelerometer
Code
3) - (void)accelerometer:(UIAccelerometer *)meter didAccelerate:(UIAcceleration *)acceleration
{
float x = acceleration.x;
float y = acceleration.y;
float z = acceleration.z;
float angle = atan2(y, x);
//User Code
}
55
Magnetometer / Compass
Cocoa Touch Layer :: CoreLocation ::
CLLocationManager Class
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
57
Camera
UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
pickerController.delegate = self;
pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
58
Microphone
Framework
◦ /
System/Library/Frameworks/AVFoundation.framew
ork/AVAudioRecorder
http://developer.apple.com/library/ios/#doc
umentation/AVFoundation/Reference/AVAudi
oRecorder_ClassReference/Reference/Referen
ce.html
59
References
Look at the references throughout the
presentation
Your First iOS App:
◦ http://developer.apple.com/library/ios/#document
ation/iPhone/Conceptual/iPhone101/Articles/00_In
troduction.html#//apple_ref/doc/uid/TP40007514
-CH1-SW1
iOS Dev Center:
◦ http://developer.apple.com/devcenter/ios/index.ac
tion
60
Issues I Came Across
Developing in a MAC, in windows was more
difficult. Using VirtualBox and installing MAC
OS inside the computer.
When installing iOS SDK, I had to install a
previous version to be compatible with the
VirtualBox.
After that development was just like on a MAC
and proved very productive with the simulator.
For issues contact me at
J.Mohlenhoff@gmail.com
61