iPhone – Protocol & Delegate Implementation

CustomHttpRequest.h

@protocol CustomHTTPRequestDelegate

@required
-(void) receivedDataForURL : (NSString *) reqUrl resultData: (NSDictionary *) result_;

@end
@property (retain) id delegate;

CustomHttpRequest.m
[[self delegate] receivedDataForURL:urlStr resultData:dic];

—–

.h
@interface VCAtm : UIViewController {
}

.m
CustomHttpRequest *httpReq = [[CustomHttpRequest alloc] initWithURL:url];
httpReq.delegate = self;

#pragma mark –
#pragma mark CustomHttpRequest Delegate

-(void) receivedDataForURL : (NSString *) reqUrl resultData:(NSDictionary *) result_{
NSLog(@”reqURL %@”, reqUrl);
NSLog(@”result data %@”, result_);

//show all values
for (id key in result_) {
id value = [result_ objectForKey:key];
NSString *keyAsString = (NSString *)key;
NSString *valueAsString = (NSString *)value;

NSLog(@”key: %@”, keyAsString);
NSLog(@”value: %@”, valueAsString);
}

NSArray *results = [result_ objectForKey:@”results”];
for (NSDictionary *result in results) {
NSString *name = [result objectForKey:@”name”];
[nameList addObject:name];
NSLog(@”Name : %@”, name);
}

atmTableView.delegate = self;
atmTableView.dataSource = self;
[atmTableView reloadData];

}

iPhone – Convert NSMutableData to ios JSON object and parse the data

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[self.responseData length];
//convert to json
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:nil];

//show all values
for (id key in dic) {
id value = [dic objectForKey:key];
NSString *keyAsString = (NSString *)key;
NSString *valueAsString = (NSString *)value;

NSLog(@"key: %@", keyAsString);
NSLog(@"value: %@", valueAsString);
}

NSArray *results = [dic objectForKey:@"results"];
for (NSDictionary *result in results) {
NSString *name = [result objectForKey:@"name"];

[nameList addObject:name];
NSLog(@"Name : %@", name);
}