GSConfiguration 0.1.0-beta1

GSConfiguration 0.1.0-beta1

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
发布最新发布2015年2月

Ryan Brignoni维护。



GSConfiguration 0.1.0-beta1

这是什么?

GSConfiguration 是一个为 iOS 应用提供类型安全的通用配置库。它支持多个本地 plist 配置文件以及远程 JSON 配置服务。所有配置文件都由与 Core Data 的 NSManagedObject 类似的类表示。

安装

基本用法

使用 GSConfiguration 的第一步是在您的应用中某个位置定义一个包含不同类型值的 plist 文件。让我们称它为 "Config.plist"。

<?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>coolFeatureActivated</key>
    <true/>
    <key>timeout</key>
    <real>60</real>
    <key>serviceName</key>
    <string>some service</string>
    <key>totallyAwesomeUrl</key>
    <string>https://github.com/gliders/GSConfiguration</string>
    <key>cachePolicy</key>
    <real>1</real>
</dict>
</plist>

然后创建一个 GSConfiguration 的子类,并定义 @dynamic 属性,就像您创建 NSManagedObject 实体一样。

@interface ApplicationConfig : GSConfiguration

@property BOOL coolFeatureActivated;
@property NSTimeInterval timeout;
@property NSString *serviceName;
@property NSURL *totallyAwesomeUrl; // Note: This is an NSURL here but String in the plist.
@property NSURLRequestCachePolicy cachePolicy;

@end

@implementation ApplicationConfig

@dynamic coolFeatureActivated;
@dynamic timeout;
@dynamic apiKey;
@dynamic totallyAwesomeUrl;
@dynamic cachePolicy;

@end

接下来,配置 GSConfiguration 加载您的 plist 并将数据存储在某个地方。通常在需要使用配置值之前执行此操作,可能是在 AppDelegate 中。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [GSConfigurationManager setStore:[GSUserDefaultsStore store]];
    [GSConfigurationManager addSource:[GSDictionarySource sourceWithPListNamed:@"Config"]];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    [GSConfigurationManager cleanUp]; // This will synchronize NSUserDefaults store.
}

就这样!现在,每次需要配置值时,只需与 ApplicationConfig 外观交互,并将其像普通对象一样使用。

ApplicationConfig *config = [ApplicationConfig config];

if (config.coolFeatureActivated) {
    [self doCoolFeatureWithServiceNamed:config.serviceName];
}

NSURLRequest *request = [NSURLRequest requestWithURL:config.totallyAwesomeUrl // automatic type conversion here
                                         cachePolicy:config.cachePolicy
                                     timeoutInterval:config.timeout];

我想使用自定义类型

当然,您需要这样做。默认情况下,任何实现 NSCoding 的对象都可以很好地编码和解码。不想使用 NSCoding?定义一个 NSValueTransformer 并将其注册到 GSConfigurationManager 中。确保它可以接受一个字符串,将转换回字符串。查看 GSURLStringTransformer 以获取灵感,并考虑使用 TransformerKit。

[NSValueTransformer setValueTransformer:[[GSURLStringTransformer alloc] init]
                                        forName:GSURLStringTransformerName];

[GSConfigurationManager registerTransformerName:GSURLStringTransformerName forClass:[NSURL class]];

我想远程控制配置

有时我们需要从远处控制我们的应用。我为您准备了这些。添加一个 GSRemoteSource,并传递一个指向具有额外配置值的 JSON 服务的 NSURL。您甚至可以告诉源在特定间隔内持续轮询。

GSRemoteSource *remote = [GSRemoteSource sourceWithEndpoint:[NSURL URLWithString:@"http://example.com/api/config"] 
                                                parserBlock:^NSDictionary *(id jsonData) {
    // If your JSON is complex or not simply a dictionary with key/values, 
    // parse it as needed and return a dictionary of just key/values.
    return jsonData;
}];
remote.refreshIntervalInSeconds = 300;
[GSConfigurationManager addSource:remote];

我们需要深入研究

不够吗?很好。您还可以定义多个外观,根据需要组织您的配置。想要发布和调试不同的配置吗?只需在调试配置后添加发布配置,调试模式下匹配名称的键的调试值将被自动压缩。

注意事项

  • 所有GSConfiguration子类的属性名称都在一个字典中共享。这意味着,如果您在两个GSConfiguration子类中定义了两个具有相同确切名称的属性,它们在内部将映射到相同的值。

  • 添加源时的顺序很重要。当它们对同一个密钥具有不同值时,最后一个添加的源获胜。

日志记录

通过定义宏来启用日志记录

#define GS_CONFIG_LOGGING_ENABLED 1

未来计划

  • 门面_key-value观察支持
  • 通过GSSecureConfiguration门面进行安全密钥链存储,用于更敏感的配置数据。

作者

Ryan Brignoni
Twitter: @RyanBrignoni

许可证

GSConfiguration可在MIT许可证下使用。有关更多信息,请参阅LICENSE文件。