0% found this document useful (0 votes)
107 views198 pages

Introducing The Iphone Os: 1 Himanshu H Patel

The document discusses the architecture of the iPhone OS. It is divided into four main layers - Cocoa Touch, Media, Core Services, and Core OS. Cocoa Touch contains the frameworks for building the user interface like UIKit. Media provides audio, video and graphics capabilities through frameworks like OpenGL ES and Core Animation. Core Services offers fundamental services like data storage, networking etc. Core OS interacts directly with the hardware and provides low-level services.

Uploaded by

Vinay Pahelani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
107 views198 pages

Introducing The Iphone Os: 1 Himanshu H Patel

The document discusses the architecture of the iPhone OS. It is divided into four main layers - Cocoa Touch, Media, Core Services, and Core OS. Cocoa Touch contains the frameworks for building the user interface like UIKit. Media provides audio, video and graphics capabilities through frameworks like OpenGL ES and Core Animation. Core Services offers fundamental services like data storage, networking etc. Core OS interacts directly with the hardware and provides low-level services.

Uploaded by

Vinay Pahelani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 198

Introducing the iPhone OS

1 Himanshu H Patel
Architecture Of the i-phone OS
The iPhone OSs frameworks are
divided into four major layers.

Each of these layers contains a


variety of frame-works that you can
access when writing iPhone SDK
programs.

Generally, you should prefer the


higher- level layers when youre
coding (those shown toward the top
in the diagram).
2
Architecture Of the i-phone OS

3
Architecture Of the i-phone OS
The Cocoa Touch layer sits at the top of the iPhone OS stack
and contains the frameworks that are most commonly used
by iPhone application developers.

Cocoa Touch is primarily written in Objective-C, is based on


the standard Mac OS X Cocoa API and has been extended
and modified to meet the needs of the iPhone.

4 By: Prof. Himanshu patel


Architecture Of the i-phone OS
The Cocoa Touch layer provides the following frameworks
for iPhone app development:
1) UIKit Framework (UIKit.framework)
2) Map Kit Framework (MapKit.framework)
3) Push Notification Service
4) Message UI Framework (MessageUI.framework)
5) Address Book UI Framework (AddressUI.framework)
6) Game Kit Framework (GameKit.framework)

5 By: Prof. Himanshu patel


Architecture Of the i-phone OS
Media Services:

The role of the Media layer is to provide the iPhone OS with


audio, video, animation and graphics capabilities.
As with the other layers comprising the iPhone OS stack, the
Media layer comprises a number of frameworks
that can be utilized when developing iPhone apps.

6 By: Prof. Himanshu patel


Architecture Of the i-phone OS
The Media Service layer provides the following frameworks
for iPhone app development:
1) Core Graphics Framework (CoreGraphics.framework)
2) Quartz Core Framework (QuartzCore.framework)
3) OpenGL ES framework (OpenGLES.framework)
4) iPhone Audio Support
5) AV Foundation framework (AVFoundation.framework)
6) Core Audio Frameworks (CoreAudio.framework,
AudioToolbox.framework and
AudioUnit.framework)
7) Open Audio Library (OpenAL)
7 8) Media Player framework (MediaPlayer.framework)
By: Prof. Himanshu patel
Architecture Of the i-phone OS
Core Services:

provides fundamental access to iPhone OS services and


consists of the following components:

Collections ,Address BookNetworking ,File Access ,SQLite


,Core Location ,Net Services ,Threading ,URL Utilities

8 By: Prof. Himanshu patel


Architecture Of the i-phone OS
The iPhone Core Services layer provides much of the
foundation on which the above layers are built and consists
of the following frameworks:

1) Address Book framework (AddressBook.framework)


2) Core Data Framework (CoreData.framework)
3) Core Foundation Framework
(CoreFoundation.framework)
4) Foundation Framework (Foundation.framework)
5) Core Location Framework (CoreLocation.framework)
6) Store Kit Framework (StoreKit.framework)
9 7) SQLite library By: Prof. Himanshu patel
Architecture Of the i-phone OS
Core OS:
The Core OS Layer is the bottom layer of the iPhone OS
stack and sits directly on top of the device hardware.

The layer provides a variety of services including low level


networking, access to external accessories and the usual
fundamental operating system services such as memory
management, file system handling and threads.

10 By: Prof. Himanshu patel


Architecture Of the i-phone OS
The Core OS layer provides the following frameworks for
iPhone app development:
1) CFNetwork Framework (CFNetwork.framework)
2) External Accessory framework
(ExternalAccessory.framework)
3) Security Framework (Security.framework)
4) System (LibSystem)

11 By: Prof. Himanshu patel


Architecture Of the i-phone OS
iPhone Hardware:
Hardware devices are managed by iPhone OS and provides
the technologies needed for implementing native applications
on the phone.

The OS ships with several system applications such as Mail,


Safari and Phone that provide standard services to the user.

12 By: Prof. Himanshu patel


Architecture Of the i-phone OS
Cocoa Touch is a top layer of the iPhone OS stack and it
contains the frameworks that are most commonly used by
iPhone application developers.

Cocoa Touch is primarily written in Objective-C, and it is


based on the standard Mac OS X Cocoa API.

It contains the UIKit framework.

The UIKit includes window support, event support, and


user-interface management, and allows you to create both
text and web pages.
13 By: Prof. Himanshu patel
Architecture Of the i-phone OS
Media is the second layer from the top of the stack.

It provides the iPhone OS with audio, video, animation and


graphics capabilities.

As with the other layers of the iPhone OS stack, the Media


layer comprises a number of frameworks that can be
utilized when developing iPhone apps.

Its four graphical technologies are OpenGL ES, EAGL


(which connects OpenGL to your native window objects),
Quartz (which is Apples vector-based drawing engine), and
14 By: Prof. Himanshu patel
Core Animation (which is also built on Quartz).
Architecture Of the i-phone OS
Core Services offers the frameworks used in all
applications.

provides fundamental access to iPhone OS services and


consists of the following components:

Collections ,Address BookNetworking ,File Access ,SQLite


,Core Location ,Net Services ,Threading ,URL Utilities

15 By: Prof. Himanshu patel


Architecture Of the i-phone OS
The Core OS Layer is the bottom layer of the iPhone OS
stack and sits directly on top of the device hardware.

This layer provides a variety of services including low level


networking, access to external accessories and the usual
fundamental operating system services such as memory
management, file system handling and threads.

16 By: Prof. Himanshu patel


Most of your iPhone programming work will be done
using the UIKit (UI) or Foundation (NS) frameworks.
These libraries are collectively called Cocoa Touch;
theyre built on Apples modern Cocoa framework.

However, youll sometimes have to fall back on libraries


that are instead based on simple C functionality. Examples
include Apples Quartz 2D and Address Book frame-
works, as well as third-party libraries like SQLite.

When you fall back on non-Cocoa libraries, youll


sometimes have to use Apples Core Foundation
17framework, which lies belowBy:Cocoa.
Prof. Himanshu patel
The hierarchy of the iPhones objects

Figure shows many of the classes.

18 By: Prof. Himanshu patel


Coca Touch Framework

19 By: Prof. Himanshu patel


Cocoa Touch Framworks
Cocoa Touch is a UI(User Interface) framework for
building software programs to run on the iOS operating
system (for the iPhone, iPod Touch, and iPad) from Apple
Inc.

Using UIKit you have access to the special GUI controls,


buttons, and full-screen views on iOS. You also get to
control your application with the the multi-touch gesture.

20 By: Prof. Himanshu patel


Cocoa Touch Framworks
Cocoa Touch is based on the Mac OS X Cocoa
API toolset and, like it, is primarily written in
the Objective-C language.

Because Objective-C is a superset of C, it is easy to mix


C and even C++ into your Cocoa Touch applications.

21 By: Prof. Himanshu patel


Cocoa Touch Framworks
Cocoa Touch includes powerful Objective-C frameworks
that perform entire tasks in only a few lines of code,
while providing the foundational C-language APIs to give
direct access to the system when needed.

Examples of those frameworks include:

Core Animation
Core Audio
Core Data

22 By: Prof. Himanshu patel


Cocoa Touch Framworks

Graphics And Animation in iOS

23 By: Prof. Himanshu patel


Core Animation
Use Core Animation to create rich user experiences
from an easy programming model based on compositing
independent layers of graphics.

Animations are created by defining key steps along a path,


describing how the layers of text, images, and OpenGL ES
graphics will interact.

Core Animation processes the definitions at runtime,


smoothly moving the visual elements from one step to
the next.

24 By: Prof. Himanshu patel


OpenGL ES

OpenGL ES has a simpler API than its desktop


counterpart but uses the same key concepts, including
programmable shaders and extensions to make your 3D
app or game stand out.

25 By: Prof. Himanshu patel


Core Image

Create amazing effects in your camera and image editing


apps with Core Image.

Core Image is a hardware-accelerated framework that


provides an easy way to enhance photos and videos. Core
Image provides several built-in filters, such as color
effects, distortions and transitions.

It also includes advanced features such as auto enhance,


red-eye reduction and facial recognition.
26 By: Prof. Himanshu patel
Resolution Independence

The high pixel density of the Retina display makes text


and graphics look smooth and continuous at any size.

With resolution independence in iOS, a few changes to


your app icons, graphics, and code will ensure that your
app looks its best on any iOS device.

27 By: Prof. Himanshu patel


Quartz 2D

Quartz 2D is the powerful 2D graphics API for iOS.

Use Quartz 2D for custom interface elements to give a


stylized look to your application.

it is a snap to display PDF documents as well.

28 By: Prof. Himanshu patel


Photo Library

UIKit provides access to the users photo library.

The photo picker interface provides controls for


navigating the users photo library and selecting an image
to return to your app.

You can also enable user editing controls and provide an


interface to the camera, so photos taken can be loaded
directly into your app.

29 By: Prof. Himanshu patel


Audio and Video in iOS

30 By: Prof. Himanshu patel


iOS delivers rich audio and video capabilities.

Stream and play full-screen video within your app. Get full
programmatic control over video playback and capture.

Leverage full control over the audio processing


capabilities of iPad, iPhone, and iPod touch with Core
Audio.

31 By: Prof. Himanshu patel


Core Audio and OpenAL
Core Audio is the professional-grade technology for
playing, processing and recording audio.

Core Audio seamlessly manages the audio environment,


automatically routing audio when using headphones,
Bluetooth headsets, and docks.

It can also trigger vibrations. For advanced effects, the


OpenAL API models and plays audio in a 3D space.

32 By: Prof. Himanshu patel


Data Management in iOS

33 By: Prof. Himanshu patel


iOS has a comprehensive collection of tools and
frameworks for storing, accessing, and sharing data.

Core Data is a full-featured data modeling framework for


object-oriented Cocoa Touch applications, while SQLite is
perfect for low-level relational database work.

Applications can share data using a URL-based system


that works across all of iOS.

34 By: Prof. Himanshu patel


Web apps can persist data in a client-side cache with the
HTML5 data storage API.

iOS apps even have access to a devices global data such


as contacts in the Address Book, and photos in the
Photo Library.

35 By: Prof. Himanshu patel


Core Data
Core Data provides a flexible and powerful data model
framework for building well-factored Cocoa applications
based on the Model-View-Controller (MVC) pattern.

Core Data provides a general-purpose data management


solution developed to handle the data model needs of
every kind of application, large or small.

36 By: Prof. Himanshu patel


Core Data
Core Data lets you quickly define your applications data
model in a graphical way and easily access it from your
code.

It provides an infrastructure to deal with common


functionality such as save, restore, undo and redo, allowing
you to get on with the task of building innovation into
your application.

Because Core Data uses the built-in SQLite data library


there is no need to install a separate database system.

37 By: Prof. Himanshu patel


SQLite
iOS includes the popular SQLite library, a lightweight yet
powerful relational database engine that is easily
embedded into an application.

Used in countless applications across many platforms,


SQLite is considered a de facto industry standard for
lightweight embedded SQL database programming.

Unlike the object-oriented Core Data framework, SQLite


uses a procedural, SQL-focused API to manipulate the
data tables directly.

38 By: Prof. Himanshu patel


XML Files
XML files provide a lightweight structured format that
your application can easily read and write. Plus XML files
readily fit into the iOS file system.

Store your application settings and user preferences in


the built-in User Defaults database.

This XML-based data store includes a simple API with


powerful features, including the ability to serialize and
restore complex objects on demand.

39 By: Prof. Himanshu patel


Features List: Frameworks by
Category
Audio and Video
Core Audio
OpenAL
Media Library
AV Foundation

Graphics and Animation


Core Animation
OpenGL ES
Quartz 2D

40 By: Prof. Himanshu patel


Features List: Frameworks by
Category
Data Management
Core Data
SQLite

Networking and Internet


Bonjour
WebKit
BSD Sockets

41 By: Prof. Himanshu patel


Features List: Frameworks by
Category
User Applications
Address Book
Core Location
Map Kit
Store Kit

42 By: Prof. Himanshu patel


Difference Between Cocoa and Cocoa
Touch
Cocoa

Cocoa is the application framework for used for


development in Mac OS X.

Cocoa is commonly referred to as the combination of the


Foundation and AppKit frameworks, while
In Cocoa, the have the "NS" prefix as in "NSButton" (the
NS stands for NextStep which is a predecessor to Mac
OS X).

If you refer Foundation frameworks in Cocoa ,it


43 By: Prof. Himanshu patel
has "NSHost" class.
Difference Between Cocoa and Cocoa
Touch
Cocoa Touch
Cocoa Touch is the application framework used for
development in iOS (iPhone,iPad and iPod)

Cocoa Touch is commonly referred as combination of the


Foundation and UIKit frameworks

In Cocoa Touch, the UI classes all start with the prefix


"UI" as in "UIButton", "UIView", etc,(the UI stands for
User Interface elements on iOS)

If you refer Foundation frameworks in Cocoa Touch ,it's


44 By: Prof. Himanshu patel
missing "NSHost" class.
THE NS CLASSES
The NS classes come from Core Services Foundation
framework (the Cocoa equivalent of the Core
Foundation frame-work), which contains a huge number
of fundamental data types and other objects.

You should use the fundamental Cocoa classes like


NSString and NSArray when-ever you can, rather than C
fundamentals like string * or a plain array.

45 By: Prof. Himanshu patel


THE UI CLASSES
The second broad category of classes contains the UI
classes. These come from Cocoa Touchs UIKit
framework.

It includes all of the graphical objects that youll be using


as well as all the functionality for the iPhone OSs event
model, much of which appears in UIResponder. Thats
another topic well return to soon.

46 By: Prof. Himanshu patel


The iPhone OSs methods
The iPhone OS has a complex and deep structure of
classes.

Two of the most important are NSObject and


UIResponder, which contain many of the methods and
properties that youll use throughout your programming.

47 By: Prof. Himanshu patel


Object creation
how instance objects are created from classes depend on
the implementation of your framework.
In the iPhone OS its the NSObject that defines how
object creation works.
a two-step procedure that uses the alloc class method
and the init instance method. The alloc method allocates
the memory for your object, and then returns the object
itself.
The init method then sets some initial variables in that
method. They usually occur through a single, nested
message:
id newObject = [[objectClass alloc] init];
48 By: Prof. Himanshu patel
Object creation

49 By: Prof. Himanshu patel


Memory management
Because of power considerations, the iPhone OS doesnt
support garbage collection.
That means that every object thats created must
eventually have its memory released.
The fundamental rule of memory management in the
iPhone OS is this: if you allocated the memory for an
object, you must release it.
This is done via the release message (which is once again
inherited from NSObject):
[object release];

50 By: Prof. Himanshu patel


Memory management
Youll note that we said you only must release the
memory if you allocated the memory for it.
If you look back to the class factory methods we talked
about in the previous section,
youll see that we didnt actually allocate the memory for
those (because we didnt send any alloc message), which
means were not responsible for releasing it.
How does the OS know when weve finished working
with the object it created for us?

51 By: Prof. Himanshu patel


THE AUTORELEASE ALTERNATIVE
If youre responsible for the creation of an object and
youre going to pass it off to some other class for usage,
you should autorelease the object before you send it off.

This is done with the autorelease method:


[object autorelease];

Youll typically send the autorelease message just before


you return the object at the end of a method.

52 By: Prof. Himanshu patel


THE AUTORELEASE ALTERNATIVE
Once an object has been autoreleased, its watched over
by a special NSAutoreleasePool.

The object is kept alive for the scope of the method that
its been passed to, and then the NSAutoreleasePool
cleans it up.

53 By: Prof. Himanshu patel


RETAINING AND COUNTING
So what if you want to hold onto an object that has been
passed to you, and that is going to get autoreleased?

In that case, you send it a retain message:


