About These Code Samples

These code samples show you the most frequently used ways that you can use UIApplication in your iPhone programs.

Access UIApplication From Anywhere

No matter where you are in your code, from any file, you can access your program's UIApplication object.
Option 1: Gain Access to UIApplication object:
UIApplication *application;
application = [UIApplication sharedApplication];

// Get idleTimeDisabled property
BOOL result;
result = application.idleTimerDisabled;
Option 2: Gain Access to UIApplication Object and property in a single statement:
result = [UIApplication sharedApplication].idleTimerDisabled;
Option 3: Older coding method:
result = [[UIApplication sharedApplication] isIdleTimerDisabled];

Idle Timer Status/Disable/Enable

When enabled, the idle timer puts your iPhone or iPod Touch into sleep mode after a given amount of non-use. Typically, this is want you want.
When disabled, the idle timer will not put your device to sleep. You sometimes want this, based on the type of iPhone application you are running.
Get current value:
BOOL result;
result = [UIApplication sharedApplication].idleTimerDisabled;
Timer Disabled:
[UIApplication sharedApplication].idleTimerDisabled = YES;
Timer Enabled:
[UIApplication sharedApplication].idleTimerDisabled = NO;

Status Bar Status/Hidden/Visible

The status bar takes up 20 pixels at the very top of the screen, showing cell phone signal strength, network activity, time, and other info.
Get status:
BOOL result;
result = [UIApplication sharedApplication].statusBarHidden;
Hide status bar:
[UIApplication sharedApplication].statusBarHidden = YES;
Show status bar:
[UIApplication sharedApplication].statusBarHidden = NO;

Network Activity Status/Hidden/Visible

The network activity icon is a small icon on the status bar. You can check the status, hide it, or make it visible.
Check status:
BOOL result
result = [UIApplication sharedApplication].networkActivityIndicatorVisible;
Hide:
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
Show:
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

Icon Badge Number

The icon on the home screen can show a number, or badge, on the icon.
Set the icon badge number:
[UIApplication sharedApplication].applicationIconBadgeNumber = 10;
Remove Icon Badge Number:
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

Gain Access to UIApplicationDelegate

From anywhere in your program, you can access the UIApplicationDelegate object. This is often useful to access that object's properties and/or methods.
DemoAppDelegate *appDelegate;
appDelegate = [UIApplication sharedApplication].delegate;

Transfer Control To Another App

Sometimes you wish to transfer control from your application to another application.
Dial the phone (your app ends and the phone dialer begins):
// Dial the phone
NSString *phoneToCall = @"tel: 123-456-7890";
NSString *phoneToCallEncoded = [phoneToCall _
_stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url = [[NSURL alloc] initWithString:phoneToCallEncoded];
[[UIApplication sharedApplication] openURL:url];
Transfer to a web page (your app ends and the web app begins):
// Access web page
NSURL *url = [[NSURL alloc] initWithString:@"http://servin.com"];
[[UIApplication sharedApplication] openURL:url];