cs193p – Assignment #5 Task #1

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

Download the data at the URL provided by the FlickrFetcher class method URLforTopPlaces to get an array of the most popular Flickr photo spots in the last day or so. See the Hints for how to interpret data returned by Flickr.

Create a new single-view project. Make it an universal app (to be able to run it on the iPad). Add the Flickr-API to the project by dragging in the provided files (or get them from Shutterbug). Get the API key directly from Flickr and set it in FlickrAPIKey.h.

Create a new sub class from the Flickr fetcher class with a public instance method:

+ (void)loadTopPlacesOnCompletion:(void (^)(NSArray *photos, NSError *error))completionHandler;


The job of this method is to download the top-places data from Flickr, format it appropriately and – when done – provide it to the the completion handler. This handler is a block, because we do not yet know what the future code wants to do with the results from Flickr. The download itself is handled by creating a session with a task to do download the URL provided by the Flickr fetcher class. When the download has finished without error, generate a property list from the JSON data and use the entry for the place results to populate the places array. Finally run the completion block with that array (or nil if an error occurred):

+ (void)loadTopPlacesOnCompletion:(void (^)(NSArray *photos, NSError *error))completionHandler
{    
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration ephemeralSessionConfiguration]];
    NSURLSessionDownloadTask *task = [session downloadTaskWithURL:[FlickrHelper URLforTopPlaces]
                                                completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
                                                    NSArray *places;
                                                    if (!error) {
                                                        places = [[NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL:location]
                                                                                                 options:0
                                                                                                   error:&error] valueForKeyPath:FLICKR_RESULTS_PLACES];
                                                    }
                                                    dispatch_async(dispatch_get_main_queue(), ^{
                                                        completionHandler(places, error);
                                                    });
                                                }];
    [task resume];
}

For now – we will not use it later on – add some test code to the automatically generated view controller to see if the interface to Flickr works.

- (void)viewDidLoad
{
    [super viewDidLoad];
	
    [FlickrHelper loadTopPlacesOnCompletion:^(NSArray *photos, NSError *error) {
        NSLog(@"photos: %@\nerror: %@", photos, error);
    }];
    
}

The complete code is available on github.

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Flattr this!

6 thoughts on “cs193p – Assignment #5 Task #1”

  1. Hello,
    first off all, great work! I love your Website!

    My problem ist that Paul’s Code of the Shutterbug App doesn’t work. It doesn’t load the things from Flickr. Do you know a solution? 🙂

        1. if you can afford to not support previous iOS versions … and do not plan to publish till Xcode 6 is available as non-beta …

          Nevertheless, I noted that Flickr changed it’s API. Thus you need to change all “http://” strings in FlickrFether.m to “https://”

Leave a Reply

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