[object retain];

When you do this, youre saying you want the object to


stay around but now youve become responsible for its
memory as well: you must send a release message at
some point to balance your retain.

54 By: Prof. Himanshu patel


RETAINING AND COUNTING
At this point, we should probably back up and explain the
underlying way that the iPhone OS actually manages these
memory objects.
It does so by maintaining a count of object usage.
By default its set to 1. Each retain message increases that
count by 1,and each release message reduces that count
by 1. When the count drops to 0, the memory for the
object is freed up.
Therefore, all memory management can be thought of as
pairs of messages. If you balance every alloc and every
retain with a release, your object will eventually be
freed up when youre done with it.
55 By: Prof. Himanshu patel
MEMORY MANAGEMENT WRAP-UP

56 By: Prof. Himanshu patel


Event response
There are three main ways that events can appear on the
iPhone:
through bare events (or actions),
through delegated events,
or through notification.

Whereas the methods of our earlier topics all derived


from NSObject, iPhone event response instead comes
from the UIResponder object, while iPhone notification
comes from the NSNotificationCenter.

57 By: Prof. Himanshu patel


Life-cycle management

58 By: Prof. Himanshu patel


Protocols in Objective C
for instance, it is called an interface in Java.

A protocol, as its name implies, is a set of rules that


classes can abide by in order to be used in certain ways.

Protocols are different from actual classes in that they do


not have an implementation.

They are just rules. For instance, every car has wheels,
doors, and a main body color, among many other things.

59 By: Prof. Himanshu patel


Protocols in Objective C
#import <Foundation/Foundation.h>
@protocol Car <NSObject>
@property (nonatomic, copy) NSArray *wheels;
@property (nonatomic, strong) UIColor
*bodyColor;
@property (nonatomic, copy) NSArray *doors;
@end

60 By: Prof. Himanshu patel


Protocols in Objective C
lets create a class for a car, such as Jaguar, and then make
that class conform to our protocol.

#import <Foundation/Foundation.h>
#import "Car.h"
@interface Jaguar : NSObject <Car>
@end

61 By: Prof. Himanshu patel


Model-View-Controller
You may hear iOS programmers mention the Model-
View-Controller pattern.

What this means is every object you create is exactly one


of the following: a model object, a view object, or a
controller object

62 By: Prof. Himanshu patel


Model-View-Controller
View objects are visible to the user.

In Quiz, the buttons, labels, and the view they are placed
on top of are all view objects.

Views are usually standard UIView subclasses (UIButton,


UISlider), but you will sometimes write custom view
classes.

These typically have names like DangerMeterView or


IncomeGraphView.
63 By: Prof. Himanshu patel
Model-View-Controller
Model objects hold data and know nothing about the
user interface.

In this application, the model objects will be two lists of


strings: the questions array and the answers array.

64 By: Prof. Himanshu patel


Model-View-Controller

65 By: Prof. Himanshu patel


Model-View-Controller
Model objects typically use standard collection classes
(NSArray, NSDictionary, NSSet) and standard value types
(NSString, NSDate, NSNumber).

But there can be custom classes, which typically have


names that sound like data-bearing objects, such as
InsurancePolicy or PlayerHistory.

66 By: Prof. Himanshu patel


Model-View-Controller
View and model objects are the factory workers of an
application they focus tightly on specific tasks.

For example, an instance of UILabel (a view object)


knows how to display text in a given font within a given
rectangle.

An NSString instance (a model object) knows how to


store a character string.

But the label doesnt know what text it should display, and
the string doesnt know what characters it should store.
67 By: Prof. Himanshu patel
Model-View-Controller
This is where controller objects come in.

Controllers are the managers in an application. They keep


the view and model objects in sync, control the flow of
the application, and save the model objects out to the file
system.

Controllers are the least reusable classes that you will


write, and they tend to have names like
ScheduleController and ScoreViewController.

68 By: Prof. Himanshu patel


Model-View-Controller

The controller behavior is implemented through three


key technologies:
delegation,
target-action, and
notification

69 By: Prof. Himanshu patel


Model-View-Controller

70 By: Prof. Himanshu patel


Model-View-Controller
When you create a new iOS project from a template, the
template automatically makes a controller object for you.

For Quiz, this controller is the QuizViewController.

Most applications have more than one controller object,


but a simple application like Quiz only needs one.

