在ios中启动URL

时间:2013-08-23 21:14:36

标签: ios

我的java webservice中有一个方法,它接受参数并运行sql查询来更新数据库。参数当然是从iphone插入到URL中,当url执行时,它将调用webservice方法来更新数据库。 我的问题是,如何在ios中执行URL但不能打开safari或其他任何内容。代码如下:

-(void)updateInfo
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Updating Profile" message:@"Your data have been saved." delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
    [alert setTag:02];
    [alert show];
}

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    GlobalObjects *global = [[GlobalObjects alloc]init];
    NSString *url =[NSString stringWithFormat:@"%@/profile/login/update/%@/%@/%@/%@/%@/%@/%@",global.domain,self.user, self.firstName.text,self.address.text,self.city.text,self.country.text,self.zipCode.text,self.phone.text];

    if (alertView.tag == 02 && buttonIndex != alertView.cancelButtonIndex) {
        // The URL execution method should come here.

    }
}

1 个答案:

答案 0 :(得分:1)

绝对最小(并且不是很好的练习)NSURLConnection示例:

NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]];
NSURLResponse * response = nil;
NSError * error = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest
                                      returningResponse:&response
                                                  error:&error];

if (error == nil)
{
    // Parse data here
}

你应该使用异步版本,以免阻塞主线程。检查this