Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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;

@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;


any help much appreciated

Sam Soffes please help . :)

1 Answer