Skip to content

Instantly share code, notes, and snippets.

View astannard's full-sized avatar

Andy Stannard astannard

View GitHub Profile
@astannard
astannard / gist:eca5c880d40f96adcd3c
Created January 27, 2015 08:45
iOS camera action
@IBAction func snapBarButtonItemTapped(sender: UIBarButtonItem) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){
var cameraController = UIImagePickerController()
cameraController.delegate = self
cameraController.sourceType = UIImagePickerControllerSourceType.Camera
let mediaTypes:[AnyObject] = [kUTTypeImage]
cameraController.mediaTypes = mediaTypes
}
var alertController = UIAlertController(title: "Alert", message: "Your device does not support camera or photo libary", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
@astannard
astannard / gist:fc42d95ec49e167915dd
Created February 2, 2015 08:23
iOS swift setup cache
//Withing APP Delegate
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let cache = NSURLCache(memoryCapacity: 8 * 1024 * 1024, diskCapacity: 20 * 1024 * 1024, diskPath: nil)
NSURLCache.setSharedURLCache(cache)
return true
}
@astannard
astannard / gist:894c2100f6ed33ab628e
Created February 2, 2015 08:24
iOS swift clear cache on memory warning
func applicationDidReceiveMemoryWarning(application: UIApplication) {
NSURLCache.sharedURLCache().removeAllCachedResponses()
}
(UIApplication.sharedApplication().delegate as AppDelegate).saveContext()
@astannard
astannard / gist:15ff8b5b2a7198c2ebff
Created February 4, 2015 10:47
create an ios in OSX from a folder
hdiutil makehybrid -o ~/Desktop/image.iso ~/path/to/folder/to/be/converted -iso -joliet
@astannard
astannard / gist:6d17552d83c83b9e4d56
Created April 14, 2015 07:38
iOS Swift Post Request Example
var request = NSMutableURLRequest(URL: NSURL(string: "https://api.nutritionix.com/v1_1/search/")!)
let session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"
var params = [
"appId" : kAppId,
"appKey" : kAppKey,
"fields" : ["item_name", "brand_name", "keywords", "usda_fields"],
"limit" : "50",
"query" : searchString,
"filters": ["exists":["usda_fields": true]]
@astannard
astannard / gist:1eabd0e7877a5409a243
Created July 6, 2015 14:47
function to serialize a form to JSON
$.fn.serializeObject = function () {
var o = {};
var a = this.serializeArray();
$.each(a, function () {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
@astannard
astannard / gist:6273f4aa7aab9eed97ed
Last active January 27, 2016 09:54
Redux Notes
1/ 1 object to represent the whole set of app data
2/ State is read only
3/ Pure functions dont modify arguments and dont rely on anytihng other than arguments passed to them to calculate
4/ UI is a representation of application state,
5/ State changes use pure functions so you take the existing state and the action to calculate a new state from, then pass that back. (testable and predictable). The new state can still reference old objects that made up the previous state if these have not been modified
6/ Reducer function takes the state and action arguments to create new state. (an action is the minimum set of arguments needed to create a new state object from its previous state
7/ If reducer recieved undefined as its stateargument it must return what is to be considered the inital state of the application
8/ If a reducer recieves an unknow action it mus return the existing state
@astannard
astannard / gist:5e786f024360c7d9f1f7
Created February 18, 2016 08:17
Javascript this
this varies depending on the execution context and can sometimes lead to unexpected behaviour
ways of tackling this are:
newname = functionname.bind(object); // calling the bind method which exists on all JS functions creates a copy of the function
// with this scoped to the value of the object passed into the bind method.
newname(arg1,arg2); // newname will then need to be executed