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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Jonathan Fernandez](https://uploads.teamtreehouse.com/production/profile-photos/1083012/micro_Photo_on_2-25-14_at_3.06_PM.jpg)
Jonathan Fernandez
8,325 PointsCreating a logout Button for Photo Bombers App.. How to do this??
Hi Everyone,
I just finished the Photo Bombers course and I wanted to create a logout button on the navigation bar so that users can log out of their Instagram. I have tried to clear the accessToken and attempted to recheck for it so that if nill it would bring out the Login Page for users.
Shown Below is my Code:
<p>
//...
// ViewDidLoad in PhotoViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Photo Bombers";
// Creating Logout Button
UIBarButtonItem *logout = [[UIBarButtonItem alloc]initWithTitle:@"Log Out" style:UIBarButtonItemStylePlain target:self action:@selector(logout:)];
self.navigationItem.leftBarButtonItem = logout;
[self.collectionView registerClass:[JWFPhotoCell class] forCellWithReuseIdentifier:@"photo"];
self.collectionView.backgroundColor = [UIColor whiteColor];
[self checkForAccessToken];
}
//...
// Methods to be noted:
- (void)checkForAccessToken {
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"];
// Setting userDefaults to the accessToken.
[userDefaults setObject:self.accessToken forKey:@"accessToken"];
[userDefaults synchronize]; // Saves user defaults.
[self refresh];
}];
} else {
[self refresh];
}
}
- (void)refresh {
NSURLSession *session = [NSURLSession sharedSession];
NSString *urlString = [[NSString alloc] initWithFormat:@"https://api.instagram.com/v1/tags/photobomb/media/recent?access_token=%@", 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];
});
}];
[task resume];
}
// This is where I am implementing my method for logging out the User.
- (IBAction)logout:(id)sender {
self.accessToken = nil;
[self checkForAccessToken];
NSLog(@"Access Token: %@", self.accessToken);
}
</p>
So basically even if I clear the Access Token and set it to nil, the NSLog shows that it is still there. Is there something I'm not getting or doing right?
Any feedback will be greatly appreciated. : )
2 Answers
![Eliezer Marte](https://secure.gravatar.com/avatar/a36639cd20c613a89b55d1fb8c0051e3?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Eliezer Marte
4,728 PointsDo you know how to log in with another Instagram account it keeps saying forbidden?
Jonathan Fernandez
8,325 PointsJonathan Fernandez
8,325 PointsJust to note for anyone who is reading/following on with my code and cares about bar button color. I was able to make the logout Button white by adding the following code to my App Delegate in launch options: