You are on page 1of 22

iPhone Programming

martino ruggiero@unibo it
martino.ruggiero@unibo.it

Important Announcement
• The iPhone Programming lessons are strictly
related to the MPHS course
• We received tens of external requests for
attendingg the lessons, but the classroom has a
limited number of seats
• External attendees are kindly requested to leave
their seats to MPHS students
• We are organizing an open seminar into a
bigger room
• Thank you for your help!
Material and acknowledgements
• Stanford iPhone Application Programming course:
– http://www.stanford.edu/class/cs193p/cgi-bin/index.php
• WebPages and blogs:
• iPhoneitalia
• iSpazio
• Etc.
• iPhone Dev Center:
• http://developer.apple.com/iphone/
• The following documents provide key information related to iPhone
p
development:
• Cocoa Fundamentals Guide provides fundamental information about the design patterns and practices
used to develop iPhone applications.
• iPhone Application Programming Guide provides an architectural overview of iPhone applications along
with practical guidance on how to create them.
• iPhone Human Interface Guidelines provides guidance and important information about how to design
your iPhone application’s user interface.
• iPhone Development Guide provides important information about the iPhone development process from
the tools perspective. This document covers the configuration of devices and the use of Xcode (and other
tools) for building
building, running,
running and testing your software.
software
• The Objective-C Programming Language introduces Objective-C and the Objective-C runtime system,
which is the basis of much of the dynamic behavior and extensibility of iPhone OS.

Outline 1st day


• iPhone Hardware Details
• iPhone SDK
• iPhone OS
• Core Application
Outline 2nd day
• Development tools:
– Xcode
– Interface Builder
– Static Analyzer
– Performance tool
– iPhone Simulator

Outline 3rd day


• ObjectiveC programming:
– Introduction to ObjectiveC
j
– Memory Management
– Databases
– Handling events
– Etc.
Outline 4th day
• Application building example
– Views and Windows
– Events handles
– Tips
– Hands on

What is iPhone

• The Apple iPhone is/has a:


– multimedia/Internet-Enabled
multimedia/Internet Enabled Phone
– Multi-touch (Revolutionary New UI)
• Allows for multiple points of contact on the touch screen
– mobile telephone
– camera
– multimedia player
– e-mail
– text messaging
– web browsing
– and wireless connectivity
What Makes iPhone
Economically Important?
• 50.000.000
50 000 000 iPhone ed iPod touch sold
• 2.000.000.000 downloaded applications through AppStore
• AppStore counts 85.000 applications
• More than 125.000 official developers
• Worldwide iPhone OS marketshare has increased from 33 to 40 percent in
the last 6 months
• iPhone represented 50 percent of US smartphone usage, followed by RIM
and Android devices at 14 and 13 percent, respectively.

measure of mobile Web and application usage from the network


It does not represent handset sales

Let’s
Let s take a look inside
inside….

No iPhones were mistreated in any way


during the production of these slides…. ☺
Which dev device
iPod
iPh
iPhone 3GS iPhone
iPh 3G iPhone
iPh 2G
Touch

App Store, multitouch,


Yes Yes Yes Yes
accelerometer

cellular data, camera Yes Yes Yes

GPS, 3G data Yes Yes

video, autofocus,
compass, more speed & Yes
RAM

ARM based CPU


• The iPhone and iPod Touch use the ARM CPU architecture (Samsung)
• iPod Touch "1G", iPhone "2G"/3G
– ARM 11 (1176JZ(F)-S), (Jazelle)
– 412MHz (Underclocked from 620MHz)
– 16KB L1 cache
– SIMD, 8 Stage Pipeline
– 90nm manufacturing process
• iPod Touch "2G"
– 532MHz (Underclocked from 620MHz)
• iPod Touch "3G"
3G • iPhone 3GS
– ARM Cortex-A8 • ARM Cortex-A8
– 600MHz (Underclocked fom 833MHz) • 600MHz (Underclocked fom
833MHz)
– 32KB L1 cache • 32KB L1 cache
h
– 13 Stage Pipeline • 13 Stage Pipeline
– 65nm manufacturing process • 65nm manufacturing process
Samsung System
System-on-Chip
on Chip

