Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Photo Bomber keep crushing when instagram login is cancel

hi Photo Bomber keep crashing when Instagram login is cancel , i guess it crashing due to the response object , i am a bit confused please help

UINavigationBar *navigationBar = navigationController.navigationBar; navigationBar.barTintColor = [UIColor colorWithRed:232.0 / 255.0 green:129.0 / 255.0 blue:91.0 / 255.0 alpha:1.0]; navigationBar.barStyle = UIBarStyleBlackOpaque; navigationBar.tintColor = [UIColor whiteColor]; [self.collectionView registerClass:[THPhotoCell class] forCellWithReuseIdentifier:@"photo"]; self.collectionView.backgroundColor = [UIColor blackColor]; NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; self.accessToken = [userDefaults objectForKey:@"accessToken"];

if (self.accessToken == nil) {
    [SimpleAuth authorize:@"instagram" options:@{@"scope": @[@"likes"]} completion:^(NSDictionary *responseObject, NSError *error) {

        self.accessToken = responseObject[@"credentials"][@"token"];

        [userDefaults setObject:self.accessToken forKey:@"accessToken"];
        [userDefaults synchronize];

        [self refresh];
    }];
} else {
    [self refresh];
}
[self refresh];

[self refresh];}
  • (void)refresh { if (self.loading) { return; }

    self.loading = YES;

    NSURLSession *session = [NSURLSession sharedSession];

    // You can change the hashtag here to make your very own photo browser app! NSString *urlString = [[NSString alloc] initWithFormat:@"https://api.instagram.com/v1/tags/photobomber/media/recent?access_token=%@&count=500", self.accessToken];

    NSURL *url = [[NSURL alloc] initWithString:urlString]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url]; NSURLSessionDownloadTask *task = [session downloadTaskWithRequest:request completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {

    NSData *data = [[NSData alloc] initWithContentsOfURL:location];
    NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    
    self.photos = [responseDictionary valueForKeyPath:@"data"];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.collectionView reloadData];
        self.loading = NO;
    });
    

    }]; [task resume]; }