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!
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

Lachlan Dawson
10,007 PointsPhoto Bombers app crashing when device is not connected to network
Hi
I'm building the Photo Bombers app but have ran into a problem where the app crashes when the device is not connect to a network. I think the problem is in this bit of code
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];
}
I believe that it is crashing in the else {[self refresh];}
and I have tried to replace it with an alert but when I open the app only the alert shows up (even if connect to network).
Any help would be great! thank you
2 Answers

kerdeseverin
9,688 PointsIf its crashing then it might be because one of your objects is nil.
responseObject[@"credentials"][@"token"]
Since the connection was lost and no data was received, does responseObject have a value?

salem mohamed
577 Pointsi have the same problem, the app keeps crashing when the user cancel instagram login ANY fix?
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]; }
Lachlan Dawson
10,007 PointsLachlan Dawson
10,007 PointsSam Soffes Amit Bijlani Would you guys know how to fix this problem?