Activity Indicator on Status Bar iOS Tutorial

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 made with Xcode 10 and built for iOS 12.

Open Xcode and create a new Single View App.

For product name, use iOS12ActivityStatusBarTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and choose Next.

Drag two Buttons to the main view, center them horizontally and place them on top of each other.  Give them a title of "Start" and "Stop". The Storyboard should look like this.

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: Any) {
    UIApplication.shared.isNetworkActivityIndicatorVisible = true
}

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

@IBAction func startActivity(_ sender: Any) {
    UIApplication.shared.isNetworkActivityIndicatorVisible = false
}

Build and Run the project, The activity indicator can not be displayed on a iPhone X, so choose a iPhone 8 as device for the simulator. Press start and stop to show and hide the Activity Indicator on the status bar.

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