Skip to content

Instantly share code, notes, and snippets.

View nickjshearer's full-sized avatar

Nick Shearer nickjshearer

  • Apple Inc.
  • London
View GitHub Profile
@nickjshearer
nickjshearer / dialup.js
Last active August 29, 2015 14:18
Dial Up Images
// Dial-up modem image loading using canvas. Relive the glory days of the internet
//
// Usage:
// <canvas class="image-canvas" width="180" height="180" data-image="http://lorempixel.com/180/180/">
// <p>Image alt textc</p>
// </canvas>
//
window.onload = function() {
var elements = document.getElementsByClassName("image-canvas");
@nickjshearer
nickjshearer / gist:5662839
Created May 28, 2013 13:41
Example NSURLCache setup
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Create a 4MB in-memory, 32MB disk cache
NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:4*1024*1024
diskCapacity:32*1024*1024
diskPath:@"app_cache"];
// Set the shared cache to our new instance
[NSURLCache setSharedURLCache:cache];
}
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}
NSURLRequest *urlReq = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:30.0];
AFHTTPRequestOperation *request = [[AFHTTPRequestOperation alloc] initWithRequest:urlReq];
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Create a 4MB in-memory, 32MB disk cache
NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:4*1024*1024
diskCapacity:32*1024*1024
diskPath:@"app_cache"];
// Set the shared cache to our new instance
[NSURLCache setSharedURLCache:cache];
}
@nickjshearer
nickjshearer / gist:5659166
Created May 27, 2013 21:25
Simple AFNetworking example with JSON
NSURL *url = [NSURL URLWithString:@"http://content.guardianapis.com/search?format=json"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// Prepare the operation with completion handlers
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"Latest stories: %@", [JSON valueForKeyPath:@"response.results"]);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){
NSLog(@"There was a problem: %@", [error localizedDescription]);
}];
@implementation Article
- (id)initWithDictionary:(NSDictionary *)dictionary {
self = [self init];
if (self != nil) {
self.title = jsonObject[@"title"];
self.body = jsonObject[@"body"];
self.url = [NSURL URLWithString:jsonObject[@"url"]];
self.dateModified = [self.dateFormatter dateFromString:jsonObject[@"modified"]];
// etc etc etc
@nickjshearer
nickjshearer / gist:5459985
Created April 25, 2013 14:08
Mantle Example
@interface Article : MTLModel <MTLJSONSerializing>
@property (nonatomic) NSString *title;
@property (nonatomic) NSString *body;
@property (nonatomic) NSURL *url;
@property (nonatmoic) NSDate *dateModified;
@implementation Article
@nickjshearer
nickjshearer / gist:5361836
Created April 11, 2013 08:52
Sample recursive description
(lldb) po [[[UIApp delegate] window] recursiveDescription]
$6 = 0x0a596190 <UIWindow: 0xa58b880; frame = (0 0; 768 1024)[...]
| <UILayoutContainerView: 0xc0a8460; frame = (0 0; 768 1024)[...]
| | <UINavigationTransitionView: 0xc0aa8e0; frame = (0 0; 768 1024)[...]
| | | <UIViewControllerWrapperView: 0xc1b99f0; frame = (0 20; 768 1004)[...]
| | | | <UIView: 0xc1a96c0; frame = (0 0; 768 1004)[...]