Skip to content

Instantly share code, notes, and snippets.

@westonplatter
Last active December 22, 2015 08:29
Show Gist options
  • Select an option

  • Save westonplatter/6445784 to your computer and use it in GitHub Desktop.

Select an option

Save westonplatter/6445784 to your computer and use it in GitHub Desktop.
How to reference environment specific iOS Enviroment Variables. Followed this example, and updated to reflect ARC usuage. http://blog.carbonfive.com/2011/06/20/managing-ios-configurations-per-environment-in-xcode-4/
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Release</key>
<dict/>
<key>Debug</key>
<dict>
<key>apiUrl</key>
<string>http://localhost:3000/api/v1/</string>
</dict>
</dict>
</plist>
//
// Environment.h
// Visible
//
// Created by weston on 9/4/13.
// Copyright (c) 2013 Visible. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Environment : NSObject
@property (nonatomic, strong) NSString *apiUrl;
+ (Environment *)sharedInstance;
@end
//
// Environment.m
// Visible
//
// Created by weston on 9/4/13.
// Copyright (c) 2013 Visible. All rights reserved.
//
#import "Environment.h"
@implementation Environment
static Environment *sharedInstance = nil;
- (id)init
{
self = [super init];
return self;
}
- (void)initializeSharedInstance
{
NSString* configuration = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"Configuration"];
NSBundle* bundle = [NSBundle mainBundle];
NSString* envsPListPath = [bundle pathForResource:@"env" ofType:@"plist"];
NSDictionary* environments = [[NSDictionary alloc] initWithContentsOfFile:envsPListPath];
NSDictionary* environment = [environments objectForKey:configuration];
_apiUrl = [environment valueForKey:@"apiUrl"];
}
#pragma mark - Lifecycle Methods
+ (Environment *)sharedInstance
{
@synchronized(self) {
if (sharedInstance == nil) {
sharedInstance = [[self alloc] init];
[sharedInstance initializeSharedInstance];
}
return sharedInstance;
}
}
@end
//
// LoginViewController.m
// Visible
//
// Created by weston on 9/4/13.
// Copyright (c) 2013 Visible. All rights reserved.
//
#import "LoginViewController.h"
#import "Environment.h"
@implementation LoginViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self authenticate];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)authenticate {
Environment *env = [Environment sharedInstance];
NSString *apiUrl = env.apiUrl;
NSLog(@"apiUrl = %@", apiUrl);
}
@end
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Configuration</key>
<string>${CONFIGURATION}</string>
</dict>
</plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment