Skip to content

Instantly share code, notes, and snippets.

View chrishulbert's full-sized avatar

Chris Hulbert chrishulbert

View GitHub Profile
!!! There's a better solution here now, that doesn't require swizzling:
!!! http://www.splinter.com.au/2014/09/10/afnetworking-error-bodies/
!!! But i'll leave the below swizzling solution up for old time's sake
// AFURLSessionManager+ErrorResponse.h
// This hacks AFURLSessionManager so that it returns the error response in failure callbacks, in the error's userInfo.
// Usage:
// [mySessionManager POST:@"some_api_endpoint" parameters:params success:^(NSURLSessionDataTask *task, NSDictionary *responseObject) {
import UIKit
extension UIColor {
/// Creates a UIColor from an 'rrggbb' hex string.
class func fromHex(hexColour: String) -> UIColor {
let str = hexColour.cStringUsingEncoding(NSUTF8StringEncoding)
let x = strtol(str!, nil, 16)
let r = x >> 16
let g = (x >> 8) & 0xff
@chrishulbert
chrishulbert / WeakDelegates.swift
Created March 3, 2015 04:26
Swift implementation of C#-style Events concept (eg a manager with multiple weakly-referenced delegates)
import Foundation
/// Since there can be multiple delegates, they can only be used to signifying events (output), not for
/// supplying data for the manager (input).
@objc protocol MyManagerDelegate {
func manager(manager: MyManager, isNowLoading: Bool)
}
@objc class MyManager {
@chrishulbert
chrishulbert / MyNavigationController.m
Created March 5, 2015 03:10
Allows you to slide your navigation bar away when hiding it
// Header:
@interface MyNavigationController : UINavigationController
- (void)setNavigationBarSlidAway:(BOOL)slidAway animated:(BOOL)animated;
@end
// Implementation:
//
// Keychain.swift
// SwiftKeychain
//
// Created by Chris Hulbert on 14/06/2015.
// Copyright (c) 2015 Chris Hulbert. All rights reserved.
//
import Foundation
import Security
import Foundation
import Security
struct SecItemWrapper {
static func matching(query: NSDictionary) -> (status: Status, result: AnyObject?) {
var rawResult: Unmanaged<AnyObject>?
let rawStatus = SecItemCopyMatching(query, &rawResult)
let result: AnyObject? = rawResult?.takeRetainedValue()
let status = Status.fromOSStatus(rawStatus)
return (status, result)
@chrishulbert
chrishulbert / buildquerystring.m
Created April 13, 2011 02:17
Build a url query string in obj-c from a dictionary of params like jquery does
+(NSString*)urlEscape:(NSString *)unencodedString {
NSString *s = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)unencodedString,
NULL,
(CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
kCFStringEncodingUTF8);
return [s autorelease]; // Due to the 'create rule' we own the above and must autorelease it
}
// Put a query string onto the end of a url
@chrishulbert
chrishulbert / SizableImageCell.m
Created April 21, 2011 06:18
Make a UITableViewCell with a resized image
@interface SizableImageCell : UITableViewCell {}
@end
@implementation SizableImageCell
- (void)layoutSubviews {
[super layoutSubviews];
float desiredWidth = 80;
float w=self.imageView.frame.size.width;
if (w>desiredWidth) {
float widthSub = w - desiredWidth;
@chrishulbert
chrishulbert / multi line label.m
Created April 28, 2011 04:59
How to make a multiline label with dynamic text on the iphone and get the correct height
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 9999)];
myLabel.lineBreakMode = UILineBreakModeWordWrap;
myLabel.numberOfLines = 0;
myLabel.text = @"Some \n dynamic \n multiline \n text";
[myLabel sizeToFit]; // This shrinks the 9999 down to the size of the text
NSLog(@"Actual height is: %f", myLabel.frame.size.height); // Use this for spacing any further elements
[self.view addSubview:title]; // Or add it to a scroll view, or whatever...
[myLabel release];
@chrishulbert
chrishulbert / centered_header.m
Created May 6, 2011 03:58
Centered header on a uitableview
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel *lbl = [[[UILabel alloc] init] autorelease];
lbl.textAlignment = UITextAlignmentCenter;
lbl.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
lbl.text = @"My Centered Header";
lbl.textColor = [UIColor whiteColor];
lbl.shadowColor = [UIColor grayColor];
lbl.shadowOffset = CGSizeMake(0,1);
lbl.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"my_head_bg"]];
lbl.alpha = 0.9;