Display an Activity Indicator in the status bar

The Activity Indicator can be used to show the user that a task is in progress. In this tutorial we will start and stop the Activity Indicator and display on the status bar.

Update: Dec 2, 2014. The rewritten version in Swift for iOS 8 and Xcode 6 is available here.

Open Xcode and create a new Single View Application. For product name, use ActivityIndicatorStatusBarDemo and then fill out the Organization Name, Company Identifier and Class Prefix fields with your customary values. Make sure only iPhone is selected in Devices, and that the Use Storyboards checkbox is deselected and Use Automatic Reference Counting checkbox is selected.

First, let's create our User Interface. Open ViewController.xib and drag 2 buttons to the main view and name the buttons start and stop. The view should look like this.

Select the Assistant Editor. Open The ViewController.xib and ViewController.m files. Ctrl+Drag from the buttons to the interface section and create the following IBAction method declarations.

@interface ViewController ()

- (IBAction)startActivity:(id)sender;
- (IBAction)stopActivity:(id)sender;


Implement the startActivity and stopActivity methods

- (IBAction)startActivity:(id)sender 
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

- (IBAction)stopActivity:(id)sender
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

This will display and hide the activity indicator from the status bar. Build and Run, and click the start button to display the activity indicator.

You can download the source code of the ActivityIndicatorStatusBarDemo at the ioscreator repository on github.