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

Jonathan Fernandez
Jonathan Fernandez
8,325 Points

Creating 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. : )

Jonathan Fernandez
Jonathan Fernandez
8,325 Points

Just 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:

<p>
self.window.tintColor = [UIColor whiteColor];
</p>

2 Answers

Eliezer Marte
Eliezer Marte
4,728 Points

Do you know how to log in with another Instagram account it keeps saying forbidden?