Skip to content

Instantly share code, notes, and snippets.

@bananita
Last active December 19, 2015 19:09
Show Gist options
  • Save bananita/6004248 to your computer and use it in GitHub Desktop.
Save bananita/6004248 to your computer and use it in GitHub Desktop.
Cocoa singletons wrapped in one class to allow mocking them.
//
// ApplicationContext.h
//
// Created by Michał Banasiak on 22.07.2013.
// Copyright (c) 2013 Michał Banasiak. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface ApplicationContext : NSObject
@property(weak, readonly) UIApplication* application;
@property(weak, readonly) NSBundle* bundle;
@property(weak, readonly) NSNotificationCenter* notificationCenter;
@property(weak, readonly) UIScreen* screen;
@property(weak, readonly) UIDevice* device;
@property(weak, readonly) NSFileManager* fileManager;
@property(weak, readonly) NSUserDefaults* userDefaults;
@end
//
// ApplicationContext.m
//
// Created by Michał Banasiak on 22.07.2013.
// Copyright (c) 2013 Michał Banasiak. All rights reserved.
//
#import "ApplicationContext.h"
@implementation ApplicationContext
- (id)init
{
self = [super init];
if (!self)
return nil;
_application = [UIApplication sharedApplication];
_bundle = [NSBundle mainBundle];
_notificationCenter = [NSNotificationCenter defaultCenter];
_screen = [UIScreen mainScreen];
_device = [UIDevice currentDevice];
_fileManager = [NSFileManager defaultManager];
_userDefaults = [NSUserDefaults standardUserDefaults];
return self;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment