Tuesday 18 June 2013

NSURLConnection Asynchronous Request

An NSURLConnection object provides support to perform the loading of a URL request. The interface for NSURLConnection is sparse, providing only the controls to start and cancel asynchronous loads of a URL request.
NSURLConnection’s delegate methods—defined by the NSURLConnectionDelegate Protocol protocol—allow an object to receive informational callbacks about the asynchronous load of a URL request. Other delegate methods provide facilities that allow the delegate to customize the process of performing an asynchronous URL load. These delegate methods are called on the thread that started the asynchronous load operation for the associated NSURLConnection object.
NSURLConnection has a convenience class method, sendSynchronousRequest:returningResponse:error:, to load a URL request synchronously.

Example Code





-----------------------------------------------------------------
NSString *strURL= [NSString stringWithFormat:@"http://www.google.com/"];//Here is pass your json link
NSURL *URL = [NSURL URLWithString:[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSURLRequest *requestURL = [[NSURLRequest alloc] initWithURL:URL];
    [NSURLConnection sendAsynchronousRequest:requestURL
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {
         //When Json request complite then call this block
         NSLog(@"Json Call Done Perform Your Action with data");
         NSLog(@"Reply is:%@",[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]);
     }];
-----------------------------------------------------------------

Additional References



Here is example to use NSURLConnection with asynchronouse. You can use this to call your request while other process is running. Any other useful way to use this code or any correction then please share it. Thank you...:)

1 comment:

  1. if i want to call soap request twice,thrice,,, for different function and getting response from different related function from same asmx file in ios7. how it does? please help me…Thank You

    ReplyDelete