SuppyConfig 1.0.15

SuppyConfig 1.0.15

Ricardo Hazan 维护。



  • 作者:
  • Ricardo Rautalahti-Hazan

如何开始

  • suppy.io 上创建一个账户
  • suppy.io 上创建一个配置文件,至少包含一个属性并发布
  • 查看我们的例子:Swift 示例或 Objc 示例项目。
  • 继续阅读本页面。

安装

目前推荐的安装方法是 CocoaPods。

CocoaPods

CocoaPods 是一个 Swift 和 Objective-C 的依赖管理器,它自动化并简化了在项目中使用第三方库如 SuppyConfig 的过程。您可以使用以下命令安装它

$ gem install cocoapods

要使用 CocoaPods 将 SuppyConfig 集成到您的 Xcode 项目中,请在您的 Podfile 中指定它

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'

target 'TargetName' do
  pod 'SuppyConfig', '1.0.11'
end

然后,运行以下命令

$ pod install

使用方法

此库支持 iOS 10.0 和 iPadOS 14.0 及更高版本。

导入

Swift: import SuppyConfig
Objective-C: @import SuppyConfig;

初始化

为了使用SuppyConfig,您首先需要对其进行初始化。

参数

  • configId: 使用的配置。
  • applicationName: 在网页界面路由表中看到的应用程序标识符。
  • dependencies: 应用程序所需的配置。
  • suiteName: 使用指定的名称初始化UserDefaults数据库。
  • enableDebugMode: 输出配置检索和处理信息。
let suppyConfig = SuppyConfig(configId: "<identifier>", 
                              applicationName: "<name>", 
                              dependencies: "[Dependency]")       

如果您想在远程配置中保留单独的UserDefaults数据库,可以在init中指定参数suiteName。

https://developer.apple.com/documentation/foundation/userdefaults/1409957-init 了解更多关于该主题的信息。

let suppyConfig = SuppyConfig(configId: "<identifier>", 
                              applicationName: "<name>", 
                              dependencies: "[Dependency]",
                              suiteName: "<UserDefaults suite name>",
                              enableDebugMode: "<Boolean>"")       

初始化后,您可以检索您的配置。

依赖项

依赖项存储在UserDefaults注册字典中。它作为每个搜索列表中的最后一项使用。这意味着在UserDefaults在每个其他有效位置查找值之后,它将查找已注册的默认值。

重要 服务器的响应基于应用程序依赖项。如果传递空数组,则不会检索任何内容。如果您的依赖项与服务器配置不匹配,则不会检索任何内容。

Dependency(name: "<Name of the dependency>", 
           value: <A initial value / fallback>, // type = Any
           mappedType: <Dependency Type>)

支持的类型: 字符串、数字、布尔值、数组、字典、URL、日期

检索配置

suppyConfig.fetchConfiguration(completion:)       

完成是可选的,并且允许您在检索完成后接收通知。

使用配置

配置存储在标准UserDefaults中,因此可以通过您已知的API进行访问。

配置值根据初始化时指定的依赖类型进行存储。

UserDefaults.standard.string(forKey:)
UserDefaults.standard.bool(forKey:)
UserDefaults.standard.array(forKey:)
UserDefaults.standard.dictionary(forKey:)
UserDefaults.standard.url(forKey:)
UserDefaults.standard.integer(forKey:)
UserDefaults.standard.double(forKey:)
UserDefaults.standard.float(forKey:)

建议

调试

建议仅在开发期间启用调试模式(init参数enableDebugMode)。在生产中应将其关闭。

获取

配置获取不绑定到您的应用生命周期的任何特定部分。尽管如此,我们建议在AppDelegate的applicationDidBecomeActive期间调用fetchConfiguration。

applicationDidBecomeActive(_:)

在applicationDid BecomeActive期间获取将确保每次应用从后台返回以及初始化时都会刷新配置。

示例

此库不是单例。您需要持有其引用。

let dependencies = [
            
            Dependency(name: "Application Title", value: "Intial App Title", mappedType: .string),

            Dependency(name: "Privacy Policy", value: URL(string: "https://default-local-url.com")!, mappedType: .url),
            
            Dependency(name: "Number of Seats", value: 2, mappedType: .number),
            
            Dependency(name: "Product List", value: [], mappedType: .array),
            
            Dependency(name: "Feature X Enabled", value: false, mappedType: .boolean)
        ]

let suppyConfig = SuppyConfig(configId: "5f43879d25bc1e682f988129",        
                              applicationName: "Swift Example", 
                              dependencies: dependencies, 
                              suiteName: nil, 
                              enableDebugMode: true) 
                              
suppyConfig.fetchConfiguration {
    let defaults = UserDefaults.standard    
    let string = defaults.string(forKey: "Application Title")
    let url = defaults.url(forKey: "Privacy Policy")
    let int = defaults.integer(forKey: "Number of Seats")
    let bool = defaults.bool(forKey: "Feature X Enabled")
    let array = defaults.array(forKey: "Product List")
}