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 Showing Photos in a Collection View

Display Instagram profile pictures of "follows" instead of recent pictures in UICollectionView

Hi Teamtreehousers,

I am a newbie iOS learner.

To get more familiar with the MVC theory and practice, I am trying to modify the Photo Bombers to create a UICollectionView that will show the pictures of the "follows" of the logged in Instagram user.

In the course material, the method imageForPhoto was created to get recent pictures relevant to the user, not the pictures of his follows.

The method's code is as follows:

+ (void)imageForPhoto:(NSDictionary *)photo size:(NSString *)size completion:(void(^)(UIImage *image))completion {
    if (photo == nil || size == nil || completion == nil) {
        return;
    }

    NSString *key = [[NSString alloc] initWithFormat:@"%@-%@", photo[@"id"], size];
    NSURL *url = [[NSURL alloc] initWithString:photo[@"images"][size][@"url"]];
    [self downloadURL:url key:key completion:completion];
}

Therefore, I first modified my code to get the data related to the "follows" in my THPhotosViewController:

NSURLSession *session = [NSURLSession sharedSession];
    NSString *urlString = [[NSString alloc] initWithFormat:@"https://api.instagram.com/v1/users/3/follows?count=99&access_token=%@", self.accessToken];

Then, I created a new method that I called friendAvatarForPhoto to get the follows profile pictures. I placed it in my THPhotoController class:

+ (void)friendAvatarForPhoto:(NSDictionary *)photo completion:(void(^)(UIImage *image))completion {
    if (photo == nil || completion == nil) {
        return;
    }

    NSString *key = [[NSString alloc] initWithFormat:@"avatar-%@", photo[@"user"][@"id"]];
    NSURL *url = [[NSURL alloc] initWithString:photo[@"profile_picture"]];
    [self downloadURL:url key:key completion:completion];
}

It seems to work but I am not sure that I am actually getting the element "profile_picture" from the responseDictionnary I get back from Instagram.

Here is the structure of this dictionary: https://www.dropbox.com/s/ldjqupadqg0j3nq/friends_response_dictionary.png

Specifically, as you can see, I modified these part:

NSString *key = [[NSString alloc] initWithFormat:@"avatar-%@", photo[@"user"][@"id"]];
    NSURL *url = [[NSURL alloc] initWithString:photo[@"profile_picture"]];

but I am not sure I am picking up the right keys... I kept "avatar" for example but why should I? I am pretty sure it's wrong.

I'd love to better understand the overall process and better connect the dots with Instagram's API so I can query the right key and be sure I am getting what I am looking for.

Any help from experienced developers in the community would be welcome, I've just started to explore iOS dev a few weeks ago based on rusty C skills from a long time ago.

Thank you! :) Arsene