Skip to content

Instantly share code, notes, and snippets.

View cwagdev's full-sized avatar

Chris Wagner cwagdev

View GitHub Profile
@cwagdev
cwagdev / gist:5103091
Last active December 14, 2015 14:48
Detect UINavigation back button press
- (void)viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
// back button was pressed. We know this is true because self is no longer
// in the navigation stack.
}
[super viewWillDisappear:animated];
}
@cwagdev
cwagdev / UIColor+Grayscale.h
Created August 2, 2013 09:11
Convert a UIColor color to its grayscale equivalent
#import <UIKit/UIKit.h>
@interface UIColor (Grayscale)
- (UIColor *)grayscale;
@end
@cwagdev
cwagdev / gist:7549537
Last active December 28, 2015 19:19
Handle keyboard presentation and dismissal with scroll views
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardShowNotification:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardHideNotification:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@cwagdev
cwagdev / gist:7759777
Last active December 30, 2015 01:59
iOS Version Macros
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
@cwagdev
cwagdev / gist:7e0c7d8e2d2c9808be92
Last active August 29, 2015 14:02
Swift Array.firstObject() and Array.lastObject() Extensions
extension Array {
func firstObject() -> T! {
var firstItem: T!
if !self.isEmpty {
firstItem = self[0]
}
return firstItem
}
func lastObject() -> T! {
@cwagdev
cwagdev / A.swift
Created August 5, 2014 07:28
Swift - Array Extension for .any
extension Array {
var any: T? {
get {
if self.count > 0 {
let randomIndicie = arc4random_uniform(UInt32(self.count))
return self[Int(randomIndicie)]
} else {
return nil
}
}
@cwagdev
cwagdev / gist:9de963f579ae0281b4ec
Created March 2, 2015 18:05
Letter from Matt Salmon

Dear Mr. Wagner,

Thank you for contacting me with your thoughts regarding net neutrality. I appreciate hearing from you on this matter.

As you know, net neutrality is the idea that internet providers and governments should treat all data on the internet equally and not discriminate based on the type of content. In 2010, the Federal Communications Commission (FCC) approved an Open Internet Order, which prohibits broadband providers from blocking or discriminating against Internet content, making net neutrality the law.

There is no question that the advancement of the Internet has spurred huge opportunities for all Americans and substantially enhanced our national economy. Access to the Internet has moved from a luxury to a necessity. As a result of the substantial increase of Internet demand, more pressure has been put on the existing infrastructure, and additional enormous investments in infrastructure are necessary in order to accommodate this ever-growing demand.

In order to ensure that we benefit fr

@cwagdev
cwagdev / UIApplicationExt.swift
Created March 13, 2015 22:06
UIApplication Extension for Version and App Name
extension UIApplication {
class var versionString: String {
get {
if let bundleInfoDictionary = NSBundle.mainBundle().infoDictionary {
let buildVersionString = bundleInfoDictionary["CFBundleVersion"] as String
let marketingVersionString = bundleInfoDictionary["CFBundleShortVersionString"] as String
return marketingVersionString + " (\(buildVersionString))"
} else {
return "Unknown"
}
func loginUserWithUsername(username: String, password: String) throws -> String {
guard username.characters.count != 0 else {
throw LoginError.EmptyUsername
}
guard password.characters.count != 0 else {
throw LoginError.EmptyPassword
}
///Handle all the other,
protocol ValueForKeyLookupable {
func valueForKey(key: String) -> Any?
}
extension ValueForKeyLookupable {
func valueForKey(key: String) -> Any? {
let mirror = reflect(self)
for index in 0..<mirror.count {
let child = mirror[index]
if key == child.0 {return child.1.value}