Display Activity Indicator on Status Bar in iOS8 with Swift

The Activity Indicator can be displayed in the status bar to inform the user that some kind of network activity is in progress. This tutorial will start and stop the Activity Indicator in the status bar. This tutorial is built with iOS 8.1 and Xcode 6.1.

Open Xcode and create a new Single View Application. For product name, use iOS8SwiftActivityIndicatorStatusBarTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.

rag two Button to the main view, center them horizontally and place them on top of each other.  Give them a title of "Start" and "Stop". Select the two buttons and select the "Resolve Auto Layout Issues" button at the bottom-right(3rd button) and select Add Missing Constraints.

Select the Assistant Editor and make sure the ViewController.swift is visible. Ctrl and drag from the start Button to the ViewController class  and create the following Action.

Ctrl and drag from the stop Button to the ViewController class  and create the following Action.

implement the  startActivity method

 @IBAction func startActivity(sender: AnyObject) {
    UIApplication.sharedApplication().networkActivityIndicatorVisible = true
}

The networkActivityIndicatorVisible is an boolean to turn the indicator on or off. Next, implement the stopActivity method

 @IBAction func startActivity(sender: AnyObject) {
    UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}

Build and Run the project, press start and stop to show and hide the Activity Indicator on the status bar.

You can download the source code of the iOS8SwiftActivityIndicatorStatusBarTutorial at the ioscreator repository on Github.