This example takes a simple view controller, calls the googlemaps api, and parses the returning JSON results.

The network call

NSURLConnection is the object that handles the HTTP request. You basically define the URL you are connecting to, implement the required delegates, and then get notified when the request is complete in the connectionDidFinishLoading method.

Parsing the JSON

NSJSONSerialization seems to currently be the best way to take your URL response and convert it into an iOS JSON object.

With this object you can then parse the response, looking at the keys and values, and then manually determine which elements you want to extract.

The code

Putting all that together, you get something that looks like this:

ViewController.m

#import "spike1ViewController.h"

@interface spike1ViewController()
@property (nonatomic, strong) NSMutableData *responseData;
@end

@implementation spike1ViewController

@synthesize responseData = _responseData;

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSLog(@"viewdidload");
    self.responseData = [NSMutableData data]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:
                             [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key=AIzaSyAbgGH36jnyow0MbJNP4g6INkMXqgKFfHk"]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
  
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"didReceiveResponse");
    [self.responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {        
    [self.responseData appendData:data]; 
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {    
    NSLog(@"didFailWithError");
    NSLog([NSString stringWithFormat:@"Connection failed: %@", [error description]]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"connectionDidFinishLoading");
    NSLog(@"Succeeded! Received %d bytes of data",[self.responseData length]);
    
    // convert to JSON
    NSError *myError = nil;
    NSDictionary *res = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:&myError];
    
    // show all values
    for(id key in res) {
        
        id value = [res objectForKey:key];
        
        NSString *keyAsString = (NSString *)key;
        NSString *valueAsString = (NSString *)value;
        
        NSLog(@"key: %@", keyAsString);
        NSLog(@"value: %@", valueAsString);
    }
    
    // extract specific value...
    NSArray *results = [res objectForKey:@"results"];

    for (NSDictionary *result in results) {
        NSString *icon = [result objectForKey:@"icon"];
        NSLog(@"icon: %@", icon);
    }

}

- (void)viewDidUnload {
    [super viewDidUnload];
}

@end

You can tell it’s working by looking at the output in the NSLogs. Should see something that looks like this:

Advertisements