├── LICENSE ├── README.md ├── SDNetworkActivityIndicator.h └── SDNetworkActivityIndicator.m /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2010 Olivier Poitrey 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is furnished 8 | to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SDNetworkActivityIndicator 2 | 3 | Handle showing / hiding of the iOS network activity indicator to allow multiple concurrent threads to show / hide the indicator such that the indicator remains visible until all the requests have completed and requested the indicator to be hidden. 4 | 5 | ## Requirements 6 | 7 | * iOS 5.0 or later. 8 | * ARC memory management. 9 | 10 | ## Installation 11 | 12 | The easiest way to install it is by copying the following files to your project: 13 | 14 | * SDNetworkActivityIndicator.h 15 | * SDNetworkActivityIndicator.m 16 | 17 | ## Usage 18 | 19 | * When you start a network activity (will show the network activity indicator): 20 | 21 | [[SDNetworkActivityIndicator sharedActivityIndicator] startActivity]; 22 | 23 | * When you finish a network activity (will hide the network activity indicator only if the number of calls to `stopActivity` matches the number of calls to `startActivity`): 24 | 25 | [[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity]; 26 | 27 | * To hide the network activity indicator regardless of whether all activities have finished (without having to call `stopActivity` for each `startActivity` called): 28 | 29 | [[SDNetworkActivityIndicator sharedActivityIndicator] stopAllActivity]; 30 | 31 | 32 | ## License 33 | Copyright (c) 2010 Olivier Poitrey 34 | 35 | Permission is hereby granted, free of charge, to any person obtaining a copy 36 | of this software and associated documentation files (the "Software"), to deal 37 | in the Software without restriction, including without limitation the rights 38 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 39 | copies of the Software, and to permit persons to whom the Software is furnished 40 | to do so, subject to the following conditions: 41 | 42 | The above copyright notice and this permission notice shall be included in all 43 | copies or substantial portions of the Software. 44 | 45 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 46 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 47 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 48 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 49 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 50 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 51 | THE SOFTWARE. 52 | 53 | -------------------------------------------------------------------------------- /SDNetworkActivityIndicator.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the SDNetworkActivityIndicator package. 3 | * (c) Olivier Poitrey 4 | * 5 | * For the full copyright and license information, please view the LICENSE 6 | * file that was distributed with this source code. 7 | */ 8 | 9 | #import 10 | 11 | @interface SDNetworkActivityIndicator : NSObject 12 | 13 | + (id)sharedActivityIndicator; 14 | - (void)startActivity; 15 | - (void)stopActivity; 16 | - (void)stopAllActivity; 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /SDNetworkActivityIndicator.m: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the SDNetworkActivityIndicator package. 3 | * (c) Olivier Poitrey 4 | * 5 | * For the full copyright and license information, please view the LICENSE 6 | * file that was distributed with this source code. 7 | */ 8 | 9 | #import "SDNetworkActivityIndicator.h" 10 | 11 | @interface SDNetworkActivityIndicator() 12 | { 13 | @private NSUInteger counter; 14 | } 15 | @end 16 | 17 | 18 | @implementation SDNetworkActivityIndicator 19 | 20 | + (instancetype) sharedActivityIndicator 21 | { 22 | static id _sharedInstance = nil; 23 | static dispatch_once_t onceToken; 24 | dispatch_once(&onceToken, ^{ 25 | _sharedInstance = [[self alloc] init]; 26 | }); 27 | 28 | return _sharedInstance; 29 | } 30 | 31 | - (id)init 32 | { 33 | if ((self = [super init])) 34 | { 35 | counter = 0; 36 | } 37 | 38 | return self; 39 | } 40 | 41 | - (void)startActivity 42 | { 43 | @synchronized(self) 44 | { 45 | counter++; 46 | [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; 47 | } 48 | } 49 | 50 | - (void)stopActivity 51 | { 52 | @synchronized(self) 53 | { 54 | if (counter > 0 && --counter == 0) 55 | { 56 | [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; 57 | } 58 | } 59 | } 60 | 61 | - (void)stopAllActivity 62 | { 63 | @synchronized(self) 64 | { 65 | counter = 0; 66 | [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; 67 | } 68 | } 69 | 70 | @end 71 | --------------------------------------------------------------------------------