Made with ♥︎ at Supercharge
With SCConfiguration you can easily manage environment dependent (or global) configuration parameters in a property list file.
SCConfiguration is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "SCConfiguration"
First, you need to create a configuration file called Configuration.plist
in your project.
You can add global or environment dependent key-value pairs to it, here's an example with STAGING
and PRODUCTION
environments.
In source format:
<dict>
<key>APPLE_APP_ID</key> <!-- global parameter -->
<string>123456789</string>
<key>API_URL</key> <!-- environment dependent parameter -->
<dict>
<key>STAGING</key>
<string>https://staging.myappserver.com</string>
<key>PRODUCTION</key>
<string>https://myappserver.com</string>
</dict>
</dict>
If ENV
is a preprocessor macro defined by the build configuration you can easily set the configuration class's environment in the application:didFinishLaunchingWithOptions:
method:
SCConfiguration *configuration = [SCConfiguration new];
// Set the environment defined by the ENV preprocessor macro
[configuration setEnv:ENV];
// Read the API_URL environment dependent value from the configuration
NSString *apiUrl = [configuration configValueForKey:@"API_URL"]
You can also use SCConfiguration
as a singleton:
// Set the environment defined by the ENV preprocessor macro
[[SCConfiguration sharedInstance] setEnv:ENV];
// Read the API_URL environment dependent value from the configuration
NSString *apiUrl = [[SCConfiguration sharedInstance] configValueForKey:@"API_URL"]
It's a good practice to subclass SCConfiguration
and declare your configuration parameters explicitly in your application.
// MyAppConfiguration.h
@interface MyAppConfiguration : SCConfiguration
- (NSString *)apiUrl;
@end
// MyAppConfiguration.m
@interface MyAppConfiguration : SCConfiguration
- (NSString *)apiUrl
{
return (NSString *)[self configValueForKey:@"API_URL"];
}
@end
You can override configuration variable at runtime. This can be useful if you would like to synchronize configuration parameters through a backend service.
You need to add the following line to the applicationDidEnterBackground:
and applicationWillTerminate:
methods:
[[SCConfiguration sharedInstance] tearDown];
This method saves the configuration modifications between application launches.
You can overwrite / add key-value pairs:
NSDictionary *newConfigValues = @{ @"key1": @"new value", @"new key": @"new value" };
[[SCConfiguration sharedInstance] overwriteConfigWithDictionary:newConfigValues];
NOTE: overwritten key-value pairs will stay between application launches by default! You can change this behaivor by calling the [[SCConfiguration sharedInstance] setOverwriteStateToPersistant:NO]
.
Or you can set key-value pairs to protected / unprotected:
[[SCConfiguration sharedInstance] setKeysToProtected:@[@"key2", @"key3"]];
[[SCConfiguration sharedInstance] setKeyToProtected:@"key4"];
[[SCConfiguration sharedInstance] removeAllKeyFromProtection];
Protected values cannot be changed / added later.
iOS 6+
Contributions are always welcome! (:
- Fork it ( http://github.com/team-supercharge/SCConfiguration/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
SCConfiguration is available under the MIT license. See the LICENSE file for more info.
test1