Skip to content

Commit f467402

Browse files
authored
Merge pull request silkimen#118 from 0verrfl0w/ios-networking-indicator
Ios networking indicator
2 parents 27e9598 + 5919d90 commit f467402

File tree

6 files changed

+198
-0
lines changed

6 files changed

+198
-0
lines changed

plugin.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<header-file src="src/ios/AFNetworking/AFURLRequestSerialization.h"/>
3434
<header-file src="src/ios/AFNetworking/AFURLResponseSerialization.h"/>
3535
<header-file src="src/ios/AFNetworking/AFURLSessionManager.h"/>
36+
<header-file src="src/ios/SDNetworkActivityIndicator/SDNetworkActivityIndicator.h"/>
3637
<source-file src="src/ios/CordovaHttpPlugin.m"/>
3738
<source-file src="src/ios/TextResponseSerializer.m"/>
3839
<source-file src="src/ios/TextRequestSerializer.m"/>
@@ -42,6 +43,7 @@
4243
<source-file src="src/ios/AFNetworking/AFURLRequestSerialization.m"/>
4344
<source-file src="src/ios/AFNetworking/AFURLResponseSerialization.m"/>
4445
<source-file src="src/ios/AFNetworking/AFURLSessionManager.m"/>
46+
<source-file src="src/ios/SDNetworkActivityIndicator/SDNetworkActivityIndicator.m"/>
4547
<framework src="Security.framework"/>
4648
<framework src="SystemConfiguration.framework"/>
4749
</platform>

src/ios/CordovaHttpPlugin.m

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#import "TextResponseSerializer.h"
44
#import "TextRequestSerializer.h"
55
#import "AFHTTPSessionManager.h"
6+
#import "SDNetworkActivityIndicator.h"
67

78
@interface CordovaHttpPlugin()
89

@@ -172,6 +173,7 @@ - (void)post:(CDVInvokedUrlCommand*)command {
172173

173174
CordovaHttpPlugin* __weak weakSelf = self;
174175
manager.responseSerializer = [TextResponseSerializer serializer];
176+
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
175177

176178
@try {
177179
[manager POST:url parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) {
@@ -180,15 +182,18 @@ - (void)post:(CDVInvokedUrlCommand*)command {
180182

181183
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
182184
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
185+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
183186
} failure:^(NSURLSessionTask *task, NSError *error) {
184187
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
185188
[self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error];
186189

187190
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
188191
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
192+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
189193
}];
190194
}
191195
@catch (NSException *exception) {
196+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
192197
[self handleException:exception withCommand:command];
193198
}
194199
}
@@ -210,6 +215,7 @@ - (void)get:(CDVInvokedUrlCommand*)command {
210215

211216
CordovaHttpPlugin* __weak weakSelf = self;
212217
manager.responseSerializer = [TextResponseSerializer serializer];
218+
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
213219

214220
@try {
215221
[manager GET:url parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) {
@@ -218,15 +224,18 @@ - (void)get:(CDVInvokedUrlCommand*)command {
218224

219225
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
220226
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
227+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
221228
} failure:^(NSURLSessionTask *task, NSError *error) {
222229
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
223230
[self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error];
224231

225232
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
226233
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
234+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
227235
}];
228236
}
229237
@catch (NSException *exception) {
238+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
230239
[self handleException:exception withCommand:command];
231240
}
232241
}
@@ -248,6 +257,7 @@ - (void)put:(CDVInvokedUrlCommand*)command {
248257

249258
CordovaHttpPlugin* __weak weakSelf = self;
250259
manager.responseSerializer = [TextResponseSerializer serializer];
260+
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
251261

252262
@try {
253263
[manager PUT:url parameters:parameters success:^(NSURLSessionTask *task, id responseObject) {
@@ -256,15 +266,18 @@ - (void)put:(CDVInvokedUrlCommand*)command {
256266

257267
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
258268
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
269+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
259270
} failure:^(NSURLSessionTask *task, NSError *error) {
260271
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
261272
[self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error];
262273

263274
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
264275
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
276+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
265277
}];
266278
}
267279
@catch (NSException *exception) {
280+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
268281
[self handleException:exception withCommand:command];
269282
}
270283
}
@@ -286,6 +299,7 @@ - (void)patch:(CDVInvokedUrlCommand*)command {
286299

287300
CordovaHttpPlugin* __weak weakSelf = self;
288301
manager.responseSerializer = [TextResponseSerializer serializer];
302+
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
289303

290304
@try {
291305
[manager PATCH:url parameters:parameters success:^(NSURLSessionTask *task, id responseObject) {
@@ -294,15 +308,18 @@ - (void)patch:(CDVInvokedUrlCommand*)command {
294308

295309
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
296310
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
311+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
297312
} failure:^(NSURLSessionTask *task, NSError *error) {
298313
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
299314
[self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error];
300315

301316
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
302317
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
318+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
303319
}];
304320
}
305321
@catch (NSException *exception) {
322+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
306323
[self handleException:exception withCommand:command];
307324
}
308325
}
@@ -323,6 +340,7 @@ - (void)delete:(CDVInvokedUrlCommand*)command {
323340

324341
CordovaHttpPlugin* __weak weakSelf = self;
325342
manager.responseSerializer = [TextResponseSerializer serializer];
343+
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
326344

327345
@try {
328346
[manager DELETE:url parameters:parameters success:^(NSURLSessionTask *task, id responseObject) {
@@ -331,15 +349,18 @@ - (void)delete:(CDVInvokedUrlCommand*)command {
331349

332350
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
333351
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
352+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
334353
} failure:^(NSURLSessionTask *task, NSError *error) {
335354
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
336355
[self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error];
337356

338357
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
339358
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
359+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
340360
}];
341361
}
342362
@catch (NSException *exception) {
363+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
343364
[self handleException:exception withCommand:command];
344365
}
345366
}
@@ -358,6 +379,7 @@ - (void)head:(CDVInvokedUrlCommand*)command {
358379

359380
CordovaHttpPlugin* __weak weakSelf = self;
360381
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
382+
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
361383

362384
@try {
363385
[manager HEAD:url parameters:parameters success:^(NSURLSessionTask *task) {
@@ -367,15 +389,18 @@ - (void)head:(CDVInvokedUrlCommand*)command {
367389

368390
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
369391
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
392+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
370393
} failure:^(NSURLSessionTask *task, NSError *error) {
371394
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
372395
[self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error];
373396

374397
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
375398
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
399+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
376400
}];
377401
}
378402
@catch (NSException *exception) {
403+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
379404
[self handleException:exception withCommand:command];
380405
}
381406
}
@@ -399,6 +424,7 @@ - (void)uploadFile:(CDVInvokedUrlCommand*)command {
399424

400425
CordovaHttpPlugin* __weak weakSelf = self;
401426
manager.responseSerializer = [TextResponseSerializer serializer];
427+
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
402428

403429
@try {
404430
[manager POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
@@ -410,6 +436,7 @@ - (void)uploadFile:(CDVInvokedUrlCommand*)command {
410436
[dictionary setObject:@"Could not add file to post body." forKey:@"error"];
411437
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
412438
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
439+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
413440
return;
414441
}
415442
} progress:nil success:^(NSURLSessionTask *task, id responseObject) {
@@ -418,15 +445,18 @@ - (void)uploadFile:(CDVInvokedUrlCommand*)command {
418445

419446
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
420447
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
448+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
421449
} failure:^(NSURLSessionTask *task, NSError *error) {
422450
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
423451
[self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error];
424452

425453
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
426454
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
455+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
427456
}];
428457
}
429458
@catch (NSException *exception) {
459+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
430460
[self handleException:exception withCommand:command];
431461
}
432462
}
@@ -452,6 +482,7 @@ - (void)downloadFile:(CDVInvokedUrlCommand*)command {
452482

453483
CordovaHttpPlugin* __weak weakSelf = self;
454484
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
485+
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
455486

456487
@try {
457488
[manager GET:url parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) {
@@ -492,6 +523,7 @@ - (void)downloadFile:(CDVInvokedUrlCommand*)command {
492523
}
493524
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
494525
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
526+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
495527
return;
496528
}
497529
NSData *data = (NSData *)responseObject;
@@ -501,6 +533,7 @@ - (void)downloadFile:(CDVInvokedUrlCommand*)command {
501533
[dictionary setObject:@"Could not write the data to the given filePath." forKey:@"error"];
502534
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
503535
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
536+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
504537
return;
505538
}
506539

@@ -511,16 +544,19 @@ - (void)downloadFile:(CDVInvokedUrlCommand*)command {
511544

512545
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
513546
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
547+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
514548
} failure:^(NSURLSessionTask *task, NSError *error) {
515549
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
516550
[self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error];
517551
[dictionary setObject:@"There was an error downloading the file" forKey:@"error"];
518552

519553
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
520554
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
555+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
521556
}];
522557
}
523558
@catch (NSException *exception) {
559+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
524560
[self handleException:exception withCommand:command];
525561
}
526562
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2010 Olivier Poitrey <[email protected]>
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+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 <[email protected]>
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+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* This file is part of the SDNetworkActivityIndicator package.
3+
* (c) Olivier Poitrey <[email protected]>
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 <Foundation/Foundation.h>
10+
11+
@interface SDNetworkActivityIndicator : NSObject
12+
13+
+ (id)sharedActivityIndicator;
14+
- (void)startActivity;
15+
- (void)stopActivity;
16+
- (void)stopAllActivity;
17+
18+
@end

0 commit comments

Comments
 (0)