简体   繁体   中英

Multiple web service calls on same viewController iphone

I want help making multiple web service calls on the same view controller. Is there a way I can do it.

Thanks

There are several ways to solve this problem and each depends on your circumstance. The first would be to use multiple copies of + (id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error method of NSString. So if you wanted to get the contents of some URL you could 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];

The issue with this approach is that it will block whatever thread you call that on. So you can either call it in a thread or use one of the other approaches.

The second approach is to use NSURLConnection. This uses delegates to handle the process in an event driven fashion. There is a good summary of that approach here . But you will also need to differentiate between requests in the delegate methods. For example

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

The third approach is to use some kind of wrapper class that handles http requests at a higher level. Personally I like ASIHTTPRequest . It can handle requests synchronous, asynchronous using delegates, and asynchronous using blocks.

- (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];
}

This example shows you how to do an asynchronous request using blocks as the callbacks intsead of delegate methods. Note that this can only be used in iOS 4.0 and greater since it uses blocks. But ASIHTTPRequest in general can be used on iOS 3.0 and greater without blocks.

- (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];

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM