Skip to content

Instantly share code, notes, and snippets.

@pizthewiz
pizthewiz / gist:1121036
Created August 2, 2011 19:47 — forked from mikeash/gist:837409
Block-based URL connection
// NSURLConnection wrapper
// like NSURLConnection, requires a runloop, callbacks happen in runloop that set up load
@interface LDURLLoader : NSObject {
NSURLConnection* _connection;
NSTimeInterval _timeout;
NSTimer* _timeoutTimer;
NSURLResponse* _response;
long long _responseLengthEstimate;
NSMutableData* _accumulatedData;
void (^_timeoutHandler)(void);
@pizthewiz
pizthewiz / gist:1135292
Created August 9, 2011 21:52
QC constants added in 10.7
#if defined(MAC_OS_X_VERSION_10_7) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7)
if (&QCPlugInAttributeCategoriesKey != NULL) {
NSArray* categories = [NSArray arrayWithObjects:@"Utility", nil];
[attributes setObject:categories forKey:QCPlugInAttributeCategoriesKey];
}
if (&QCPlugInAttributeExamplesKey != NULL) {
NSArray* examples = [NSArray arrayWithObjects:
[[NSBundle bundleForClass:[self class]] URLForResource:TTExampleCompositionName withExtension:@"qtz"], nil];
@pizthewiz
pizthewiz / gist:2789482
Created May 25, 2012 17:53
workaround iOS 4.3 bug in slicing variant of canvas's drawImage when scaling
function _equalRects(left, right) {
return left.origin.x === right.origin.x && left.origin.y === right.origin.y && left.size.width === right.size.width && left.size.height === right.size.height;
}
function _drawImage(context, image, sourceFrame, destinationFrame) {
// WORKAROUND - iOS 4.3 bug with the slicing variant of drawImage when scaling
if (!_equalRects(sourceFrame, destinationFrame)) {
context.save();
// clip
@pizthewiz
pizthewiz / gist:3949228
Last active October 12, 2015 01:18
NSDate category to deal with NTP timestamps
// yoinked and recrafted from Gavin Eadie's ios-ntp http://code.google.com/p/ios-ntp/
// NB - not perfectly symmetrical, this evaluates to NO:
// NSDate* now = [NSDate date]; [now isEqualToDate:[NSDate dateWithNTPTimestamp:[now NTPTimestamp]];
struct NTPTimestamp {
uint32_t seconds;
uint32_t fractionalSeconds;
};
typedef struct NTPTimestamp NTPTimestamp;
@pizthewiz
pizthewiz / gist:4465585
Last active December 10, 2015 17:08
sift through Settings.bundle for default values and register as user defaults. makes use of NSArray category from Mantle: https://github.com/github/Mantle/blob/master/Mantle/NSArray%2BMTLHigherOrderAdditions.h
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
// set default preferences from Settings.bundle
NSURL* plistURL = [[[NSBundle mainBundle] URLForResource:@"Settings" withExtension:@"bundle"] URLByAppendingPathComponent:@"Root.plist"];
if (!plistURL) {
NSLog(@"ERROR - failed to find 'Settings.bundle/Root.plist' within main bundle");
} else {
NSArray* specifiers = [NSDictionary dictionaryWithContentsOfURL:plistURL][@"PreferenceSpecifiers"];
NSDictionary* defaults = [specifiers mtl_foldLeftWithValue:[NSMutableDictionary dictionary] usingBlock:^id(NSMutableDictionary* left, NSDictionary* right) {
if (right[@"Key"] && right[@"DefaultValue"]) {
left[right[@"Key"]] = right[@"DefaultValue"];
@pizthewiz
pizthewiz / gist:4532416
Last active December 11, 2015 02:39
starting with version 3.1 (possibly 3.0, but i've only tested on 3.1.1) the iOS Facebook SDK disables the in-app UIWebView modal dialog (FBDialog) to authenticate the user and instead only enables iOS 6 system, Facebook.app and Mobile Safari. this hack was found and tested by stepping through the Facebook SDK source and building it manually into…
// GRITTY - force Facebook SDK 3.1+ to use in-app modal UIWebView for user authentication
@implementation FBSession (CLUAdditions)
- (void)HACKEDauthorizeWithPermissions:(NSArray*)permissions behavior:(FBSessionLoginBehavior)behavior defaultAudience:(FBSessionDefaultAudience)audience isReauthorize:(BOOL)isReauthorize {
objc_msgSend(self, @selector(authorizeWithPermissions:defaultAudience:integratedAuth:FBAppAuth:safariAuth:fallback:isReauthorize:), permissions, audience, NO, NO, NO, YES, isReauthorize);
}
+ (void)load {
method_exchangeImplementations(class_getInstanceMethod(self, @selector(authorizeWithPermissions:behavior:defaultAudience:isReauthorize:)), class_getInstanceMethod(self, @selector(HACKEDauthorizeWithPermissions:behavior:defaultAudience:isReauthorize:)));
}
@end
@pizthewiz
pizthewiz / gist:4658147
Created January 28, 2013 19:09
NSString category to validate contents as an email address
@interface NSString (CLUEmailAdditions)
- (BOOL)isValidEmailAddress;
@end
@implementation NSString (CLUEmailAdditions)
static NSString* CLUEmailAddressPattern = @"^([0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$";
// NB - mostly yoinked from https://github.com/jPaolantonio/JPValidation/blob/master/JPValidators/JPValidatorEmail.m
- (BOOL)isValidEmailAddress {
@pizthewiz
pizthewiz / gist:4658164
Created January 28, 2013 19:11
NSString category to validate contents as Twitter username.
@interface NSString (CLUTwitterAdditions)
- (BOOL)isValidTwitterUsername;
@end
@implementation NSString (CLUTwitterAdditions)
static NSString* CLUTwitterUsernamePattern = @"^([0-9a-zA-Z_]{1,15})$";
// NB - largely yoinked from http://stackoverflow.com/questions/4424179/twitter-username-regex-validation/4424288#4424288
- (BOOL)isValidTwitterUsername {
@pizthewiz
pizthewiz / of-no-hard-paths.diff
Created November 19, 2013 01:11
A project created by the projectGenerator in oF v0.8.0 uses an Xcode config file to define OF_PATH but the internal project file still contains several hardcoded references to the expected historical value of OF_PATH "../../..". A few that are fixable with the diff below are [1] the Copy Files build phase for the FMOD dylib [2] the definition of…
diff --git a/Project.xcconfig b/Project.xcconfig
index c90f7b1..6f6232a 100644
--- a/Project.xcconfig
+++ b/Project.xcconfig
@@ -13,5 +13,5 @@ ICON_FILE_PATH = $(OF_PATH)/libs/openFrameworksCompiled/project/osx/
//IF YOU WANT AN APP TO HAVE A CUSTOM ICON - PUT THEM IN YOUR DATA FOLDER AND CHANGE ICON_FILE_PATH to:
//ICON_FILE_PATH = bin/data/
-OTHER_LDFLAGS = $(OF_CORE_LIBS)
+OTHER_LDFLAGS = $(OF_CORE_LIBS) -framework GLUT
@pizthewiz
pizthewiz / shadyApp.cpp
Last active December 30, 2015 05:19
Simple oF application with GL2, GL3 and ES2 passthrough shaders on a non-power-of-two image (1024x768).
#include "shadyApp.h"
void shadyApp::setup() {
ofSetBackgroundColor(ofColor::gray);
ofSetFrameRate(60);
image.loadImage("checkerboard.jpg"); // 1024x768
string passthroughVertex, passthroughFragment;
#ifdef TARGET_OPENGLES