1

I'm incredibly new at xcode and know a bit of coding but I'm trying to implement the use of an api in my app. Heres what I'm trying to do.

  • Take a geolocation from my first viewcontroller.
  • Take several variables from my second viewcontroller.
  • Use all collected variables and generate my HTTP REQUEST
  • Show returned data on my third viewcontroller.

I have set up my viewcontrollers and have my first viewcontroller locating me already.

Any help would be much appreiciated. I'm using the latest xcode on mountain lion, this is the request that needs to be sent http://yourtaximeter.com/api/?key=...

Evgeny Lazin
  • 9,193
  • 6
  • 47
  • 83

2 Answers2

0

Don't implement the logic in your viewcontrollers, use another class for all your connections. For example a "ConnectionManager" class or a "DataManager" class. Check out this question.

You could also check out AFNetworking and use their AFHTTPClient to create a subclass for your own api.

Community
  • 1
  • 1
Tieme
  • 62,602
  • 20
  • 102
  • 156
0

On the VC before the request, declare a property (and @synthesize) to keep the result of the network request.

@property (nonatomic, strong) NSData *responseData;

Then on whatever event triggers the request, start it like this:

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

Then pass the response data to the third VC like this:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([[segue identifier] isEqualToString:@"ThridVCSegue"]) {
        ThirdViewController *vc = (ThirdViewController *)[segue destinationViewController];
        [vc dataFromHTTPRequest:self.responseData];
    }
}

This assumes you'll use ARC, storyboards, and define that segue. ThirdViewController needs a public method to accept the http response data.

danh
  • 62,181
  • 10
  • 95
  • 136