How can I send multiple URL requests from an NSURLConnection delegate?

This is the logical thread for my application:

  • First, when the view controller has finished loading, then the NSURLConnection request can start its execution

  • The response consists of XML data.

  • After parsing this xml, I need to send another NSURLConnection request.

  • After sending the second request, if the answer is ok, I get other xml data

  • After parsing the second xml, I need to check some problems between the first and second xml data.

So, is it possible to send multiple requests? How? I don't need code, you can just explain it.

Thanks in advance.

+4
source share
3 answers
 - (void)viedDidLoad{ [super viewDidLoad]; [self firstRequestMethod]; } - (void)firstRequestMethod{ NSString *myFirstRequestURL = @"<URL>"; NSURL *webURL = [NSURL URLWithString:myFirstRequestURL]; NSURLRequest *request = [NSURLRequest requestWithURL:webURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; NSError *error; NSURLResponse *response; NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; if(returnData) { NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding]; //Parse your response here. //Is desired response obtain call the second Request, as described above if (TRUE) { //on success [self secondRequestMethod]; } } } - (void)secondRequestMethod{ NSString *mySecondRequestURL = @"<URL>"; NSURL *webURL = [NSURL URLWithString:mySecondRequestURL]; NSURLRequest *request = [NSURLRequest requestWithURL:webURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; NSError *error; NSURLResponse *response; NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; if(returnData) { NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding]; //Parse your response here. //Is desired response obtain call the second Request, as described above if (TRUE) { //on success //subsequent calls to other url, same as above } } } 

Hope this helps you better understand ....

0
source

I do this using NSURLConnection, creating their properties and then checking which one:

  @property (nonatomic,retain) NSURLConnection *myConnection; @property (nonatomic,retain) NSURLConnection *mySecondConnection; 

then in the delegate:

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection{ if (connection == myConnection){ //do something } if (connection == mySecondConnection){ // do something else } } 

You can pass NSURLRequest to the connection:

 self.myConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
+14
source

There is a third-party library available that is a wrapper in CFNetwork, ASIHTTPREQUEST

This library should work for you. so you don’t need to write code from scratch. another alternative is to create one class that will be responsible for creating the NSURLConnection , and then send a request and finally notify to view the controller using the delegate or notification received.

+1
source

Source: https://habr.com/ru/post/1392602/


All Articles