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

时间:2011-07-08 12:34:08

标签: iphone

我正在创建一个需要调用一个web服务的应用程序,当用户在iPhone中按home键时,是否可以在后台调用与JSON相关的webservices

3 个答案:

答案 0 :(得分:0)

在按钮方法

 //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
} 

答案 1 :(得分:0)

您可以使用

调用异步请求
NSMutableURLRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"Your webservice URL here"]];

[[NSURLConnection alloc]initWithRequest:request delegate:self];

您可以在其中实现委托方法

答案 2 :(得分:0)

使用这个tuttorial可能会对你有所帮助 See this link