The CORTEX
CORTEX-A8
A8 Microprocessor

• On-chip
On chip L1 and L2
• 2 ALU pipes
• NEON media engine
• Jazelle
• Thumb-2
Memory hierarchy
• M i S
Main System
t Memory
M
• The iPod Touch "1G"/"2G" and iPhone "2G"/3G have 128MB of main working
memory (RAM).
• Th iP
The iPod
d Touch
T h "3G" and
d iPh
iPhone 3GS hhave 256MB off RAM.
RAM
• Storage Flash Memory
• The iPod Touch and iPhones mainly contains from 4GB to 64GB of NAND flash
storage
t memory.
• The iPod Touch "3G" has a 64GB model (from Toshiba)
• Apple decided to leave out a flash memory card slot and force users to choose
iPh
iPhone models
d l bbasedd on fifixed
d amountt off non-removeable
bl flash
fl h memory.
• Firmware Flash Memory (from 4MB to 16MB)
– In addition to regular storage flash memory, the iPhone also has additional NOR
fl h memory to
flash t store
t bootup
b t code d (similar
( i il to
t BIOS iin PC)
PC).
– This data usually holds the minimum operating system for booting up the device.
After booting up it lets other instructions stored in the storage flash memory to
take over
over.

PVR 3D – MBX vs GTX5


• iPhone 3G
• OpenGL ES 1.1
• Tile-based rendering
Architecture
• Tile accelerator
• Image synthesis processor
• Texture and shading
processor
TA ISP TSP •
• iPhone 3Gs
• OpenGL ES 2 2.0
0
• USSE is a scalable multi-
threaded multimedia
processing
i enginei ththatt
efficiently enables advanced
geometry and pixel processing
• advanced
d d per pixel
i l andd vertex
lighting effects as well as other
related GP-GPU tasks
Connectivity peripherals
• C ll l 3G
Cellular
– The iPhone elected to use W-CDMA as their 3G technology.
– WCDMA 384kbps class UL/DL
– HSDPA 7.2Mbps
– EDGE multislot class 12
– SAIC/DARP support
• Wi-Fi
– For the iPhone 3GS, the Broadcom BCM4325 chipset was used, supporting
802.11 b/g.
– Note that although this chip also supports FM radio, this feature does not seem
to be useable in the iPhone 3GS at this time.
• Bluetooth
– All the iPhone models support Class 2 Bluetooth 2.0 or 2.1
– Apple decided to only support a small subset of available Bluetooth Profiles.

Other on-board
on board peripherals
• GPS
– The iPhone 3G/3GS have assisted GPS (A-GPS) using the Infineon PMB 2525
(Hammerhead II) chipset. A-GPS means that there are many ways to obtain your location
besides reading the multiple GPS geosynchronous satellites signals broadcasted in 1.57542
GHz. When you turn ON the "Location Services", there are three major ways the iPhone gets
your location: Satellite GPS, Cell Tower Triangulation, and WPS.
• Accelerometer
– 3-axis accelerometer from STMicroelectronics, which detects rotation and orientation.
– N t that
Note th t with
ith this
thi chip
hi alone
l you are onlyl able
bl tto ttellll your position
iti iin 3D iin relation
l ti tto gravity.
it
– Gravity cannot tell you information on direction your iPhone is pointing if resting on a table.
• Magnetometer
– The iPhone 3GS also contains a 3-axis electronic compass from an AKM chip (Asahi Kasei
AK8973), which allows detection of the direction of magnetic north. In companion with GPS
data, it allows for hardware assisted turn-by-turn GPS.
• Ambient Light Sensor
– The iPhone and iPod Touch have an ambient light sensor near the top of of the device. This
sensor detects the amount of light so it can adjust the brightness of the LCD to save battery
life.
• Proximity Sensor
– All iPhones have a proximity sensor near the top left of the device. It detects when your face
is close to the phone so that the screen can by powered off to save battery life.
Which Type of Applications?
• Users can run two diff
U different types off custom applications
li i on a
device:
– web applications
pp
– native applications.
• Web applications use a combination of HTML, cascading style
sheets and JavaScript code to implement interactive applications
sheets,
that live on a web server and are transmitted over the network and
run inside the Safari web browser.
• Native applications, on the other hand, are installed directly on the
device and can run without the presence of a network connection.

