Wednesday, May 1, 2013

NSURLRequest initWithURL example ios


initWithURL:

Returns a URL request for a specified URL with default cache policy and timeout value.
- (id)initWithURL:(NSURL *)theURL
Parameters of [NSURLRequest initWithURL]
theURL
The URL for the request.
Return Value
The initialized URL request.
Discussion of [NSURLRequest initWithURL]
The default cache policy is NSURLRequestUseProtocolCachePolicy and the default timeout interval is 60 seconds.
Example of [NSURLRequest initWithURL]
// Create request
self.imageRequest = [[NSURLRequest alloc] initWithURL:imageURL 
                                          cachePolicy:(self.cachesImage) ? NSURLRequestReturnCacheDataElseLoad : NSURLRequestReloadIgnoringLocalCacheData 
                                      timeoutInterval:self.downloadTimeoutInterval];

// Begin download
self.imageData = nil;
self.imageConnection = [[NSURLConnection alloc] initWithRequest:self.imageRequest 
                                                       delegate:self 
                                               startImmediately:YES];
Example of [NSURLRequest initWithURL]
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:_url];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
self.connection = connection;
[connection release];
[request release];