October 2013
Intermediate to advanced
1053 pages
28h 7m
English
You want to call a web service using the HTTP DELETE method to delete a resource from a URL, and perhaps pass parameters, as part of the HTTP body or in the query string, to the web service.
Just as with the GET and POST methods, you can use the DELETE
method using NSURLConnection. You
must explicitly set your URL’s method to DELETE.
Let’s write a simple app that can create an asynchronous connection and send a few parameters as a query string and a few parameters in the HTTP body to the aforementioned URL, using the DELETE HTTP method:
-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{NSString*urlAsString=<#PlacetheURLofthewebserverhere#>;urlAsString=[urlAsStringstringByAppendingString:@"?param1=First"];urlAsString=[urlAsStringstringByAppendingString:@"¶m2=Second"];NSURL*url=[NSURLURLWithString:urlAsString];NSMutableURLRequest*urlRequest=[NSMutableURLRequestrequestWithURL:url];[urlRequestsetTimeoutInterval:30.0f];[urlRequestsetHTTPMethod:@"DELETE"];NSString*body=@"bodyParam1=BodyValue1&bodyParam2=BodyValue2";[urlRequestsetHTTPBody:[bodydataUsingEncoding:NSUTF8StringEncoding]];NSOperationQueue*queue=[[NSOperationQueuealloc]init];[NSURLConnectionsendAsynchronousRequest:urlRequestqueue:queuecompletionHandler:^(NSURLResponse*response,NSData*data,NSError*error){if([datalength]>0&& ...
Read now
Unlock full access