iPhone SDK
• The iPhone SDK contains the tools and interfaces
needed to develop, install, and run custom native
applications.
applications
• Native applications are built using the iPhone OS system
frameworks and the Objective
Objective-CC language and they run
directly on iPhone OS.
– Unlike web applications, native applications are installed
physically on a device and can run with or without the presence
of a network connection.
– They reside next to other system applications and both the
application and any user data is synced to the user’s computer
through iTunes.
What Can You Create?
• The iPhone
Th iPh SDK supports the h creation
i off native
i fforeground d
applications that appear on the device’s Home screen only.
• The iPhone SDK does not supportpp the creation of other types
yp of
code, such as:
– Drivers,
– Background
B k d applications,
li ti
– Dynamic libraries.
– If you want to integrate code from a dynamic library into your
application, you should link that code statically into your
application’s executable file when building your project.

What’ss in the iPhone SDK?


What
• Th iPhone
The iPh SDK :
– Frameworks
– Xcode Tools
– iPhone Simulator
– iPhone Reference Library
• It also
l contains
t i h header
d fil
files, sample
l code,
d and
dddocumentation
t ti ffor
the platform’s technologies.
• You can download the iPhone SDK from the members area of the
iPhone Dev Center
– it is located at http://developer.apple.com/iphone.
– Registration is required but free
free.
Frameworks
• Apple
A l ddelivers
li most off iits system iinterfaces
f iin special
i l packages
k
called frameworks.
• A framework is a directory y that contains a dynamic
y shared libraryy
and the resources (such as header files, images, helper
applications, and so on) needed to support that library.
• To use frameworks
frameworks, you link them into your application project just
as you would any other shared library.
• Linking them to your project gives you access to the features of the
framework and also lets the development tools know where to find
the header files and other framework resources.

Xcode Tools
• It provides
id ththe ttools
l th
thatt supportt iPhone
iPh application
li ti d development,
l t iincluding
l di
the following key applications:
– Xcode: an integrated development environment that manages your application
projects
j t and d llets
t you edit,
dit compile,
il run, anddddebug
b your code. d Xcode
X d integrates
i t t
with many other tools and is the main application you use during development.
– Interface Builder: a tool you use to assemble your user interface visually. The
interface objects you create are then saved to a special resource file format and
loaded into your application at runtime.
– Instruments: a runtime performance analysis and debugging tool. You can use
Instruments to g gather information about yyour application’s runtime behavior and
identify potential problems.
• Xcode and Instruments also let you interact directly with an attached device
to run and debug your code on the target hardware.
• Development on an actual device requires signing up for Apple’s paid
iPhone Developer Program and configuring a device for development
p p
purposes.
Xcode

• To develop applications for iPhone OS, you need a Mac OS X


computer running the Xcode tools.
• Xcode is Apple’s suite of development tools that provide support for
project management, code editing, building executables, source-
level debugging, source-code
source code repository management, performance
tuning, and much more.
• At the center of this suite is the Xcode application itself, which
provides the basic source-code
source code development environment
environment.

Interface Builder

• I
Interface
f Builder
B ild iis the
h tooll you use to assemble
bl your application’s
li i ’ user
interface visually.
• Using Interface Builder, you assemble your application’s window by
dragging and dropping preconfigured components onto it.
• The components include standard system controls such as switches, text
fields, and buttons, and also custom views to represent the views your
application provides.
• After you’ve placed the components on the window’s surface, you can
position them by dragging them around, configure their attributes using the
inspector, and establish the relationships between those objects and your
code.
Instruments

• The Instruments environment lets you analyze the performance of your


pp
iPhone applications while runningg in the simulator or on a device.
• Instruments gathers data from your running application and presents that
data in a graphical display called the timeline.
• You can gather data about your application
application’s
s memory usage
usage, disk activity
activity,
network activity, and graphics performance.
• The timeline view can display all of the different types of information side by
side letting you correlate the overall behavior of your application
side, application, not just
the behavior in one specific area.

Simulator & Ref


