Skip to content

Instantly share code, notes, and snippets.

View rsaunders100's full-sized avatar

Rob Saunders rsaunders100

View GitHub Profile
NSURL *url = [NSURL URLWithString:@"https://google.co.uk"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSError *error;
[NSURLConnection sendSynchronousRequest:request
returningResponse:nil
error:&error];
NSLog(@"NSURLConnection error: %@", error.localizedDescription);
[[[NSURLSession sharedSession]
@rsaunders100
rsaunders100 / gist:8972207
Created February 13, 2014 09:24
NSDateFormatter reuse
// For NSString to NSDate to parse server dates in the full ISO 8601 format
// E.g. 2014-02-11T10:22:46+00:00
+ (NSDateFormatter *) iso8601FullDateFromatter
{
static NSDateFormatter * dateFormatter;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
dateFormatter = [[NSDateFormatter alloc] init];
@rsaunders100
rsaunders100 / gist:8711151
Created January 30, 2014 15:35
Wrap HTML with a given UIColor and UIFont
+ (NSString *)htmlFromBodyString:(NSString *)htmlBodyString
textFont:(UIFont *)font
textColor:(UIColor *)textColor
{
int numComponents = CGColorGetNumberOfComponents([textColor CGColor]);
NSAssert(numComponents == 4 || numComponents == 2, @"Unsupported color format");
// E.g. FF00A5
NSString *colorHexString = nil;
@rsaunders100
rsaunders100 / gist:8436798
Created January 15, 2014 14:03
To prevent application logic from interfering with unit tests.
static BOOL isRunningTests(void) __attribute__((const));
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (isRunningTests()) {
return YES;
}
//
@rsaunders100
rsaunders100 / gist:8269049
Created January 5, 2014 14:47
Timed Scope Smoothed
CFAbsoluteTime startTimeStamp = CFAbsoluteTimeGetCurrent();
<#code to time#>
static NSTimeInterval smoothedTimeTaken = 0.0;
NSTimeInterval timeTaken = (CFAbsoluteTimeGetCurrent() - startTimeStamp);
if (smoothedTimeTaken == 0) smoothedTimeTaken = timeTaken;
else smoothedTimeTaken = smoothedTimeTaken * 0.9 + timeTaken * 0.1;
NSLog(@"Took %0.1f ms (smoothed) to <#name of task#>", smoothedTimeTaken * 1000);
@rsaunders100
rsaunders100 / ExampleUnitTest.m
Created December 4, 2013 12:15
How to unit an objective C class with an async callback.
- (void)testAsyncExample
{
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
__block BOOL hasRunBlock = NO;
[MyClass asyncCallback:^{
hasRunBlock = YES;
dispatch_semaphore_signal(semaphore);
}];
@rsaunders100
rsaunders100 / gist:6160147
Created August 5, 2013 22:25
Logs all unicode character with numeric value up to and including 2^16
+ (void) logCharacterSet:(NSCharacterSet*)characterSet
{
unichar unicharBuffer[20];
int index = 0;
for (unichar uc = 0; uc < (0xFFFF); uc ++)
{
if ([characterSet characterIsMember:uc])
{
unicharBuffer[index] = uc;
@rsaunders100
rsaunders100 / gist:6094708
Created July 27, 2013 12:14
Core image rendering on a EAGLContext
// View controller is a subclass of GLKViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.eaglContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
self.ciContext = [CIContext
contextWithEAGLContext:self.eaglContext
@rsaunders100
rsaunders100 / gist:6056183
Created July 22, 2013 18:17
Demonstrates the various NSDataWritingOptions. Should be used with a program like iExplorer to see how these file behave. http://www.macroplant.com/iexplorer/
[MyClass writeStringWithFileName:@"Complete"
option:NSDataWritingFileProtectionComplete];
[MyClass writeStringWithFileName:@"CompleteUnlessOpen"
option:NSDataWritingFileProtectionCompleteUnlessOpen];
[MyClass writeStringWithFileName:@"CompleteUntilFirstUserAuthentication"
option:NSDataWritingFileProtectionCompleteUntilFirstUserAuthentication];
[MyClass writeStringWithFileName:@"None"
@rsaunders100
rsaunders100 / UIImage+Transform.h
Created October 4, 2012 17:25
Transform an image threadsafe
//
// UIImage+Transform.h
// TestImage
//
// Created by Robert Saunders on 04/10/2012.
// Copyright (c) 2012 Robert Saunders. All rights reserved.
//
#import <UIKit/UIKit.h>