I have noticed that some apps like Safari and Mail show a loading indicator in the status bar (the bar at the very top of the phone) when they are accessing the network. Is there a way to do the same thing in SDK apps, or is this an Apple only thing?
8 Answers
It's in UIApplication:
For Objective C:
Start:
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
End:
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
For swift :
Start
UIApplication.shared.isNetworkActivityIndicatorVisible = true
End
UIApplication.shared.isNetworkActivityIndicatorVisible = false

- 6,627
- 2
- 58
- 83

- 51,577
- 12
- 107
- 152
-
1Thanks that works perfectly. Just a side note: the simulator seems to ignore this value, which made me think at first it didn't work. – rustyshelf Oct 03 '08 at 13:33
-
11@rustyshelf, it displays in the newer simulators. – MrHus Feb 11 '11 at 12:18
-
2`[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];` //as a one liner – David Douglas Jun 25 '15 at 22:16
-
1For Swift syntax use `UIApplication.sharedApplication().networkActivityIndicatorVisible = true` – moraisandre Feb 04 '16 at 03:42
-
2The activity indicator won't show in the status bar on the iPhone X – Melly Mar 05 '18 at 01:39
-
@ClareHuxtable Yep, for some reason Apple decided not to show the indicator on the X (and presumably all future phones with a "notch"). – Stephen Darlington Mar 05 '18 at 12:01
-
Likely the reason why it was deprecated: `'isNetworkActivityIndicatorVisible' was deprecated in iOS 13.0: Provide a custom network activity UI in your app if desired.` – jacksoor Jan 23 '23 at 11:11
I've found the following macros pretty useful!
#define ShowNetworkActivityIndicator() [UIApplication sharedApplication].networkActivityIndicatorVisible = YES
#define HideNetworkActivityIndicator() [UIApplication sharedApplication].networkActivityIndicatorVisible = NO
So you can just call ShowNetworkActivityIndicator();
or HideNetworkActivityIndicator();
from within your app (as long as the header is included of course!).

- 20,497
- 27
- 111
- 168
-
35Why not define a category on UIApplication? Seems much nicer (and more debuggable) than a #define. – Barry Wark Nov 17 '09 at 18:56
I wrote a singleton that solves the problem of multiple connections by keeping a counter of what is happening (to avoid removing the status when a connection returns but another one is still active):
The header file:
#import <Foundation/Foundation.h>
@interface RMActivityIndicator : NSObject
-(void)increaseActivity;
-(void)decreaseActivity;
-(void)noActivity;
+(RMActivityIndicator *)sharedManager;
@end
and implementation:
#import "RMActivityIndicator.h"
@interface RMActivityIndicator ()
@property(nonatomic,assign) unsigned int activityCounter;
@end
@implementation RMActivityIndicator
- (id)init
{
self = [super init];
if (self) {
self.activityCounter = 0;
}
return self;
}
-(void)increaseActivity{
@synchronized(self) {
self.activityCounter++;
}
[self updateActivity];
}
-(void)decreaseActivity{
@synchronized(self) {
if (self.activityCounter>0) self.activityCounter--;
}
[self updateActivity];
}
-(void)noActivity{
self.activityCounter = 0;
[self updateActivity];
}
-(void)updateActivity{
UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = (self.activityCounter>0);
}
#pragma mark -
#pragma mark Singleton instance
+(RMActivityIndicator *)sharedManager {
static dispatch_once_t pred;
static RMActivityIndicator *shared = nil;
dispatch_once(&pred, ^{
shared = [[RMActivityIndicator alloc] init];
});
return shared;
}
@end
Example:
[[RMActivityIndicator sharedManager]increaseActivity];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:self.networkReceiveProcessQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
[[RMActivityIndicator sharedManager]decreaseActivity];
}

- 6,500
- 3
- 32
- 40
-
3Thanks @Schrockwell, I further improved it by using synchronized blocks - I saw a race condition where the activity was not decreased properly. – Resh32 Dec 19 '12 at 08:50
-
3
A single line code to do that:
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

- 4,767
- 4
- 29
- 49
The status bar network activity indicator was deprecated in iOS 13.
Using UIApplication.shared.isNetworkActivityIndicatorVisible = true
will not work anymore.
The deprecation message says:
Provide a custom network activity UI in your app if desired.

- 18,350
- 14
- 66
- 71
You need to take care of hiding the activity indicator also once your network call is done.
If you use AFNetworking
, then you don't need to do much.
Do following changes in AppDelegate
Class:
Import
AFNetworking/AFNetworkActivityIndicatorManager.h
Put this in
didFinishLaunchingWithOptions:
[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES]

- 7,342
- 8
- 68
- 104

- 136
- 1
- 4
-
1Or with restkit it will be #import [[AFRKNetworkActivityIndicatorManager sharedManager] setEnabled:YES]; – deepwinter Sep 21 '17 at 00:34
It might also be helpful to make sure you are running it on the main thread as it is UI related.
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
});

- 883
- 1
- 14
- 34
As many have said, there is no network activity indicator for the iPhone X and probably for the other new iPhones with the notch.
I came across this incredible library written by Ortwin Gentz, FutureTap: https://github.com/futuretap/FTLinearActivityIndicator
It puts the indicator right back where it was when the iPhone X was initially released, many would remember the Knight Rider type of indicator.
This library is available for Swift 4.2, so you will need to change the Swift Language settings, as described here: Type 'NSAttributedStringKey' (aka 'NSString') has no member 'font'

- 513
- 6
- 17