Ref. Lib
Lib.
• iPhone Simulator:
– a Mac OS X application that simulates the iPhone technology
stack allowing you to test iPhone applications locally on your
stack,
Intel–based Macintosh computer.
• iPhone Reference Library:
– the SDK includes the reference documentation for iPhone OS by
default and you can download a more complete version of the
iPhone Reference Library (including sample code and
conceptual documentation) by subscribing to the iPhone OS
Library doc set.
The iPhone OS Architecture
• iPhone OS is
iPh i the
h operating
i system at the
h hheart off iPhone
iPh
and iPod touch devices.
• The Operating
p g System:
y
– manages the device hardware
– provides the basic technologies needed to implement
native
ti applications
li ti on th
the phone.
h
• iPhone OS is based on UNIX:
– manyy of the technologies
g that form the lower-levels of the
operating system are derived from open-source technologies.
– The interfaces for these technologies are therefore available in the
standard library and interface directories.
• The OS also ships with several system applications,
such as Phone, Mail, and Safari, that provide standard
system services for the user.

The iPhone OS Architecture


• The iPhone
Th iPh OS platform
l f was built
b il using
i the
h kknowledge
l d
that went into the creation of Mac OS X
• Manyy of the tools and technologies
g used for
development on the platform have their roots in Mac
OS X as well.
• Despite its similarities to Mac OS X,X you do not need to
be an experienced Mac OS X developer to write
applications for iPhone OS.
• The iPhone Software Development Kit (SDK) provides
everything you need to get started creating iPhone
applications.
pp
The iPhone OS Architecture
Application

Application

• At the high level, iPhone OS acts as an intermediary between the iPhone


andd iPod
iP d touch
t h hardware
h d andd th
the applications
li ti th
thatt appear on the
th screen.
• Applications that you create never interact directly with the hardware but
instead go through system interfaces, which interact with the appropriate
di
drivers.
• This abstraction protects your application from changes to the underlying
hardware.
– Even though your application is generally protected from changes to the underlying
hardware, you still need to account for differences between iPhone and iPod touch devices in
your code.

iPhone OS Technologies
• The iPhone
Th iPh OS uses a ffairly
i l straightforward
i hf d software
f stack.
k
• At the very bottom of this stack is the Mach kernel and hardware
drivers,, which manage g the overall execution of p
programs
g on the
device.
• On top of that layer are additional layers that contain the core
technologies and interfaces you use for development
development.
• Although iPhone OS does not expose any of the kernel or driver-
level interfaces, it does expose technologies at the higher levels of
the stack.
Layers of iPhone OS

• The higher-level frameworks are there to provide object-oriented


abstractions for lower-level constructs
constructs.
– They make it much easier to write code
– They reduce the number of lines of code you have to write and encapsulate potentially
complex features, such as sockets and threads.
• Although they abstract out lower-level technologies, they do not mask those
technologies from you.
• The lower
lower-level
level frameworks are still available for developers who prefer
using them or who want to use aspects of those frameworks that are not
exposed at the higher level.

Cocoa in
iPhone OS (touch) and Mac OS X
• C
Cocoa presents two ffaces:
– a runtime aspect: Cocoa applications present the user interface and
are tightly integrated with the other visible portions of the operating
system;
• on Mac OS X, these include the Finder, the Dock, and other applications
from all environments.
– a development aspect: Cocoa is an integrated suite of object-oriented
software components (classes) that helps in creating robust, full-
featured applications.
• These classes are reusable and adaptable software building blocks;
• You can build Cocoa applications for two platforms:
– the Mac OS X Cocoa
– iPhone OS Cocoa Touch
• Generally, the system libraries and frameworks of iPhone are a subset of the
libraries and frameworks on Mac OS X X.
Cocoa Touch Layer
• C
Cocoa T
Touch
h is
i one off the
h most iimportant llayers iin iPhone
iPh OS
OS.
– It comprises the key frameworks that provide the infrastructure you
need to implement applications in iPhone OS.
• When developing your applications, you should always start with
these frameworks and drop down to lower-level frameworks only as
needed.
needed
• It is composed by:
– Apple Push Notification Service
– Address Book UI Framework
– In App Email
– Map Kit Framework
– Peer to Peer Support
– UIKit Framework

