cs193p – Assignment #6 Part #2

cs193p – Assignment #6 Part #2

Please note, this blog entry is from a previous course. You might want to check out the current one.

… back to the actual task … prevent the “old” top-places table view controller from loading photos we do not need any more:

- (void)viewDidLoad
    [super viewDidLoad];
//    [self fetchPlaces];

Download Photos from Flickr using URLforRecentGeoreferencedPhotos:

Assignment #6 Task #1

Your application must work identically to last week except that where you are displaying the “top places” (as reported by Flickr), you are going to display the “top regions” in which photos have been taken. You will calculate the most popular regions from the data you gather periodically from the URLforRecentGeoreferencedPhotos.

… let’s start the initial fetch from the application delegate:

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    [self startFlickrFetch];
    return YES;

Let’s assume we have not read task #7, and just perform a “normal fetch” for which we use a helper function from the FlickrHelper class. As soon as we get a result show how many photos we got … (hopefully 250) …

- (void)startFlickrFetch
    [FlickrHelper loadRecentPhotosOnCompletion:^(NSArray *photos, NSError *error) {
        if (error) {
            NSLog(@"Flickr background fetch failed: %@", error.localizedDescription);
        } else {
            NSLog(@"%d photos fetched", [photos count]);

The helper method just opens a new ephemeral session, accesses Flickr, tries to get photo data from the received JSON file, and runs the completion handler (which is for now a log message):

//  FlickrHelper.h
+ (void)loadRecentPhotosOnCompletion:(void (^)(NSArray *places, NSError *error))completionHandler;

//  FlickrHelper.m
+ (void)loadRecentPhotosOnCompletion:(void (^)(NSArray *places, NSError *error))completionHandler
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration ephemeralSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
    NSURLSessionDownloadTask *task = [session downloadTaskWithURL:[FlickrHelper URLforRecentGeoreferencedPhotos]
                                                completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
                                                    NSArray *photos;
                                                    if (!error) {
                                                        photos = [[NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL:location]
                                                                                                    error:&error] valueForKeyPath:FLICKR_RESULTS_PHOTOS];
                                                    dispatch_async(dispatch_get_main_queue(), ^{
                                                        completionHandler(photos, error);
    [task resume];

… and if you run it … it will return nothing … Flickr just changed its API and does only allow https requests. The FlickrFetcher files provided by Stanford only use http URLs. Therefore you need to change all “http://” strings to “https://” in FlickrFetcher.m.

The complete code for tasks #1 to #7 is available on github.


Flattr this!

2 thoughts on “cs193p – Assignment #6 Part #2”

  1. Hi there,

    Thanks so much for this blog, it’s super helpful. I had a quick question – I noticed the same thing about the http / https, but it took me forever to figure it out and I can’t even remember how (I’m just revisiting iOS stuff now). How do you debug such things? e.g. if all you’re doing is sending a network request, and all you get back is “You don’t have permission” or whatever from Flickr, how do you debug this in order to figure out that it should be https?


    1. Most of the time, you are not the only one having the same problem … Google solves a lot of problems … in that very case, the Flickr-help forum did the job …

Leave a Reply

Your email address will not be published. Required fields are marked *