Wednesday, May 1, 2013

NSMutableURLRequest setCachePolicy example ios


setCachePolicy:

Sets the cache policy of the receiver.
- (void)setCachePolicy:(NSURLRequestCachePolicy)policy
Parameters of [NSMutableURLRequest setCachePolicy]
policy
The new cache policy.


Example of [NSMutableURLRequest setCachePolicy]

[webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML = \"\";"];

NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]];
[urlRequest setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];

[webView loadRequest:urlRequest];

Example of [NSMutableURLRequest setCachePolicy]
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *contentType = [NSString stringWithFormat:@"text/xml"];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[xmlMessage dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postBody];

//get response
NSHTTPURLResponse* urlResponse = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&httpError];