Skip to content

Commit cb597c0

Browse files
committed
Added SDNetworkActivityIndicator library and integrated it in plugin.xml definition as well as CordovaHttpPlugin.m
This library allows to show the network activity indicator on iOS devices, to indicate native networking It uses a counter to hide the indicator depending on whether all request have been fulfilled or not
1 parent 2ae4c7c commit cb597c0

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
@@ -34,6 +34,7 @@
3434
<header-file src="src/ios/AFNetworking/AFURLRequestSerialization.h"/>
3535
<header-file src="src/ios/AFNetworking/AFURLResponseSerialization.h"/>
3636
<header-file src="src/ios/AFNetworking/AFURLSessionManager.h"/>
37+
<header-file src="src/ios/SDNetworkActivityIndicator/SDNetworkActivityIndicator.h"/>
3738
<source-file src="src/ios/CordovaHttpPlugin.m"/>
3839
<source-file src="src/ios/TextResponseSerializer.m"/>
3940
<source-file src="src/ios/TextRequestSerializer.m"/>
@@ -43,6 +44,7 @@
4344
<source-file src="src/ios/AFNetworking/AFURLRequestSerialization.m"/>
4445
<source-file src="src/ios/AFNetworking/AFURLResponseSerialization.m"/>
4546
<source-file src="src/ios/AFNetworking/AFURLSessionManager.m"/>
47+
<source-file src="src/ios/SDNetworkActivityIndicator/SDNetworkActivityIndicator.m"/>
4648
<framework src="Security.framework"/>
4749
<framework src="SystemConfiguration.framework"/>
4850
</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

@@ -175,6 +176,7 @@ - (void)post:(CDVInvokedUrlCommand*)command {
175176

176177
CordovaHttpPlugin* __weak weakSelf = self;
177178
manager.responseSerializer = [TextResponseSerializer serializer];
179+
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
178180

179181
@try {
180182
[manager POST:url parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) {
@@ -183,15 +185,18 @@ - (void)post:(CDVInvokedUrlCommand*)command {
183185

184186
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
185187
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
188+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
186189
} failure:^(NSURLSessionTask *task, NSError *error) {
187190
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
188191
[self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error];
189192

190193
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
191194
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
195+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
192196
}];
193197
}
194198
@catch (NSException *exception) {
199+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
195200
[self handleException:exception withCommand:command];
196201
}
197202
}
@@ -213,6 +218,7 @@ - (void)get:(CDVInvokedUrlCommand*)command {
213218

214219
CordovaHttpPlugin* __weak weakSelf = self;
215220
manager.responseSerializer = [TextResponseSerializer serializer];
221+
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
216222

217223
@try {
218224
[manager GET:url parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) {
@@ -221,15 +227,18 @@ - (void)get:(CDVInvokedUrlCommand*)command {
221227

222228
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
223229
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
230+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
224231
} failure:^(NSURLSessionTask *task, NSError *error) {
225232
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
226233
[self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error];
227234

228235
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
229236
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
237+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
230238
}];
231239
}
232240
@catch (NSException *exception) {
241+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
233242
[self handleException:exception withCommand:command];
234243
}
235244
}
@@ -251,6 +260,7 @@ - (void)put:(CDVInvokedUrlCommand*)command {
251260

252261
CordovaHttpPlugin* __weak weakSelf = self;
253262
manager.responseSerializer = [TextResponseSerializer serializer];
263+
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
254264

255265
@try {
256266
[manager PUT:url parameters:parameters success:^(NSURLSessionTask *task, id responseObject) {
@@ -259,15 +269,18 @@ - (void)put:(CDVInvokedUrlCommand*)command {
259269

260270
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
261271
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
272+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
262273
} failure:^(NSURLSessionTask *task, NSError *error) {
263274
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
264275
[self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error];
265276

266277
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
267278
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
279+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
268280
}];
269281
}
270282
@catch (NSException *exception) {
283+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
271284
[self handleException:exception withCommand:command];
272285
}
273286
}
@@ -289,6 +302,7 @@ - (void)patch:(CDVInvokedUrlCommand*)command {
289302

290303
CordovaHttpPlugin* __weak weakSelf = self;
291304
manager.responseSerializer = [TextResponseSerializer serializer];
305+
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
292306

293307
@try {
294308
[manager PATCH:url parameters:parameters success:^(NSURLSessionTask *task, id responseObject) {
@@ -297,15 +311,18 @@ - (void)patch:(CDVInvokedUrlCommand*)command {
297311

298312
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
299313
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
314+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
300315
} failure:^(NSURLSessionTask *task, NSError *error) {
301316
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
302317
[self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error];
303318

304319
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
305320
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
321+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
306322
}];
307323
}
308324
@catch (NSException *exception) {
325+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
309326
[self handleException:exception withCommand:command];
310327
}
311328
}
@@ -326,6 +343,7 @@ - (void)delete:(CDVInvokedUrlCommand*)command {
326343

327344
CordovaHttpPlugin* __weak weakSelf = self;
328345
manager.responseSerializer = [TextResponseSerializer serializer];
346+
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
329347

330348
@try {
331349
[manager DELETE:url parameters:parameters success:^(NSURLSessionTask *task, id responseObject) {
@@ -334,15 +352,18 @@ - (void)delete:(CDVInvokedUrlCommand*)command {
334352

335353
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
336354
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
355+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
337356
} failure:^(NSURLSessionTask *task, NSError *error) {
338357
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
339358
[self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error];
340359

341360
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
342361
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
362+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
343363
}];
344364
}
345365
@catch (NSException *exception) {
366+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
346367
[self handleException:exception withCommand:command];
347368
}
348369
}
@@ -361,6 +382,7 @@ - (void)head:(CDVInvokedUrlCommand*)command {
361382

362383
CordovaHttpPlugin* __weak weakSelf = self;
363384
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
385+
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
364386

365387
@try {
366388
[manager HEAD:url parameters:parameters success:^(NSURLSessionTask *task) {
@@ -370,15 +392,18 @@ - (void)head:(CDVInvokedUrlCommand*)command {
370392

371393
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
372394
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
395+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
373396
} failure:^(NSURLSessionTask *task, NSError *error) {
374397
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
375398
[self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error];
376399

377400
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
378401
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
402+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
379403
}];
380404
}
381405
@catch (NSException *exception) {
406+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
382407
[self handleException:exception withCommand:command];
383408
}
384409
}
@@ -402,6 +427,7 @@ - (void)uploadFile:(CDVInvokedUrlCommand*)command {
402427

403428
CordovaHttpPlugin* __weak weakSelf = self;
404429
manager.responseSerializer = [TextResponseSerializer serializer];
430+
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
405431

406432
@try {
407433
[manager POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
@@ -413,6 +439,7 @@ - (void)uploadFile:(CDVInvokedUrlCommand*)command {
413439
[dictionary setObject:@"Could not add file to post body." forKey:@"error"];
414440
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
415441
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
442+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
416443
return;
417444
}
418445
} progress:nil success:^(NSURLSessionTask *task, id responseObject) {
@@ -421,15 +448,18 @@ - (void)uploadFile:(CDVInvokedUrlCommand*)command {
421448

422449
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
423450
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
451+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
424452
} failure:^(NSURLSessionTask *task, NSError *error) {
425453
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
426454
[self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error];
427455

428456
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
429457
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
458+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
430459
}];
431460
}
432461
@catch (NSException *exception) {
462+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
433463
[self handleException:exception withCommand:command];
434464
}
435465
}
@@ -455,6 +485,7 @@ - (void)downloadFile:(CDVInvokedUrlCommand*)command {
455485

456486
CordovaHttpPlugin* __weak weakSelf = self;
457487
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
488+
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
458489

459490
@try {
460491
[manager GET:url parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) {
@@ -495,6 +526,7 @@ - (void)downloadFile:(CDVInvokedUrlCommand*)command {
495526
}
496527
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
497528
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
529+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
498530
return;
499531
}
500532
NSData *data = (NSData *)responseObject;
@@ -504,6 +536,7 @@ - (void)downloadFile:(CDVInvokedUrlCommand*)command {
504536
[dictionary setObject:@"Could not write the data to the given filePath." forKey:@"error"];
505537
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
506538
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
539+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
507540
return;
508541
}
509542

@@ -514,16 +547,19 @@ - (void)downloadFile:(CDVInvokedUrlCommand*)command {
514547

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

522556
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
523557
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
558+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
524559
}];
525560
}
526561
@catch (NSException *exception) {
562+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
527563
[self handleException:exception withCommand:command];
528564
}
529565
}
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)