EnvChanger 🧙♂️
关于
EnvChanger 是一款简单的工具,帮助开发和测试人员快速在不同后端环境之间切换,而无需下载特定构建版本。
在左上角显示一个按钮,选择后,将显示包含给定可能环境的警报,让用户轻松切换。
预览
示例
要运行示例项目,请克隆仓库,然后先从 Example 目录运行 pod install
。
用法
- 我们假设你将后端环境的列表保存在一个枚举中
enum Envs: String {
case production = "https://production.server.com/"
case staging = "https://staging.server.com/"
case development = "https://development.server.com"
case testing = "https://10.0.1.1/"
case edge = "edge.server.com"
}
- 你的枚举需要符合
EnvironmentRepresentable
协议,如下所示
enum Envs: String, EnvironmentRepresentable {
case production = "https://production.server.com/"
case staging = "https://staging.server.com/"
case development = "https://development.server.com"
case testing = "https://10.0.1.1/"
case edge = "edge.server.com"
var environmentTitle: String {
return rawValue
}
}
- 在AppDelegate的
didFinishLaunchingWithOptions
中创建EnvChangerController
的实例,并将环境枚举作为参数传递
// The variable holding your environemnt. Your setup might be different
var ACTIVE_ENVIRONMENT = Envs.production.environmentTitle
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Passing a completion handler, for when the user selects an environment
let envChanger = EnvChangerController(envs: Envs.self) { selectedEnvironment in
// Updating your environment variable. You might need to update your networking service as well.
ACTIVE_ENVIRONMENT = selectedEnvironment.environmentTitle
print(ACTIVE_ENVIRONMENT)
}
// Optional, so that the choice of environment persists the next time you start your app
ACTIVE_ENVIRONMENT = envChanger.getSavedEnvironment()
return true
}
更多用法
访问已保存的环境
getSavedEnvironment() -> String
注意:它将所选环境保存在UserDefaults中。
指定高度/宽度调整按钮大小。
resizeFrame(newWidth: CGFloat, newHeight: CGFloat)
注意:如果设置了图像,则计算并设置imageEdgeInsets为'(高度 + 宽度) / 2'。
附加说明
- 如果没有在构造函数中传递按钮图像/标题,默认情况下将按钮标题设置为'EN'。
- 如果指定了按钮标题和图像,则应用程序将执行图像并且不会设置已设置的标题。
安装
EnvChanger可通过CocoaPods获得。要安装,只需将以下行添加到Podfile中
pod 'EnvChanger'
作者
Gavril Tonev, [email protected]
Teodor Marinov, [email protected]
许可
EnvChanger遵循MIT许可证。
贡献
通过向master
推送更新标签来发布新版本。
# Make changes, commit files
git tag -a 1.2.3 -m "Release 1.2.3"
git push --follow-tags
待办
• 实现可配置按钮样式。
• 实现可配置起始按钮位置。