Wednesday, May 1, 2013

NSURLConnection sendSynchronousRequest example ios

sendSynchronousRequest :returningResponse:error:

Performs a synchronous load of the specified URL request.
+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error
Parameters of [NSURLConnection sendSynchronousRequest]
The URL request to load. The request object is deep-copied as part of the initialization process. Changes made to request after this method returns do not affect the request that is used for the loading process.
Out parameter for the URL response returned by the server.
Out parameter used if an error occurs while processing the request. May be NULL.
Return Value of [NSURLConnection sendSynchronousRequest]
The downloaded data for the URL request. Returns nil if a connection could not be created or if the download fails.
Discussion of [NSURLConnection sendSynchronousRequest]
A synchronous load is built on top of the asynchronous loading code made available by the class. The calling thread is blocked while the asynchronous loading system performs the URL load on a thread spawned specifically for this load request. No special threading or run loop configuration is necessary in the calling thread in order to perform a synchronous load.

If authentication is required in order to download the request, the required credentials must be specified as part of the URL. If authentication fails, or credentials are missing, the connection will attempt to continue without credentials.
Example of [NSURLConnection sendSynchronousRequest]
NSError        *error = nil;
NSURLResponse  *response = nil;

[NSURLConnection sendSynchronousRequest: req returningResponse: &response error: &error];

if (error) {...handle the error}
Example of [NSURLConnection sendSynchronousRequest]
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
Example of [NSURLConnection sendSynchronousRequest]
NSString *urlString = TEST_CONNECTION;
NSError *error = nil;
NSHTTPURLResponse *response = nil;
NSURLRequest *request = [NSURLRequest
                         requestWithURL:[NSURL URLWithString:urlString]

NSData *conn = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];