I'm building my first real iOS app - I'm a webdeveloper from origin. It communicates with a server and then gets data back from the server, formatted like this:
{
"data": {
"firstname": "Test",
"lastname": "User",
"username": "TestUser",
"photo_link": "http://placehold.it/500x500"
}
}
However, when I try to use these variables, I always get 'null'. res
is outputted correctly, though, so that's okay. What am I doing wrong?
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *xAuthToken = [[NSUserDefaults standardUserDefaults]
stringForKey:@"AuthToken"];
NSLog(xAuthToken);
self.responseData = [NSMutableData data];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"http://www.example.com/api/user"]];
NSMutableURLRequest *mutableRequest = [request mutableCopy];
[mutableRequest addValue:xAuthToken forHTTPHeaderField:@"X-Auth-Token"];
request = [mutableRequest copy];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated
{
[self.navigationController setToolbarHidden:YES animated:YES];
}
- (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];
NSDictionary *switchValues = [res objectForKey:@"data"];
NSString *photoLink = [switchValues objectForKey:@"photo_link"];
NSString *firstName = [switchValues objectForKey:@"firstname"];
NSString *lastName = [switchValues objectForKey:@"lastname"];
self.tableView.separatorColor = [UIColor colorWithRed:150/255.0f green:161/255.0f blue:177/255.0f alpha:1.0f];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.opaque = NO;
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.tableHeaderView = ({
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 184.0f)];
NSURL *url = [NSURL URLWithString: photoLink];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 40, 100, 100)];
imageView.image = image;
imageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
imageView.layer.masksToBounds = YES;
imageView.layer.cornerRadius = 50.0;
imageView.layer.borderColor = [UIColor whiteColor].CGColor;
imageView.layer.borderWidth = 3.0f;
imageView.layer.rasterizationScale = [UIScreen mainScreen].scale;
imageView.layer.shouldRasterize = YES;
imageView.clipsToBounds = YES;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 150, 0, 24)];
label.text = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
label.font = [UIFont fontWithName:@"HelveticaNeue" size:21];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor colorWithRed:62/255.0f green:68/255.0f blue:75/255.0f alpha:1.0f];
[label sizeToFit];
label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
[view addSubview:imageView];
[view addSubview:label];
view;
});
};
res
outputs:
{
"data": {
"firstname": "Test",
"lastname": "User",
"username": "TestUser",
"photo_link": "http://placehold.it/500x500"
}
}