Skip to content

Instantly share code, notes, and snippets.

@micahwalter
Last active October 1, 2015 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micahwalter/5502eb28639af1a3f42d to your computer and use it in GitHub Desktop.
Save micahwalter/5502eb28639af1a3f42d to your computer and use it in GitHub Desktop.
Hello AppleTV
//
// ViewController.h
// Hello AppleTV
//
// Created by Micah Walter on 9/18/15.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (nonatomic, strong) IBOutlet UILabel *randomID;
@property (nonatomic, strong) IBOutlet UILabel *randomTitle;
@property (nonatomic, strong) IBOutlet UIImageView *imageView;
- (IBAction)fetchRandom;
@end
//
// ViewController.m
// Hello AppleTV
//
// Created by Micah Walter on 9/18/15.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (IBAction)fetchRandom;
{
// This is just a temp access key. It's probably expired by now!
NSString *temp_access_key = @"YOUR-API-TOKEN";
NSString *api_endpoint = @"https://api.collection.cooperhewitt.org/rest/";
NSString *method = @"cooperhewitt.objects.getRandom";
NSString *url_string = [api_endpoint stringByAppendingString:@"?method="];
url_string = [url_string stringByAppendingString:method];
url_string = [url_string stringByAppendingString:@"&access_token="];
url_string = [url_string stringByAppendingString:temp_access_key];
url_string = [url_string stringByAppendingString:@"&has_image=1"];
url_string = [url_string stringByAppendingString:@"&has_extras=images"];
NSURL *url = [NSURL URLWithString:url_string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response,
NSData *data, NSError *connectionError)
{
if (data.length > 0 && connectionError == nil)
{
NSDictionary *randomObject = [NSJSONSerialization JSONObjectWithData:data
options:0
error:NULL];
NSDictionary *objectData = [randomObject objectForKey:@"object"];
NSString *object_id = [objectData objectForKey:@"id"];
self.randomID.text = object_id;
NSString *object_title = [objectData objectForKey:@"title"];
self.randomTitle.text = object_title;
NSArray *images = [objectData objectForKey:@"images"];
NSDictionary *imagesDict = [images objectAtIndex:0];
NSDictionary *bImageDetails = [imagesDict objectForKey:@"b"];
NSURL *bImageURL = [NSURL URLWithString:[bImageDetails objectForKey:@"url"]];
NSData *imageData = [NSData dataWithContentsOfURL:bImageURL];
UIImage *bImage = [UIImage imageWithData:imageData];
[self.imageView setImage:bImage];
}
}];
}
UITapGestureRecognizer *tapRecognizer;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
tapRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
tapRecognizer.allowedPressTypes = @[[NSNumber numberWithInteger:UIPressTypeSelect]];
[self.view addGestureRecognizer:tapRecognizer];
[self fetchRandom];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)handleTap:(UITapGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
[self fetchRandom];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment