UIApplication + NetworkActivityIndicatorManager

The Network Activity Indicator is that little spinner that shows up in the status bar when a data exchange is in progress. It's easy enough to show and hide:

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible: YES];

That's cool when you only have one data exchange active at a time but what about if you have several? It can quickly become unwieldy to keep track of when to display the network activity indicator. e.g., I've been enjoying the Google Toolbox for Mac - HTTP Fetcher (GTMHTTPFetcher) recently but it doesn't include built-in support for managing the network activity indicator.

Enter a simple little category on UIApplication to make it easier. With the UIApplication+NetworkActivityIndicatorManager category, all you have to do is to inform the application that a new data connection has started (or ended) and it will keep track of whether or not to display the network activity indicator.


Download UIApplication+NetworkActivityIndicatorManager.zip (3 KB).


Start by adding the class to your Xcode project and importing the category:

#import "UIApplication+NetworkActivityIndicatorManager.h"

Then, when you start an asynchronous data connection, call:

[UIApplication dataOperationStarted];

Then, in the completion handler or block, simply call:

[UIApplication dataOperationEnded];

Also, if you know that all data exchanges are ending (e.g., you're cancelling them as a view is about about to disappear), you can call:

[UIApplication allDataOperationsEnded];

It's a simple little category but I hope it helps you in your own apps.