如何在iPhone中从后台调用Web服务

如何在iPhone中从后台调用Web服务,iphone,Iphone,我正在制作一个需要调用一个Web服务的应用程序,当用户在iPhone中按Button方法中的homebutton时,是否可以在后台调用与JSON相关的Web服务 //yourURL is the webservice URL [self performSelectorInBackground:@selector(loadDataFromWebservice:) withObject:yourURL]; -(void) loadDataFromWebservice : (NSString *

我正在制作一个需要调用一个Web服务的应用程序,当用户在iPhone中按Button方法中的homebutton时,是否可以在后台调用与JSON相关的Web服务

 //yourURL is the webservice URL
 [self performSelectorInBackground:@selector(loadDataFromWebservice:) withObject:yourURL];

-(void) loadDataFromWebservice : (NSString *)strUrl
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSURL *url=[[NSURL alloc] initWithString:strUrl];
    NSData *data = [NSData dataWithContentsOfURL:imgUrl];
    //Do your manipulations with data

    //If you want to update any UI with the webservice data 
    [self performSelectorOnMainThread:@selector(assignDataViews:) withObject:responseObject waitUntilDone:YES];
    [pool release];
}

//Return any object which you are comfortable with. I returned NSArray
-(void)assignDataViews : (NSArray *) yourObject
{
     //Do all your UI changes here
} 

您可以使用

NSMutableURLRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"Your webservice URL here"]];

[[NSURLConnection alloc]initWithRequest:request delegate:self];
您可以在其中实现委托方法