(Actually, the template creates another controller for


Quiz the QuizAppDelegate.

71Every iOS application has anBy:app delegate object, and it


Prof. Himanshu patel
is the primary controller of the application.
Model-View-Controller
One of the QuizViewControllers tasks will be showing
the user a new question when the Show Question button
is tapped.

Tapping this button will trigger a method in the


QuizViewController. This method will retrieve a new
question from an array of questions and place that
question in the top label.

These interactions are laid out in the object diagram for


Quiz shown in Figure.

72 By: Prof. Himanshu patel


Model-View-Controller

73 By: Prof. Himanshu patel


Object Hierarchies

74 By: Prof. Himanshu patel


code of the First iOS application

75 By: Prof. Himanshu patel


Windows and views
As the UI classes demonstrate, the iPhone OS is deeply
rooted in the idea of a graphical user interface.

Therefore, lets finish our introduction to the iPhone OS


by looking at some of the main graphical abstractions
embedded in the UIKit.

There are three major abstractions: windows, views, and


view controllers

76 By: Prof. Himanshu patel


Windows and views
A window is something that spans the entire screen of
the iPhone.

Theres only one of them for your application, and its the
overall container for everything that your application
does.

77 By: Prof. Himanshu patel


Windows and views
A view is the actual content holder in your application.

You may have several of them, each covering different


parts of the window or doing different things at different
times.

Theyre all derived from the UIView class.

major subclasses of UIView are UIControls, which give


you buttons, sliders, and other items that users may
manipulate your program with, and UIScrollableViews,
which give users access to more text than can appear at
78 By: Prof. Himanshu patel
once.
Windows and views
A view controller acts as the controller element of the
MVC model and in the process manages a screenful of
text, which is sometimes called an application view.

weve divided view controllers into two types.


Basic view controllers are those which just manage a
screenful of text (such as the table view controller).
advanced view controllers are those that let a user
move around among several pages of text (such as the
navigation bar controller and the tab bar controller)

79 By: Prof. Himanshu patel


Windows and views
Figure shows how these three types of objects
interrelate.

80 By: Prof. Himanshu patel


Windows and views
Windows, views, and view controllers are ultimately part
of a view hierarchy.
This is a tree of objects that begins with the window at
its root.
A simple program might just have a window with a view
under it.
Most programs will start with a window, have a view
controller under that, perhaps supported by additional
view controllers, each of which controls views that might
have their own sub views.

81 By: Prof. Himanshu patel


Introduction to Development Tool Mac
Desktop
When you turn on Mac, you will see

82 By: Prof. Himanshu patel


Searching for Application on Mac Machine
You can press the icon on the right upper corner to
search for application:

83 By: Prof. Himanshu patel


iPhone App Development Tool - Xcode
Basically, we need a place to write our iPhone app. In Mac,
Xcode is provided to do so.

84 By: Prof. Himanshu patel


Create a New Project in Xcode
You can create a new project by pressing:

85 By: Prof. Himanshu patel


iPhone/iPad Project Selection
Now, you can select whether you would like your app to
be run on iPad or iPhone. Note that iPhone app can also
be run on iPad by resizing the app during runtime.

86 By: Prof. Himanshu patel


View-Based Application Template
There are quite a number of application frameworks provided.
The most common one is the view-based.

Here, we input our project name as:


BallShooting

Then, several files will be generated automatically, including


BallShootingViewController.h - The View Controller Header File
BallShootingViewController.m The View Controller Implementation
File
BallShootingViewController.xib The View Controller Interface
Builder file

87 By: Prof. Himanshu patel


View based application project
BallShooting

App Delegate

Main

BallShootingViewController

Main Window

88 By: Prof. Himanshu patel


View-based Application Architecture
main

App
Delegate

View Controller

Main Window

View Controller
Screen view

89
By: Prof. Himanshu patel
View-based Application Architecture - Main
main Represent as a file main.m

App Acts like that of main


Delegate function in C/C++ program
which will be the first place
View Controller to call during the program
starts
Main Window
Invoke App Delegate
View Controller
Screen view

90 By: Prof. Himanshu patel


View-based Application Architecture
App Delegate
main Represent as files
<ProjectName>AppDelegate.h-
Invoke Header File
<ProjectName>AppDelegate.m-
App
Implementation File
Delegate Invoke
Invoke Will be activated to handle the several
View Controller application events:
Application Launched
Main Window Invoke
Application Terminated

View Controller Further invoke:


Screen view Main Window Frame
View Controller

91 By: Prof. Himanshu patel


View-based Application Architecture
Main Window Frame
main Represent as a
fileMainWindow.xib
Invoke
App
Delegate Acts as a frame container to
Invoke include all other UI
Invoke View Controller Components

Main Window Invoke

View Controller
Screen view

92 By: Prof. Himanshu patel


View-based Application Architecture
View Controller
main Represent as files
<ProjectName>ViewController.xib
Invoke <ProjectName>ViewController.m
App <ProjectName>ViewController.h
Delegate Invoke Controls various UI components
on the screen view, i.e., when,
Invoke where, and how the UI
View Controller components are shown
We can implement the code logic
Main Window Invoke here to control the UI
By default, the screen view of the
View Controller view controller is added to the
main window during startup of the
Screen view view controller
The only files we need to work on!
93 By: Prof. Himanshu patel
Xcode Layout I
c. Compile and Run
a. Target Device/Simulator

b. Files Location

d. File text
Editor

94 By: Prof. Himanshu patel


Target Selection Device / iPhone
Simulator

Select whether
your app would
like to run in a
real device or
iPhone simulator

95 By: Prof. Himanshu patel


File Text Editor in Xcode

1. When you highlight the file here

2. Corresponding content will


show here

96 By: Prof. Himanshu patel


Information Property List I
Most properties of the app can be modified in the
information property list file. i.e., BallShooting-info.plist

97 By: Prof. Himanshu patel


Information Property List II
Information Property List Hidden Status Bar

Information Property List Supported Orientations

Information Property List Icon file

98 By: Prof. Himanshu patel


Screen Shot After Modification
Landscape Orientation with no Status Bar

BallShooting Icon

99 By: Prof. Himanshu patel


Interface Builder I
When you double click BallShootingViewController.xib

b. View

d. Attributes
Inspector

a. Library

c. Components

100 By: Prof. Himanshu patel


Interface Builder II Open Library
window Here

Open Attribute
Inspector window Here

Open view window here

101 By: Prof. Himanshu patel


Developer Documentation I
When you want to look for help during coding, you can
look for the developer documentation.

102 By: Prof. Himanshu patel


Developer Documentation II

Type in the item that you


would like to search here

For example, you can search for NSString in the search field
to look for related functions.
103 By: Prof. Himanshu patel
NSLog Debug Console
Usually, we will add in some screen output in various
parts of the program to perform debugging task. This can
be done by:
Press to pop up the
debug console
window

The string message


pass to the NSLog will
show up in the console

104 By: Prof. Himanshu patel


code of the First iOS application

105 By: Prof. Himanshu patel


code of the First iOS application

106 By: Prof. Himanshu patel


Execution

107 By: Prof. Himanshu patel


APPDELEGATE.H

108 By: Prof. Himanshu patel


APPDELEGATE.H
AppDelegate inherits from UIResponder that handles iOS
events.

Implements the delegate methods of UIApplication


delegate which provide key application events like finished
launching, about to terminate and so on.

UIWindow object to manage and co-ordinate the various


views on the iOS device screen.

109 By: Prof. Himanshu patel


APPDELEGATE.H
It's like the base view over which all other views are
loaded. Generally there is only one window for an
application.

UIViewController to handle the screen flow.

110 By: Prof. Himanshu patel


APPDELEGATE.M

111 By: Prof. Himanshu patel


APPDELEGATE.M

112 By: Prof. Himanshu patel


APPDELEGATE.M

113 By: Prof. Himanshu patel


APPDELEGATE.M

114 By: Prof. Himanshu patel


APPDELEGATE.M

115 By: Prof. Himanshu patel


APPDELEGATE.M

116 By: Prof. Himanshu patel


Important items in code

117 By: Prof. Himanshu patel


Important items in code
All the methods defined above are UI application
delegates and contains no user defined methods.

UIWindow object is allocated to hold the application is


allocated.

UIViewController is allocated made as window's initial


view controller.

To make window visible makeKeyAndVisible method is


called.
118 By: Prof. Himanshu patel
VIEWCONTROLLER.H

The ViewController class inherits the UIViewController


which provides the fundamental view management model
for the iOS applications.

119 By: Prof. Himanshu patel


VIEWCONTROLLER.M

120 By: Prof. Himanshu patel


VIEWCONTROLLER.M
Two methods implemented here which are defined in the
base class UIViewController

Do initial setup in viewDidLoad which is called after


view loads.

didReceiveMemoryWarning method is called in case of


memory warning.

121 By: Prof. Himanshu patel


Interface Builder
Interface Builder is a graphical development environment
integrally tied in to Xcode.

Whenever you write an Xcode project, it includes an .xib


file that contains Interface Builder definitions for where
graphical objects are placed.

Each of the different Xcode templates comes with


different objects prebuilt this way.

122 By: Prof. Himanshu patel


IBActions and IBOutlets
When you need to connect your code with an object in
Interface Builder you use IBAction and IBOutlet.

These keywords are a flag for Interface Builder to know


that there is a property available to be connected with
the controller.

You then connect up the corresponding object with the


action or outlet.

123 By: Prof. Himanshu patel


IBActions and IBOutlets
An IBAction is how an object can communicate back to
your code when an event happens, when a button is
pressed you use an IBAction to call a method back in
your controller.

An IBOutlet is the inverse, how your controller can


communicate with an object in Interface Builder and your
view at any time.

124 By: Prof. Himanshu patel


IBActions and IBOutlets
Creating an IBOutlet is as easy as adding the flag when
you define the object property in your controller.

@interface HelloInterfaceBuilderViewController : UIViewController


{
IBOutlet UILabel *myText;
}
@property (nonatomic, retain) IBOutlet UILabel *myText;

125 By: Prof. Himanshu patel


IBActions and IBOutlets
An IBAction is similar. When defining a method in the
header file you add the IBAction flag as the return type.

@interface HelloInterfaceBuilderViewController : UIViewController


{
// ... IBOutlets and properties ...
}
- (IBAction)buttonPressed:(id)sender;

126 By: Prof. Himanshu patel


IBActions and IBOutlets
Connecting these is an easy task in Interface Builder.

When creating an IBAction: Click on the object in the window,


hold control, drag to the "Files Owner" in the Document
window, release the mouse and then control. Select the action
to link to from the drop down provided.

When creating an IBOutlet: Click on the "Files Owner" in the


Document window, hold control, drag from the "Files Owner"
to the object in the window, release the mouse then control.
Select the outlet from the drop down provided.

127 By: Prof. Himanshu patel


iOS App Delegation
A delegate is an object that acts on behalf of, or in
coordination with, another object when that object
encounters an event in a program.

Delegates are helper objects. They enable us to control


the behavior of our objects. The methods listed in the
protocol become helpers to our MyClass.

128 By: Prof. Himanshu patel


129 By: Prof. Himanshu patel
130 By: Prof. Himanshu patel
We can now use these methods in our object.

For example, including the


<CLLocationManagerDelegate> protocol in our MyClass
interface definition enables our object to be notified by
the iPhone's GPS of our new location.

131 By: Prof. Himanshu patel


The following example shows the method that we will
include and define inside our object's implementation file:

- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation*)newLocation
fromLocation:(CLLocation *)oldLocation
{

}

132 By: Prof. Himanshu patel


The locationManager delegate method automatically gets
called as our GPS location changes, allowing your code to
process the new and old coordinates

133 By: Prof. Himanshu patel


AlertView
Use of Alerts
Alerts are used to give important informations to user.
Only after selecting the option in the alert view, we can
proceed further using the app.

We can create Alert from UIAlertView

Steps Involved

Step 1. Create a simple View based application.

134 By: Prof. Himanshu patel


AlertView
Step 2. Add Buttom in ViewController.xib
/Storyboard.

Step 3. Create IBAction Method for Button in


ViewController.m file.

135 By: Prof. Himanshu patel


AlertView

136 By: Prof. Himanshu patel


AlertView
After editing

137 By: Prof. Himanshu patel


AlertView
Connecting Hello World Button with the Action

138 By: Prof. Himanshu patel


AlertView
Connecting Hello World Button with the Action
Release both buttons and a pop-up shows the showMessage
action. Select it to make a connection between the button and
showMessage action.

139 By: Prof. Himanshu patel


AlertView
OutPut:

140 By: Prof. Himanshu patel


AlertView
http://www.appcoda.com/hello-world-app-using-xcode-5-
xib/

141 By: Prof. Himanshu patel


iOS Programming Tutorial: Create a
Simple Table View App
Use of TableView
It is used for displaying a vertically scrollable view which
consists of a number of cells (generally reusable cells). It
has special features like headers, footers, rows, and
section.

