Skip to content

Instantly share code, notes, and snippets.

View danmartyn's full-sized avatar

Daniel Martyn danmartyn

  • Ceridian
  • Canada
View GitHub Profile
@danmartyn
danmartyn / GCD
Created October 15, 2013 04:18
Typical use case for GCD
// Doing something on the main thread
dispatch_queue_t myQueue = dispatch_queue_create("My Queue",NULL);
dispatch_async(myQueue, ^{
// Perform long running process
dispatch_async(dispatch_get_main_queue(), ^{
// Update the UI
});
@danmartyn
danmartyn / stringify
Created September 24, 2013 18:26
Encode and Decode objects without making typos
Taken From: http://stablekernel.com/blog/speeding-up-nscoding-with-macros/
#define OBJC_STRINGIFY(x) @#x
#define encodeObject(x) [aCoder encodeObject:x forKey:OBJC_STRINGIFY(x)]
#define decodeObject(x) x = [aDecoder decodeObjectForKey:OBJC_STRINGIFY(x)]
#define encodeFloat(x) [aCoder encodeFloat:x forKey:OBJC_STRINGIFY(x)]
#define decodeFloat(x) x = [aDecoder decodeFloatForKey:OBJC_STRINGIFY(x)]
- (id)initWithCoder:(NSCoder *)aDecoder
{
@danmartyn
danmartyn / target
Created September 2, 2013 05:31
Add target for cocoa app
1) Choose the target you want to add to
2) Choose Build Settings
3) Search for 'macros'
4) Add a macro
To use the macro in the app:
#ifdef MACRO
// do some stuff
#elif OTHERMACRO
// do other stuff
@danmartyn
danmartyn / Version String
Created July 29, 2013 04:15
Returns the app name, version and build number
- (NSString *)appNameAndVersionNumberDisplayString {
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSString *appDisplayName = [infoDictionary objectForKey:@"CFBundleDisplayName"];
NSString *majorVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
NSString *minorVersion = [infoDictionary objectForKey:@"CFBundleVersion"];
return [NSString stringWithFormat:@"%@, Version %@ (%@)",
appDisplayName, majorVersion, minorVersion];
}
@danmartyn
danmartyn / restartMacApp
Created July 22, 2013 05:24
This is a cocoa method for restarting a mac app
+(void)restart:(id)sender
{
NSString *restartScript = @"while ps -p $1 > /dev/null; do sleep 0.1; done; open \"$2\"";
NSArray *arguments = [NSArray arrayWithObjects:
@"-c", restartScript,
@"",
[NSString stringWithFormat:@"%d",[[NSProcessInfo processInfo] processIdentifier]],
[[NSBundle mainBundle] bundlePath],
nil];
[NSTask launchedTaskWithLaunchPath:@"/bin/sh" arguments:arguments];
@danmartyn
danmartyn / Singleton
Created July 16, 2013 05:09
Singleton for Obj-C
+ (<#Class#> *)sharedManager
{
static <#Class#> *sharedManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedManager = [[<#Class#> alloc] init];
// other initialisation stuff
});
return sharedManager;
}
@danmartyn
danmartyn / imagePickerControllerDelegate
Created June 15, 2013 08:22
UIImagePickerController Delegate Methods
#pragma mark -
#pragma mark UIImagePickerController Delegate Methods
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// dismiss the picker
if (self.popover)
{
[self.popover dismissPopoverAnimated:YES];
self.popover = nil;
@danmartyn
danmartyn / imageLibrary
Created June 15, 2013 08:20
Image Library
UIImagePickerController *libraryController = [[UIImagePickerController alloc] init];
libraryController.delegate = self;
libraryController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
libraryController.allowsEditing = YES;
// if on the ipad, show the library in a popover
if (iPad)
{
UIPopoverController *libraryPopover = [[UIPopoverController alloc] initWithContentViewController:libraryController];
[libraryPopover presentPopoverFromRect:self.rectToPresentPopoverFrom inView:self.scrollView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
@danmartyn
danmartyn / showCamera
Created June 15, 2013 08:20
Show Camera
// setup a picker for the camera
UIImagePickerController *cameraController = [[UIImagePickerController alloc] init];
cameraController.delegate = self;
cameraController.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraController.allowsEditing = YES;
[self presentViewController:cameraController animated:YES completion:nil];
@danmartyn
danmartyn / lastPhoto
Created June 15, 2013 08:19
Get the Last Photo
// Link Against Assets Library
// #import <AssetsLibrary/AssetsLibrary.h>
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
// Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
// Within the group enumeration block, filter to enumerate just photos.
[group setAssetsFilter:[ALAssetsFilter allPhotos]];