rustyshelf
rustyshelf

Reputation: 45101

How to show the loading indicator in the top status bar

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?

Upvotes: 124

Views: 50025

Answers (9)

Youssef Hachicha
Youssef Hachicha

Reputation: 29

For Kotlin Multiplatfrom:

UIApplication.sharedApplication.setNetworkActivityIndicatorVisible(true)
UIApplication.sharedApplication.setNetworkActivityIndicatorVisible(false)

Upvotes: 0

Stephen Darlington
Stephen Darlington

Reputation: 52565

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

Upvotes: 217

M Reza
M Reza

Reputation: 19758

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.

Upvotes: 11

erickva
erickva

Reputation: 573

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'

Upvotes: 2

Babu Lal
Babu Lal

Reputation: 136

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:

  1. Import AFNetworking/AFNetworkActivityIndicatorManager.h

  2. Put this in didFinishLaunchingWithOptions:

[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES]

Upvotes: 5

Sev
Sev

Reputation: 912

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

Upvotes: 2

Resh32
Resh32

Reputation: 6590

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

Upvotes: 25

asish
asish

Reputation: 4807

A single line code to do that:

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

Upvotes: 19

Michael Waterfall
Michael Waterfall

Reputation: 20569

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

Upvotes: 30

Related Questions