Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show the loading indicator in the top status bar

People also ask

How do I display activity indicator in Swift?

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.

What is network activity indicator?

Network Activity Indicator is described as 'displays the old 'two monitors' icon in Windows 7 that flashes blue to show network activity in the System Tray' and is an app.

What is network activity on Iphone?

App Network Activity is Apple's way of telling you which domains that apps installed on your system have been contacted in the past. When you use an app, it connects to the internet and shares information within its own internal domains as well as other third-party domains.


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

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!).


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];
    }

A single line code to do that:

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

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.