Media Layer
• IIn the
h Media
M di layer
l are the
h graphics,
hi audio,
di and
d video
id technologies.
h l i
• The high-level frameworks in iPhone OS make it easy to create
advanced ggraphics
p and animations qquickly
y
• It is composed by:
– Graphics Technologies
• Quartz
Q t
• Core Animation
• OpenGL ES
– Audio Technologies
• AV Foundation
• Core Audio
• OpenAL
O AL
– Video Technologies
Core Services Layer
• Core S
C Services
i provides
id theh ffundamental
d l system services
i that
h allll
applications use.
• Even if yyou do not use these services directly,
y, manyy parts
p of the
system are built on top of them.
• It is composed by:
– Address B
Add Bookk
– Core Data
– Core Foundation
– Core Location
– Foundation Framework
– In App Purchase
– SQLite
– XML Support

Core OS Layer
• The Core
Th C OS layer
l encompasses the th kkernell environment,
i t ddrivers,
i
and basic interfaces of the operating system

• iPhone OS provides a set of interfaces for accessing many low-level


features of the operating system. Your application accesses these
features through the LibSystem library. The interfaces are C-based
and provide support for the following:
– Threading (POSIX threads)
– Networkingg ((BSD sockets))
– File-system access
– Standard I/O
– Bonjour and DNS services
– Locale information
– Memory allocation
The Core Application
• The iPhone
Th iPh applications
li i appear as iicons on the
h user’s
’ HHome
screen.
– Unlike a web application, which runs in Safari, a native application runs directly
as a standalone executable on an iPhone OS–based device.
– Native applications have access to all the features that make the iPhone and
iPod touch interesting, such as: the accelerometers, location service, Multi-Touch
interface….
interface
– They can also save data to the local file system and even communicate with
other installed applications through custom URL schemes.
• In iPhone OS,
OS you develop native applications using the UIKit
framework. Which provides:
– Fundamental infrastructure
– Default behavior
• Frameworks provide hooks that you can use to customize and
extend that behavior.

Core Application Architecture


• Every iPh
E iPhone application
li i iis b built
il using
i the
h UIKi
UIKit fframeworkk and
d
therefore has essentially the same core architecture.
• UIKit p
provides the key
y objects
j needed to run the application
pp and to
coordinate the handling of user input and the display of content on
the screen.
• Where applications deviate from one another is in how they
configure these default objects and also where they incorporate
custom objects to augment their application’s user interface and
b h i
behavior.
The Application Life Cycle
• The application life cycle constitutes the sequence of events that occurs
between the launch and termination of your application
application.
– In iPhone OS, the user launches your application by tapping its icon on the
Home screen.
– Shortly after the tap occurs,
occurs the system displays some transitional graphics and
proceeds to launch your application by calling its main function.
– From this point on, the bulk of the initialization work is handed over to UIKit,
which loads the application’s user interface and readies its event loop.
– During the event loop, UIKit
coordinates the delivery of events to
your custom objects and responds to
commands d iissued db by your
application.
– When the user performs an action
that would cause your application to
quit, UIKit notifies your application
and begins the termination process.

The Event-Handling
Event Handling Cycle
The Application Runtime
Environment
• F t Launch,
Fast L h Short
Sh t Use
U
– only one foreground application runs at a time
– application must launch and initialize itself quickly, and be prepared to exit quickly
• The Application Sandbox
– a set of fine-grained controls limiting an application’s access to files, preferences,
network resources, hardware, and so on.
– for security reasons, iPhone OS restricts an application to a unique location in the
file system.
• The Virtual Memory System
– each program still has its own virtual address space, but its usable virtual memory
is constrained by the amount of physical memory available.
– iPhone OS does not write volatile pages to disk when memory gets full.
• The Automatic Sleep Timer
– to save power, but developers can disable it only applications that do not rely on
touch inputs but do need to display visual content on the device’s screen.
– If the system does not detect touch events for an extended period of time, it dims
the screen initially and eventually turns it off altogether.

Handling Critical Application


Tasks
• Initialization and Termination
• Responding to Interruptions
• Observing Low-Memory Warnings

You might also like