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}
0 commit comments