Multiple web service calls on the same iphone viewController

I need help creating multiple web service calls on one controller. Is there a way I can do this.

thank

+5
source share
1 answer

There are several ways to solve this problem, and each depends on your circumstances. The first would be to use multiple copies of the method + (id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)errorfor NSString. Therefore, if you want to get the contents of some URL, you can use the following code

NSURL* url = [NSURL urlWithString:@"http://www.someUrl.com/some/path"];
NSString* urlContents = [NSString stringWithContentsOfURL:url encoding:NSUTF8Encoding error:nil];
NSURL* anotherUrl = [NSURL urlWithString:@"http://www.anotherUrl.com/some/path"];
NSString* anotherUrlContents = [NSString stringWithContentsOfURL:anotherUrl encoding:NSUTF8Encoding error:nil];

, , . , .

NSURLConnection. , . . .

-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *) response
{
    if(connection == connection1)
    {
        //Do something with connection 1
    }
    else if(connection == connection2)
    {
        //Do something with connection 2    
    }
}

- - -, HTTP- . ASIHTTPRequest. , .

- (IBAction)grabURLInBackground:(id)sender
{
   NSURL *url1 = [NSURL URLWithString:@"http://example.com/path/1"];
   ASIHTTPRequest *request1 = [ASIHTTPRequest requestWithURL:url1];
   request1.delegate = self;
   request1.didFinishSelector = @selector(request1DidFinish);
   [request1 startAsynchronous];

   NSURL *url2 = [NSURL URLWithString:@"http://example.com/path/2"];
   ASIHTTPRequest *request2 = [ASIHTTPRequest requestWithURL:url2];
   request2.delegate = self;
   request2.didFinishSelector = @selector(request2DidFinish);
   [reques2 startAsynchronous];
}

- (void)request1DidFinish:(ASIHTTPRequest *)request
{
   NSString *responseString = [request responseString];
}

- (void)request2DidFinish:(ASIHTTPRequest *)request
{
   NSString *responseString = [request responseString];
}

, callbacks intsead . , iOS 4.0 , . ASIHTTPRequest iOS 3.0 .

- (IBAction)grabURLInBackground:(id)sender
{
   NSURL *url = [NSURL URLWithString:@"http://example.com/path/1"];
   __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
   [request setCompletionBlock:^{
      NSString *responseString = [request responseString];
   }];
   [request startAsynchronous];

   NSURL *url2 = [NSURL URLWithString:@"http://example.com/path/2"];
   __block ASIHTTPRequest *request2 = [ASIHTTPRequest requestWithURL:url];
   [request2 setCompletionBlock:^{
      NSString *responseString = [request2 responseString];
   }];
   [request2 startAsynchronous];

}
+18

All Articles