[Lezione Ruggiero] iPhone Programming

Embed Size (px)

Citation preview

  • 8/6/2019 [Lezione Ruggiero] iPhone Programming

    1/22

    iPhone Programming

    . .

    The iPhone Programming lessons are strictlyrelated to the MPHS course

    We received tens of external requests forattending the lessons, but the classroom has alimited number of seats

    External attendees are kindl re uested to leavetheir seats to MPHS students

    bigger room

  • 8/6/2019 [Lezione Ruggiero] iPhone Programming

    2/22

    Stanford iPhone A lication Pro rammin 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 iPhonedevelopment: 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 applications user interface.

    iPhone Development Guide provides important information about the iPhone development process fromthe tools perspective. This document covers the configuration of devices and the use of Xcode (and other

    , , .

    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.

    iPhone Hardware Details

    iPhone OS

    Core Application

  • 8/6/2019 [Lezione Ruggiero] iPhone Programming

    3/22

    Development tools:

    Xcode

    Interface Builder

    Performance tool

    iPhone Simulator

    ObjectiveC programming:

    Introduction to Ob ectiveC

    Memory Management

    Handling events

    Etc.

  • 8/6/2019 [Lezione Ruggiero] iPhone Programming

    4/22

    Application building example

    Views and Windows

    Events handles

    Hands on

    The Apple iPhone is/has a:

    - Multi-touch (Revolutionary New UI)

    mobile telephone

    camera

    multimedia player

    e-mail

    text messaging web browsing

    and wireless connectivity

  • 8/6/2019 [Lezione Ruggiero] iPhone Programming

    5/22

    What Makes iPhoneEconomically Important?

    . .

    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 inthe last 6 months

    iPhone represented 50 percent of US smartphone usage, followed by RIMand Android devices at 14 and 13 percent, respectively.

    measure of mobile Web and application usage from the networkIt does not represent handset sales

    .

    No iPhones were mistreated in any wayduring the production of these slides.

  • 8/6/2019 [Lezione Ruggiero] iPhone Programming

    6/22

    Which dev device

    iPodiPodoneone oneone oneone

    TouchTouch

    App Store, multitouch,App Store, multitouch,accelerometeraccelerometer

    Yes Yes Yes Yes

    cellular data, cameracellular data, camera Yes Yes Yes

    GPS, 3G dataGPS, 3G data Yes Yes

    video, autofocus,video, autofocus,,,RAMRAM

    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)

    " "

    ARM Cortex-A8

    600MHz (Underclocked fom 833MHz)

    ARM Cortex-A8 600MHz (Underclocked fom

    833MHz)

    13 Stage Pipeline 65nm manufacturing process

    32 1 cac e

    13 Stage Pipeline 65nm manufacturing process

  • 8/6/2019 [Lezione Ruggiero] iPhone Programming

    7/22

    - -

    -

    On-chi L1 and L2 2 ALU pipes NEON media engine Jazelle Thumb-2

  • 8/6/2019 [Lezione Ruggiero] iPhone Programming

    8/22

    a n ystem emory

    The iPod Touch "1G"/"2G" and iPhone "2G"/3G have 128MB of main working

    memory (RAM).

    e o ouc "3 " an one 3 ave 256 o .

    Storage Flash Memory

    The iPod Touch and iPhones mainly contains from 4GB to 64GB of NAND flashstorage 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 chooseone mo e s ase on xe amoun o non-removea e as memory.

    Firmware Flash Memory (from 4MB to 16MB)

    In addition to regular storage flash memory, the iPhone also has additional NORas memory to store ootup co e s m ar to n .

    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

    .

    PVR 3D MBX vs GTX5 iPhone 3G

    OpenGL ES 1.1

    Tile-based rendering

    Architecture Tile accelerator

    Image synthesis processor Texture and shading

    processor

    iPhone 3Gs

    pen .

    USSE is a scalable multi-threaded multimediaprocess ng eng ne a

    efficiently enables advancedgeometry and pixel processing

    advanced per pixel and vertexlighting effects as well as otherrelated GP-GPU tasks

  • 8/6/2019 [Lezione Ruggiero] iPhone Programming

    9/22

    e u ar

    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, supporting802.11 b/g.

    Note that although this chip also supports FM radio, this feature does not seemto 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.

    - 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 locationbesides reading the multiple GPS geosynchronous satellites signals broadcasted in 1.57542GHz. 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.

    ote t at w t t s c p a one you are on y a e to te your pos t on n n re at on to grav ty.

    Gravity cannot tell you information on direction your iPhone is pointing if resting on a table.

    Magnetometer

    - -AK8973), which allows detection of the direction of magnetic north. In companion with GPSdata, 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 batterylife.

    All iPhones have a proximity sensor near the top left of the device. It detects when your faceis close to the phone so that the screen can by powered off to save battery life.

  • 8/6/2019 [Lezione Ruggiero] iPhone Programming

    10/22

    sers can run two erent types o custom app cat ons on adevice:

    web applications

    native applications.

    Web applications use a combination of HTML, cascading style,

    that live on a web server and are transmitted over the network andrun inside the Safari web browser.

    Native applications, on the other hand, are installed directly on thedevice and can run without the presence of a network connection.

    The iPhone SDK contains the tools and interfacesneeded to develop, install, and run custom native

    .

    Native applications are built using the iPhone OS system-

    directly on iPhone OS.

    Unlike web applications, native applications are installedphysically on a device and can run with or without the presenceof a network connection.

    application and any user data is synced to the users computerthrough iTunes.

  • 8/6/2019 [Lezione Ruggiero] iPhone Programming

    11/22

    e one supports t e creat on o nat ve oregrounapplications that appear on the devices Home screen only.

    The iPhone SDK does not su ort the creation of other t es ofcode, such as:

    Drivers,

    ac groun app cat ons,

    Dynamic libraries.

    application, you should link that code statically into yourapplications executable file when building your project.

    The iPhone SDK :

    Frameworks

    Xcode Tools

    iPhone Simulator

    iPhone Reference Library

    a so con a ns ea er es, samp e co e, an ocumen a on orthe platforms technologies.

    You can download the iPhone SDK from the members area of theiPhone Dev Center

    it is located at http://developer.apple.com/iphone.

    .

  • 8/6/2019 [Lezione Ruggiero] iPhone Programming

    12/22

    pp e e vers most o ts system nter aces n spec a pac agescalled frameworks.

    A framework is a director that contains a d namic shared librarand the resources (such as header files, images, helperapplications, and so on) needed to support that library.

    ,as you would any other shared library.

    Linking them to your project gives you access to the features of theframework and also lets the development tools know where to findthe header files and other framework resources.

    It provides the tools that support iPhone application development, includingthe following key applications:

    Xcode: an integrated development environment that manages your applicationpro ec s an e s you e , comp e, run, an e ug your co e. co e n egra eswith 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

    loaded into your application at runtime.

    Instruments: a runtime performance analysis and debugging tool. You can useInstruments to gather information about your applications runtime behavior andidentify potential problems.

    Xcode and Instruments also let you interact directly with an attached deviceto run and debug your code on the target hardware.

    Development on an actual device requires signing up for Apples paidiPhone Developer Program and configuring a device for developmentpurposes.

  • 8/6/2019 [Lezione Ruggiero] iPhone Programming

    13/22

    To develop applications for iPhone OS, you need a Mac OS Xcomputer running the Xcode tools.

    Xcode is Apples suite of development tools that provide support forproject management, code editing, building executables, source-

    level debu in source-code re ositor mana ement erformancetuning, and much more.

    At the center of this suite is the Xcode application itself, whichprov es e as c source-co e eve opmen env ronmen .

    Interface Builder is the tool you use to assemble your applications userinterface visually.

    Using Interface Builder, you assemble your applications window bydragging and dropping preconfigured components onto it.

    The components include standard system controls such as switches, textfields, and buttons, and also custom views to represent the views yourapplication provides.

    After youve placed the components on the windows surface, you canposition them by dragging them around, configure their attributes using theinspector, and establish the relationships between those objects and yourcode.

  • 8/6/2019 [Lezione Ruggiero] iPhone Programming

    14/22

    The Instruments environment lets you analyze the performance of youriPhone a lications while runnin in the simulator or on a device.

    Instruments gathers data from your running application and presents thatdata in a graphical display called the timeline.

    , ,network activity, and graphics performance.

    The timeline view can display all of the different types of information side by, ,

    the behavior in one specific area.

    . .

    iPhone Simulator:

    a Mac OS X application that simulates the iPhone technology,

    Intelbased Macintosh computer.

    iPhone Reference Library:

    the SDK includes the reference documentation for iPhone OS bydefault and you can download a more complete version of the

    conceptual documentation) by subscribing to the iPhone OSLibrary doc set.

  • 8/6/2019 [Lezione Ruggiero] iPhone Programming

    15/22

    one s t e operat ng system at t e eart o oneand iPod touch devices.

    The O eratin S stem:

    manages the device hardware

    provides the basic technologies needed to implementnat ve app cat ons on t e p one.

    iPhone OS is based on UNIX: many of the technologies 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 standards stem services for the user.

    The iPhone OS platform was built using the knowledgethat went into the creation of Mac OS X

    Man of the tools and technolo ies used for

    development on the platform have their roots in MacOS X as well.

    ,be an experienced Mac OS X developer to writeapplications for iPhone OS.

    The iPhone Software Development Kit (SDK) provideseverything you need to get started creating iPhonea lications.

  • 8/6/2019 [Lezione Ruggiero] iPhone Programming

    16/22

    Application

    Application

    At the high level, iPhone OS acts as an intermediary between the iPhonean o ouc ar ware an e app ca ons a appear on e screen.

    Applications that you create never interact directly with the hardware but

    instead go through system interfaces, which interact with the appropriatedrivers.

    This abstraction protects your application from changes to the underlyinghardware. 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 inyour code.

    The iPhone OS uses a fairly straightforward software stack.

    At the very bottom of this stack is the Mach kernel and hardwaredrivers which mana e the overall execution of ro rams on the

    device. On top of that layer are additional layers that contain the core

    .

    Although iPhone OS does not expose any of the kernel or driver-level interfaces, it does expose technologies at the higher levels ofthe stack.

  • 8/6/2019 [Lezione Ruggiero] iPhone Programming

    17/22

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

    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 thosetechnologies from you.

    The lower-level frameworks are still available for develo ers who referusing them or who want to use aspects of those frameworks that are notexposed at the higher level.

    Cocoa iniPhone OS (touch) and Mac OS X

    Cocoa presents two faces:

    a runtime aspect: Cocoa applications present the user interface andare 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-orientedsoftware 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:

    iPhone OS Cocoa Touch Generally, the system libraries and frameworks of iPhone are a subset of the

    .

  • 8/6/2019 [Lezione Ruggiero] iPhone Programming

    18/22

    ocoa ouc s one o t e most mportant ayers n one .

    It comprises the key frameworks that provide the infrastructure youneed to implement applications in iPhone OS.

    When developing your applications, you should always start withthese frameworks and drop down to lower-level frameworks only as

    .

    It is composed by:

    Apple Push Notification Service

    Address Book UI Framework

    In App Email

    Peer to Peer Support

    UIKit Framework

    In the Media layer are the graphics, audio, and video technologies.

    The high-level frameworks in iPhone OS make it easy to createadvanced ra hics and animations uickl

    It is composed by:

    Graphics Technologies

    uartz

    Core Animation

    OpenGL ES

    Audio Technologies AV Foundation

    Core Audio

    pen

    Video Technologies

  • 8/6/2019 [Lezione Ruggiero] iPhone Programming

    19/22

    ore erv ces prov es t e un amenta system serv ces t at aapplications use.

    Even if ou do not use these services directl man arts of thesystem are built on top of them.

    It is composed by:

    ress oo

    Core Data

    Core Foundation

    Core Location

    Foundation Framework

    SQLite

    XML Support

    e ore ayer encompasses e erne env ronmen , r vers,and basic interfaces of the operating system

    iPhone OS provides a set of interfaces for accessing many low-levelfeatures of the operating system. Your application accesses thesefeatures through the LibSystem library. The interfaces are C-basedand provide support for the following:

    Threading (POSIX threads)

    Networking (BSD sockets)

    File-system access

    Standard I/O

    Bon our and DNS services

    Locale information Memory allocation

  • 8/6/2019 [Lezione Ruggiero] iPhone Programming

    20/22

    e one app cat ons appear as cons on t e user s omescreen. Unlike a web application, which runs in Safari, a native application runs directly

    as a standalone executable on an iPhone OSbased device.

    Native applications have access to all the features that make the iPhone andiPod touch interesting, such as: the accelerometers, location service, Multi-Touchn er ace.

    They can also save data to the local file system and even communicate withother installed applications through custom URL schemes.

    n one , you eve op na ve app ca ons us ng eframework. Which provides:

    Fundamental infrastructure Default behavior

    Frameworks provide hooks that you can use to customize andextend that behavior.

    Every iPhone application is built using the UIKit framework andtherefore has essentially the same core architecture.

    UIKit rovides the ke ob ects needed to run the a lication and to

    coordinate the handling of user input and the display of content onthe screen.

    configure these default objects and also where they incorporatecustom objects to augment their applications user interface ande av or.

  • 8/6/2019 [Lezione Ruggiero] iPhone Programming

    21/22

    The application life cycle constitutes the sequence of events that occurs

    .

    In iPhone OS, the user launches your application by tapping its icon on the

    Home screen. ,

    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 applications user interface and readies its event loop.

    During the event loop, UIKitcoordinates the delivery of events toyour custom objects and responds tocomman s ssue y yourapplication.

    When the user performs an action

    quit, UIKit notifies your applicationand begins the termination process.

    -

  • 8/6/2019 [Lezione Ruggiero] iPhone Programming

    22/22

    The Application RuntimeEnvironment

    as aunc , or se

    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 applications access to files, preferences,network resources, hardware, and so on.

    for security reasons, iPhone OS restricts an application to a unique location in thefile system.

    The Virtual Memory System

    each program still has its own virtual address space, but its usable virtual memoryis 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 ontouch inputs but do need to display visual content on the devices screen.

    If the system does not detect touch events for an extended period of time, it dimsthe screen initially and eventually turns it off altogether.

    Handling Critical ApplicationTasks

    Initialization and Termination

    Responding to Interruptions

    Observing Low-Memory Warnings