SCConfiguration 2.2.0

SCConfiguration 2.2.0

测试已测试
语言语言 Obj-CObjective C
版权 MIT
发布最后发布2018年1月

Gergő NémethDávid HorváthBalazs Kovacs维护。



在Supercharge用♥️制作

介绍

使用SCConfiguration,您可以轻松管理环境依赖(或全局)配置参数在属性列表文件中。

此外,从1.1.0版本开始,如果配置文件包含敏感信息,您可以对其进行加密。

安装

SCConfiguration可通过CocoaPods获取。要安装它,只需将以下行添加到Podfile中

pod "SCConfiguration"

用法

在Configuration.plist文件中定义参数

首先,您需要在项目中创建一个名为Configuration.plist的配置文件。

Configuration.plist

您可以为它添加全局或环境依赖的键值对,以下是一个带有STAGINGPRODUCTION环境的示例。

源格式

<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>

设置环境并读取参数

如果ENV是构建配置中定义的预处理器宏,您可以在application:didFinishLaunchingWithOptions:方法中轻松设置配置类的环境

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"]

您还可以将SCConfiguration用作单例

// 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"]

加密您的配置文件

如果您正在使用1.1.0或更高版本,您可以通过一些额外的步骤对配置文件进行加密。如果文件包含诸如生产API URL、第三方App ID或客户端机密等敏感信息,这将非常有用。

这里的问题是默认情况下,您的Configuration.plist文件将无加密地添加到.ipa文件中。如果有人从App Store下载您的应用程序,解压缩.ipa文件并检查应用程序的内容,配置文件将以纯XML文件的形式存在。

解决方案是从构建中删除plist文件,帮助自定义运行脚本将其作为加密文件添加,并使用SCConfiguration库读取加密文件。

加密步骤

1) 在 Xcode 中选择您的 Configuration.plist 文件,并在 "实用工具" 面板 / "文件检查器" 标签中 取消选中 目标成员资格

Uncheck membership

2) 在项目的目标中添加一个新的自定义 "运行脚本",命名为 Encryption,并包含以下内容

openssl enc -e -aes-256-cbc -in "$PROJECT_DIR/SCConfiguration/Configuration.plist" -out "$TARGET_BUILD_DIR/$UNLOCALIZED_RESOURCES_FOLDER_PATH/Configuration.enc" -pass pass:<your password here>

重要:您需要替换 Configuration.plist 文件的路径以匹配项目结构,并且应将您自己的密码添加到此脚本中。

Add custom Run Script

3) 您需要在 application:didFinishLaunchingWithOptions: 方法中添加额外的行

[[SCConfiguration sharedInstance] setDecryptionPassword:@"<your password here>"];

继承 SCConfiguration

继承 SCConfiguration 并在您的应用程序中明确声明配置参数是一种良好的做法。

// MyAppConfiguration.h

@interface MyAppConfiguration : SCConfiguration

- (NSString *)apiUrl;

@end
// MyAppConfiguration.m

@interface MyAppConfiguration : SCConfiguration

- (NSString *)apiUrl
{
    return (NSString *)[self configValueForKey:@"API_URL"];
}

@end

覆盖配置变量

您可以在运行时覆盖配置变量。如果您想通过后端服务同步配置参数,这可能很有用。

您需要在 applicationDidEnterBackground:applicationWillTerminate: 方法中添加以下行

[[SCConfiguration sharedInstance] tearDown];

此方法在应用程序启动之间保存配置修改。

您可以 覆盖 / 添加键值对

NSDictionary *newConfigValues = @{ @"key1": @"new value", @"new key": @"new value" };
[[SCConfiguration sharedInstance] overwriteConfigWithDictionary:newConfigValues];

注意:默认情况下,被覆盖的键值对将在应用程序启动之间保留!您可以通过调用 [[SCConfiguration sharedInstance] setOverwriteStateToPersistent:NO] 来更改此行为。

或者,您可以将键值对设置为 受保护 / 未受保护

[[SCConfiguration sharedInstance] setKeysToProtected:@[@"key2", @"key3"]];
[[SCConfiguration sharedInstance] setKeyToProtected:@"key4"];

[[SCConfiguration sharedInstance] removeAllKeyFromProtection];

受保护的值不能稍后更改 / 添加。

兼容性

iOS 6+

贡献

欢迎贡献!(

  1. 创建分支(http://github.com/team-supercharge/SCConfiguration/fork
  2. 创建您的功能分支(git checkout -b my-new-feature
  3. 提交您的更改(git commit -am 'Add some feature'
  4. 推送到分支(git push origin my-new-feature
  5. 创建新的 Pull Request

许可协议

SCConfiguration 在 MIT 许可协议下可用。有关更多信息,请参阅 LICENSE 文件。