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 Build a Photo Browser iPhone App Connecting to an API using OAuth Caching Photos

How to implement SAMCache On this code

Hey guys. i'm hoping from anybody to help regarding how to implement SAMcache for my code below.

//
//  RootViewController.m
//  
//
//  Created by hamdan mohamad on 5/17/14.
//
//

#import "RootViewController.h"
#import <SimpleAuth/SimpleAuth.h>
#import <SAMCache/SAMCache.h>

@interface RootViewController ()
@property (nonatomic) NSArray *dataFromInstagramJSON;
@property (nonatomic) NSString *token;
@property (nonatomic) NSDictionary *jsonData;
@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    //get the access token
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    self.token = [userDefaults objectForKey:@"accessToken"];

    if (self.token == nil) {
        [SimpleAuth authorize:@"instagram" options:@{@"scope":@[@"likes"]} completion:^(id responseObject, NSError *error) {
            self.token = responseObject[@"credentials"][@"token"];
            //NSLog(@"data from insta : %@", responseObject);

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

            [self instagramData];
        }];
    }
    else {

        [self instagramData];

    }

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)instagramData {





    NSURLSession *session = [NSURLSession sharedSession];

    NSString *urlString = [[NSString alloc] initWithFormat:@"https://api.instagram.com/v1/users/self/feed?access_token=%@", self.token];

    NSURL *url = [NSURL URLWithString:urlString];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    NSURLSessionDownloadTask *task = [session downloadTaskWithRequest:request completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {

        NSData *data = [NSData dataWithContentsOfURL:location];
        self.jsonData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

        self.dataFromInstagramJSON = [self.jsonData valueForKeyPath:@"data"];

        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
        });

    }];



    [task resume];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.dataFromInstagramJSON.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

    cell.backgroundColor = [UIColor lightGrayColor];

    NSDictionary *picURL = self.dataFromInstagramJSON[indexPath.row];

    NSString *urlString = picURL[@"images"][@"standard_resolution"][@"url"];
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]]];

    UIImageView *imageView = (UIImageView *)[cell viewWithTag:10];
    imageView.image = image;

    return cell;
}

@end

any help much appreciated

Sam Soffes please help . :)

1 Answer