Steps Involved

Step 1. Create a simple View based application.

142 By: Prof. Himanshu patel


iOS Programming Tutorial: Create a
Simple Table View App
Step 2. In the Object Library, select the Table View
object and drag it into the view.

143 By: Prof. Himanshu patel


iOS Programming Tutorial: Create a
Simple Table View App
Step 2. In the Object Library, select the Table View
object and drag it into the view.

144 By: Prof. Himanshu patel


iOS Programming Tutorial: Create a
Simple Table View App
Step 3. For Table View ,we have to add protocol.

UITableViewDelegate,
UITableViewDataSource

Step 4. Go to ViewController.h Append after


UIViewController.Your code should look like below:

145 By: Prof. Himanshu patel


iOS Programming Tutorial: Create a
Simple Table View App
Step 5. The UITableViewDataSource protocol declares
two required methods(tableView:cellForRowAtIndexPath
and tableView:numberOfRowsInSection) that you have to
implement.

UITableViewDelegate, on the other hand, deals with the


appearance of the UITableView. Optional methods of the
protocols let you manage the height of a table row,
configure section headings and footers, re-order table
cells, etc. We do not change any of these methods in this
example

146 By: Prof. Himanshu patel


iOS Programming Tutorial: Create a
Simple Table View App
Step 6. Select ViewController.m and define an instance
variable for holding the table data

147 By: Prof. Himanshu patel


iOS Programming Tutorial: Create a
Simple Table View App
Step 7. In the viewDidLoad: method, add the following
code to declare the recipes array. We initialize an array
with a list of recipes.

148 By: Prof. Himanshu patel


iOS Programming Tutorial: Create a
Simple Table View App
Step 8. The first method is used to inform the table view
how many rows are in the section. So lets add the below
code. The count: method simply returns the number of
items in the tableData array.

149 By: Prof. Himanshu patel


iOS Programming Tutorial: Create a
Simple Table View App
Step 8.

150 By: Prof. Himanshu patel


iOS Programming Tutorial: Create a
Simple Table View App
Step 8.

151 By: Prof. Himanshu patel


iOS Programming Tutorial: Create a
Simple Table View App
Step 9. Connecting the DataSource and Delegate

152 By: Prof. Himanshu patel


iOS Programming Tutorial: Create a
Simple Table View App
Step 10. To ensure the connections are linked properly,
you can select the Table View again. In the upper part of the
Utility area, you can reveal the existing connections in the
Connection Inspector (i.e. the rightmost tab).

153 By: Prof. Himanshu patel


iOS Programming Tutorial: Create a
Simple Table View App
OutPut:

154 By: Prof. Himanshu patel


iOS Programming Tutorial: Create a
Simple Table View App
http://www.appcoda.com/ios-programming-tutorial-
create-a-simple-table-view-app/

155 By: Prof. Himanshu patel


Working with UITableView in Xcode 5
Using Storyboard
http://www.appcoda.com/uitableview-tutorial-storyboard-
xcode5/

156 By: Prof. Himanshu patel


Use Storyboards to Build Navigation
Controller and Table View
http://www.appcoda.com/use-storyboards-to-build-
navigation-controller-and-table-view/

157 By: Prof. Himanshu patel


Storyboards Segue Tutorial: Pass Data
Between View Controllers
http://www.appcoda.com/storyboards-ios-tutorial-pass-
data-between-view-controller-with-segue/

158 By: Prof. Himanshu patel


Date & Time Picker
Step 1: Open a Xcode, Create a View base application. Give the application
name DatePickerWithDate.

Step 2: Xcode automatically creates the directory structure and adds


essential frameworks to it. You can explore the directory structure to
check out the content of the directory.

Step 3: Open the DatePickerWithDate.h file and create an instance of


UIDatePicker class and UILabel class. So make the following changes in the
file.

159 By: Prof. Himanshu patel


Date & Time Picker
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
- (IBAction) btnDate: (id) sender;
- (IBAction) btnTime: (id) sender;
@property (retain, nonatomic) IBOutlet UIDatePicker *ViewSelectedDate;
@end

160 By: Prof. Himanshu patel


Date & Time Picker
In the DatePickerWithDate.m file make the following changes in the file:
-----------------------------------------------------------------------------------

- (IBAction)btnDate:(id)sender
{
NSDate *dt = ViewSelectedDate.date;
NSDateFormatter *formtr = [[NSDateFormatter alloc]init];
formtr.dateStyle = NSDateFormatterShortStyle;
NSString *str = [formtr stringFromDate:dt];
UIAlertView *alt = [[UIAlertView alloc]initWithTitle:@"Date" message:str
delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alt show];
}

161 By: Prof. Himanshu patel


Date & Time Picker
- (IBAction)btnTime:(id)sender
{
NSDateFormatter *formtr = [[NSDateFormatter alloc]init];
[formtr setDateFormat:@"hh:mm:ss a"];
NSString *time = [formtr stringFromDate:self.ViewSelectedDate.date];
UIAlertView *alt = [[UIAlertView alloc]initWithTitle:@"Time" message:time
delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alt show];
}
@end

162 By: Prof. Himanshu patel


Date & Time Picker
Output:

163 By: Prof. Himanshu patel


Reading PDF File in iPhone Simulator
We can read PDF using the UIWebView.

Steps Involved

Step 1. Create a simple View based application.

Step 2. Add UIWebView in ViewController.xib


/Storyboard.

164 By: Prof. Himanshu patel


Reading PDF File in iPhone Simulator
Step 3. Update ViewController.h as follows

165 By: Prof. Himanshu patel


Reading PDF File in iPhone Simulator
Step 3. Update ViewController.m as follows

166 By: Prof. Himanshu patel


Reading PDF File in iPhone Simulator
Step 3. Update ViewController.m as follows

167 By: Prof. Himanshu patel


Reading PDF File in iPhone Simulator
Step 3. Update ViewController.m as follows

168 By: Prof. Himanshu patel


Reading PDF File in iPhone Simulator
Note

We need to add pdf files for ensuring that we get the


expected output.

169 By: Prof. Himanshu patel


Reading PDF File in iPhone Simulator
Output:

170 By: Prof. Himanshu patel


Email Sending in iPhone Simulator
We can send emails using the Email application of iOS
device.

Steps Involved

Step 1. Create a simple View based application.

Step 2. Select your project file, then select targets and


then addMessageUI.framework.

Step 3. Add a button in ViewController.xib and create


an action for sending email. By: Prof. Himanshu patel
171
Email Sending in iPhone Simulator
Step 4. Update ViewController.h as follows

172 By: Prof. Himanshu patel


Email Sending in iPhone Simulator
Step 5. Update ViewController.m as follows

173 By: Prof. Himanshu patel


Email Sending in iPhone Simulator
Output
When we run the application, we'll get the following
output

174 By: Prof. Himanshu patel


Email Sending in iPhone Simulator
Output
On clicking Send Email, we will get the following output

175 By: Prof. Himanshu patel


Gesture Control in iOS
http://www.appcoda.com/ios-gesture-recognizers/

https://developer.apple.com/library/ios/documentation/Eve
ntHandling/Conceptual/EventHandlingiPhoneOS/Gesture
Recognizer_basics/GestureRecognizer_basics.html

176 By: Prof. Himanshu patel


tab-bar in iOS
Use of Tab Bar
It's generally used to switch between various subtasks,
views or models within the same view.
Example for tab bar is shown below.

177 By: Prof. Himanshu patel


tab-bar in iOS
Important Properties
backgroundImage
items
selectedItem

178 By: Prof. Himanshu patel


tab-bar in iOS
Sample Code and Steps

Step 1. Create a new project and select Tabbed


Applicationinstead of the View Based application and
click next, Give the project name and select create.

Step 2. Here two view controllers are created by default


and a tab bar is added to our application.

179 By: Prof. Himanshu patel


tab-bar in iOS
Step 3. The AppDelegate.m
didFinishLaunchingWithOptions method is as follows

180 By: Prof. Himanshu patel


tab-bar in iOS
Step 3. The AppDelegate.m
didFinishLaunchingWithOptions method is as follows

181 By: Prof. Himanshu patel


tab-bar in iOS
4. Here, two view controllers are
allocated and made as view
controllers of our tab bar controller.

5. When we run the application, we'll


get the following output:

182 By: Prof. Himanshu patel


Audio & Video in iOS
Audio and video is quite common in the latest devices.

It is supported in iOS with the help of


AVFoundation.framework and MediaPlayer.framew
orkrespectively.

183 By: Prof. Himanshu patel


Audio & Video in iOS
Steps Involved

Step 1. Create a simple View based application.

Step 2. Select your project file, select targets, and then


we should
addAVFoundation.framework and MediaPlayer.fra
mework.

Step 3. Add two buttons in ViewController.xib


/Storyboard and create an action for playing audio and
video respectively.
184 By: Prof. Himanshu patel
Audio & Video in iOS
Step 4. Update ViewController.h as follows

185 By: Prof. Himanshu patel


Audio & Video in iOS
Step 5. Update ViewController.m as follows

186 By: Prof. Himanshu patel


Audio & Video in iOS
Note

We need to add audio and video files for ensuring that


we get the expected output.

187 By: Prof. Himanshu patel


Audio & Video in iOS
OutPut:

188 By: Prof. Himanshu patel


Audio & Video in iOS
OutPut:
When we click on play video, we will get an output as
shown below

189 By: Prof. Himanshu patel


ACCELEROMETER
Accelerometer is used for detecting the changes in
the position of the device in the three directions x, y
and z.

We can know the current position of the device


relative to the ground. For testing this example, you'll
need to run it on a device and it doesn't work on
simulator.

190 By: Prof. Himanshu patel


ACCELEROMETER
Accelerometer Steps Involved

1. Create a simple View based application.

2. Add three labels in ViewController.xib and create


ibOutlets naming them as xlabel, ylabel, and zlabel.

191 By: Prof. Himanshu patel


ACCELEROMETER
Update ViewController.h as follows

192 By: Prof. Himanshu patel


ACCELEROMETER
Update ViewController.m as follows

193 By: Prof. Himanshu patel


ACCELEROMETER
Output
When we run the application in iPhone device, we'll
get the following output:

194 By: Prof. Himanshu patel


Location Services in iOS
http://www.appcoda.com/how-to-get-current-location-
iphone-user/

195 By: Prof. Himanshu patel


XML Parsing & JSON Parsing in iOS
http://www.appcoda.com/parse-xml-and-json-web-
service/

196 By: Prof. Himanshu patel


SqLite in iOS
http://www.appcoda.com/sqlite-database-ios-app-tutorial/

197 By: Prof. Himanshu patel


All iOs Concepts
http://www.appcoda.com/ios-programming-course/

198 By: Prof. Himanshu patel

You might also like