Showing Network Activity When there Isn’t Any

I found that sometimes you may be doing something in your iPhone application that requires the user to wait while it completes. Often this is a network related activity, but in other cases it may not be. In my case I was parsing the response from a network connection and wanted the network activity indicator to keep spinning even though it had already downloaded the content.

Here is a quick tip on how to trigger the display of the network activity indicator (spinning icon at the top left of the screen) on an iPhone from within your application:

1
2
3
4
5
6
7
// start the indicator ...
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
 
// Do something that may take some time to complete ...
 
// stop the indicator ...
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
  1. FYI, I have confirmed that this doesn’t show up in the Simulator (2.2.1), but DOES work on the device.

  2. I heard that it does work on the simulator but only if you are connected to the net via ethernet. Weird!

    Editor’s Note: This is in reference to the SDK 3.x., where the spinner now works in the simulator

  3. Hello there,

    I’m new I’m to iPhone dev.

    I’m trying to implement NetworkActivityIndicatorVisible while sending an email.

    If I write the below mentioned code, then the activity indicator keeps on spinning, even after the mail is sent.

    -(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error{

    [self dismissModalViewControllerAnimated:YES];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    }

    If I add this code, after …YES];

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

    then the activity indicator disappears.

    I want the activity indicator to be visible as long as the mail is not sent (task performing in the background). And once it’s sent the activity indicator stops spinning and disappears.

    Would really appreciate if you could solve this problem.

    Cheers!

    • Sid, what I think you want to do is to start the activity indicator before sending the email, and once complete, didFinishWithResult is called and you can then stop the indicator.

Comments are closed.