Wednesday, May 1, 2013

NSURLResponse initWithURL example ios


initWithURL :MIMEType:expectedContentLength:textEncodingName:

Returns an initialized NSURLResponse object with the URL, MIME type, length, and text encoding set to given values.
- (id)initWithURL:(NSURL *)URL MIMEType:(NSString *)MIMETypeexpectedContentLength:(NSInteger)length textEncodingName:(NSString *)name
Parameters of [NSURLResponse initWithURL]
URL
The URL for the new object.
MIMEType
The MIME type.
length
The expected content length.This value should be –1 if the expected length is undetermined
name
The text encoding name. This value may be nil.
Return Value of [NSURLResponse initWithURL]
An initialized NSURLResponse object with the URL set to URL, the MIME type set toMIMEType, length set to length, and text encoding name set to name.
Discussion
This is the designated initializer for NSURLResponse.
Example of [NSURLResponse initWithURL]
NSURLRequest *request =  [NSURLRequest 
       requestWithURL:cachedURL 
       cachePolicy:NSURLRequestReloadIgnoringLocalCacheData 
       timeoutInterval:60.0] ;

 NSURLResponse *responseToCache = 
      [[NSURLResponse alloc]
       initWithURL:[request URL] 
       MIMEType:@"text/html" 
       expectedContentLength:[dataAtURL length] 
       textEncodingName:nil] ;

 NSCachedURLResponse *cachedResponse = 
      [[NSCachedURLResponse alloc]
       initWithResponse:responseToCache data:dataAtURL
       userInfo:nil storagePolicy:NSURLCacheStorageAllowedInMemoryOnly] ;

  // Store it
 [[NSURLCache sharedURLCache] storeCachedResponse:cachedResponse forRequest:request] ;

 [responseToCache release] ;
 [cachedResponse release] ;

 NSLog( @"*** request %@, cache=%@", request ,[[NSURLCache sharedURLCache] 
   cachedResponseForRequest:request]