How to show the loading indicator in the top status bar

后端 未结 8 2047
借酒劲吻你
借酒劲吻你 2020-12-04 06:37

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

8条回答
  •  悲&欢浪女
    2020-12-04 06:54

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

提交回复
热议问题