Created
October 10, 2012 21:47
-
-
Save maxparm/3868669 to your computer and use it in GitHub Desktop.
iOs Environment Helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Environment.h | |
// | |
// Inspired by http://blog.carbonfive.com/2011/06/20/managing-ios-configurations-per-environment-in-xcode-4/ | |
// | |
#import <Foundation/Foundation.h> | |
@interface EnvironmentHelper : NSObject | |
- (void)initializeSharedInstance; | |
//- (id)init; | |
- (NSString *)baseUrl; | |
- (NSString *)facebookId; | |
+ (EnvironmentHelper *)sharedInstance; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Environment.m | |
// | |
// Inspired by http://blog.carbonfive.com/2011/06/20/managing-ios-configurations-per-environment-in-xcode-4/ | |
// | |
#import "EnvironmentHelper.h" | |
// Private Methods / Variables | |
@interface EnvironmentHelper() | |
@property (nonatomic, strong) NSString *baseUrl; | |
@property (nonatomic, strong) NSString *facebookId; | |
@property (nonatomic, strong) NSDictionary *config; | |
@end | |
// Public Methods / Variables | |
@implementation EnvironmentHelper | |
// List here all the environment data | |
@synthesize baseUrl = _baseUrl; | |
@synthesize facebookId = _facebookId; | |
static EnvironmentHelper *sharedInstance = nil; | |
/* | |
* Assign environment values to the class variables | |
* 1. Get environment name: Debug or Release | |
* 2. Get the data of the specific environment dictionary | |
* 3. Assign the value of the dictionary to the class variables | |
*/ | |
- (void)initializeSharedInstance | |
{ | |
NSString* configuration = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"Configuration"]; | |
NSLog(@"configuration: %@", configuration); | |
NSBundle* bundle = [NSBundle mainBundle]; | |
NSString* envsPListPath = [bundle pathForResource:@"Environments" ofType:@"plist"]; | |
NSDictionary* environments = [[NSDictionary alloc] initWithContentsOfFile:envsPListPath]; | |
NSDictionary* environment = [environments objectForKey:configuration]; | |
self.baseUrl = [environment valueForKey:@"baseUrl"]; | |
self.facebookId = [environment valueForKey:@"facebookId"]; | |
} | |
/* | |
* Create an instance of the class and return it. | |
* 1. Init the class | |
* 2. Initialize the environment value | |
* 3. Return object | |
*/ | |
#pragma mark - Lifecycle Methods | |
+ (EnvironmentHelper *)sharedInstance | |
{ | |
@synchronized(self) { | |
if (sharedInstance == nil) { | |
sharedInstance = [[self alloc] init]; | |
[sharedInstance initializeSharedInstance]; | |
} | |
return sharedInstance; | |
} | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// TestViewController.m | |
// | |
#import "AlbatrosViewController.h" | |
#import "EnvironmentHelper.h" | |
// Private methods, variables | |
@interface TestViewController () | |
@end | |
// Publics methods, variables | |
@implementation TestViewController | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
EnvironmentHelper *env = [EnvironmentHelper sharedInstance]; | |
NSLog(@"baseUrl = %@", env.baseUrl); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment