Ole Begemann: iPhone Development

iPhone Development, Cocoa and Objective-C

Managing the Network Activity Indicator

with 5 comments

iPhone developers should use the little network activity indicator in the iPhone’s status bar to inform the user when their app accesses the network. Showing or hiding the indicator is simple:

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

Most people probably call this method from their view controllers. This works fine until you have to deal with multiple concurrent tasks that access the network and/or multiple view controllers that are active simultaneously. For example, you might be running a HTTP request to download data from a webservice in the background and using a MKMapView instance that accesses the network whenever the user moves the map to a new location.

If you access the networkActivityIndicatorVisible property directly from multiple methods in these cases, chances are that you hide the indicator when the first task has finished even though your app continues to access the network. You would need to implement a counter to remember how often the network activity indicator has been shown and hidden to manage it correctly. If your code is spread among multiple view controllers, this gets even more cumbersome.

This “problem” is admittedly a small aspect of usability but the solution is so simple and elegant that I think it is worth doing even if you do not deal with multiple concurrent network activity tasks in your app.

Since the network activity indicator is displayed outside of your app’s window, the logical place to manage it is not a view controller but the application delegate. I have written a short method in my app delegate that uses a static variable to keep track of the number of times the activity indicator was asked to show or hide:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- (void)setNetworkActivityIndicatorVisible:(BOOL)setVisible {
    static NSInteger NumberOfCallsToSetVisible = 0;
    if (setVisible) 
        NumberOfCallsToSetVisible++;
    else 
        NumberOfCallsToSetVisible--;
 
    // The assertion helps to find programmer errors in activity indicator management.
    // Since a negative NumberOfCallsToSetVisible is not a fatal error, 
    // it should probably be removed from production code.
    NSAssert(NumberOfCallsToSetVisible >= 0, @"Network Activity Indicator was asked to hide more often than shown");
 
    // Display the indicator as long as our static counter is > 0.
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:(NumberOfCallsToSetVisible > 0)];
}

This method alone is then responsible for the actual call to the UIApplication instance to show or hide the indicator. All view controllers call the method on the app delegate instead:

[(MyAppDelegate *)[[UIApplication sharedApplication] delegate] setNetworkActivityIndicatorVisible:YES];

Written by Ole Begemann

September 10th, 2009 at 9:30 am

Posted in Uncategorized

5 Responses to 'Managing the Network Activity Indicator'

Subscribe to comments with RSS or TrackBack to 'Managing the Network Activity Indicator'.

  1. Hi,

    Congrats for an excellent article! I have a question though, maybe you know: Is it mandatory to use the network activity indicator in apps submitted to App Store? Is this a reason for rejection?

    Thanks!
    Vasile

    Vasile

    18 Sep 09 at 5:13 pm

  2. I don’t know if omitting the network activity indicator is grounds for rejection, but Apple clearly says in the HIG that and when it should be used: “You should display the network activity indicator if your application is performing a network operation that will take more than a couple of seconds.”

    I really see no reason not to use it given how simple it is to do so and how helpful it is to the user.

    Ole Begemann

    18 Sep 09 at 7:12 pm

  3. Thanks — that worked for me. I have a reference in my code and I will repay for this tidbit in the future.

    -mobibob

    mobibob

    17 Oct 09 at 7:16 pm

  4. Thanks — worked for me.

    /NL

    Nir Levy

    22 Oct 09 at 11:48 pm

  5. For my first version of Train Brain I had a ProgressViewController with a 50% black overlay indicating network status, but did not have [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; set. The app was not rejected. I am adding it now though because I think it helps even if slightly redundant.

    Andy

    10 Nov 09 at 2:54 am

Leave a Reply