diff --git a/CHANGELOG.md b/CHANGELOG.md index 708858f..e04286f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,9 @@ ## v1.6.0 +- Feature #18: implemented PATCH method (thanks akhatri for android implementation) +- Feature #21: added redirection control (thanks to notsyncing and kesozjura) - Fixed #16: cordova tries to run build script during plugin install -- Added redirection control (thanks to notsyncing and kesozjura) ## v1.5.10 diff --git a/src/ios/CordovaHttpPlugin.h b/src/ios/CordovaHttpPlugin.h index c2fca3a..8c7b93a 100644 --- a/src/ios/CordovaHttpPlugin.h +++ b/src/ios/CordovaHttpPlugin.h @@ -11,6 +11,7 @@ - (void)post:(CDVInvokedUrlCommand*)command; - (void)get:(CDVInvokedUrlCommand*)command; - (void)put:(CDVInvokedUrlCommand*)command; +- (void)patch:(CDVInvokedUrlCommand*)command; - (void)delete:(CDVInvokedUrlCommand*)command; - (void)uploadFile:(CDVInvokedUrlCommand*)command; - (void)downloadFile:(CDVInvokedUrlCommand*)command; diff --git a/src/ios/CordovaHttpPlugin.m b/src/ios/CordovaHttpPlugin.m index 8fe7c3a..d316a74 100644 --- a/src/ios/CordovaHttpPlugin.m +++ b/src/ios/CordovaHttpPlugin.m @@ -245,6 +245,38 @@ }]; } +- (void)patch:(CDVInvokedUrlCommand*)command { + AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; + manager.securityPolicy = securityPolicy; + + NSString *url = [command.arguments objectAtIndex:0]; + NSDictionary *parameters = [command.arguments objectAtIndex:1]; + NSString *serializerName = [command.arguments objectAtIndex:2]; + NSDictionary *headers = [command.arguments objectAtIndex:3]; + NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:4] doubleValue]; + + [self setRequestSerializer: serializerName forManager: manager]; + [self setRequestHeaders: headers forManager: manager]; + [self setTimeout:timeoutInSeconds forManager:manager]; + [self setRedirect: manager]; + + CordovaHttpPlugin* __weak weakSelf = self; + manager.responseSerializer = [TextResponseSerializer serializer]; + [manager PATCH:url parameters:parameters success:^(NSURLSessionTask *task, id responseObject) { + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; + [self handleSuccess:dictionary withResponse:(NSHTTPURLResponse*)task.response andData:responseObject]; + + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; + [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } failure:^(NSURLSessionTask *task, NSError *error) { + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; + [self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error]; + + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; + [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; +} + - (void)delete:(CDVInvokedUrlCommand*)command { AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.securityPolicy = securityPolicy; diff --git a/www/advanced-http.js b/www/advanced-http.js index c836b94..aa4ebcc 100644 --- a/www/advanced-http.js +++ b/www/advanced-http.js @@ -201,9 +201,7 @@ var http = { return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'put', [url, data, this.dataSerializer, headers, this.timeoutInSeconds]); }, -/* - * Disabled because PATCH method is not implemented for iOS - * + patch: function (url, data, headers, success, failure) { handleMissingCallbacks(success, failure); @@ -215,9 +213,9 @@ var http = { var onSuccess = injectCookieHandler(url, success); var onFail = injectCookieHandler(url, failure); - return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'patch', [url, data, this.dataSerializer, headers]); + return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'patch', [url, data, this.dataSerializer, headers, this.timeoutInSeconds]); }, -*/ + delete: function (url, params, headers, success, failure) { handleMissingCallbacks(success, failure);