Iphone 视图之间的Xcode HTTP GET请求

Iphone 视图之间的Xcode HTTP GET请求,iphone,xcode,http,geolocation,xmlhttprequest,Iphone,Xcode,Http,Geolocation,Xmlhttprequest,我对xcode非常陌生,知道一些编码,但我正在尝试在我的应用程序中实现api的使用。这就是我想做的 从我的第一个viewcontroller获取地理位置。 从我的第二个viewcontroller中获取几个变量。 使用所有收集的变量并生成我的HTTP请求 在我的第三个viewcontroller上显示返回的数据。 我已经设置了我的viewcontroller,并且我的第一个viewcontroller已经找到了我 任何帮助都会非常感激。我使用的是mountain lion上最新的xcode,这是

我对xcode非常陌生,知道一些编码,但我正在尝试在我的应用程序中实现api的使用。这就是我想做的

从我的第一个viewcontroller获取地理位置。 从我的第二个viewcontroller中获取几个变量。 使用所有收集的变量并生成我的HTTP请求 在我的第三个viewcontroller上显示返回的数据。 我已经设置了我的viewcontroller,并且我的第一个viewcontroller已经找到了我


任何帮助都会非常感激。我使用的是mountain lion上最新的xcode,这是需要发送的请求

不要在ViewController中实现逻辑,请为所有连接使用另一个类。例如,ConnectionManager类或DataManager类。检查问题


您还可以签出并使用它们为自己的api创建子类

在请求之前,在VC上声明一个属性并@synthesis以保留网络请求的结果

@property (nonatomic, strong) NSData *responseData;
然后在触发请求的任何事件上,按如下方式启动请求:

NSString *urlString = /* form the get request */
NSURL *url = [NSURL urlWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

// consider doing some UI on this VC to indicate that you're working on a request

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    if (!error) {
        self.responseData = data;
        // hide the "busy" UI
        // now go to the next VC with the response
        [self performSegueWithIdentifier:@"ThridVCSegue" sender:self];
    }
}];
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([[segue identifier] isEqualToString:@"ThridVCSegue"]) {
        ThirdViewController *vc = (ThirdViewController *)[segue destinationViewController];
        [vc dataFromHTTPRequest:self.responseData];
    }
}
然后将响应数据传递给第三个VC,如下所示:

NSString *urlString = /* form the get request */
NSURL *url = [NSURL urlWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

// consider doing some UI on this VC to indicate that you're working on a request

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    if (!error) {
        self.responseData = data;
        // hide the "busy" UI
        // now go to the next VC with the response
        [self performSegueWithIdentifier:@"ThridVCSegue" sender:self];
    }
}];
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([[segue identifier] isEqualToString:@"ThridVCSegue"]) {
        ThirdViewController *vc = (ThirdViewController *)[segue destinationViewController];
        [vc dataFromHTTPRequest:self.responseData];
    }
}
这假设您将使用ARC、故事板并定义该序列。ThirdViewController需要一个公共方法来接受http响应数据