Seguendo questi spunti di codice di esempio creati con Xcode si leggono 3 elementi restituiti da una pagina php:
>> nel controller.h
@property (nonatomic, strong) NSMutableArray *jsonArray;
>> nel controller.m
#define getDataUrl @"http://www.example.com/data.php"
//...
@implementation OnlineViewController
@synthesize jsonArray;
- (void)viewWillAppear:(BOOL)animated
{
[self retrieveData];
}
//other code.....
#pragma mark -
#pragma mark Class Methods
-(void) retrieveData{
NSURL *url = [NSURL URLWithString:getDataUrl];
NSData *data = [NSData dataWithContentsOfURL:url];
jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
for (int i =0 ; i < jsonArray.count; i++){
NSString *cPosition = [NSString stringWithFormat:@"%@%d", @"",(i+1)];
NSString *cScore = [[jsonArray objectAtIndex:i] objectForKey:@"score_player"];
NSString *cName = [[jsonArray objectAtIndex:i] objectForKey:@"name_player"];
//...other code to set value on tableview
}
[self.tableView reloadData];
}