EnvChanger is a simple tool that helps developers and testers to switch between backend environments really quickly without having to download a specific build.
Displays a button in the top left corner that when selected, presents an alert with the given possible environments allowing users to easily swap them.
To run the example project, clone the repo, and run pod install
from the Example directory first.
- We assume you hold the list of your backend environments in an enum:
enum Environments: 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"
}
- Your enum needs to conform to the
EnvironmentRepresentable
protocol, by implementingenvironmentTitle
:
enum Environments: 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 {
rawValue
}
}
- Create an instance of
EnvChangerController
in your AppDelegatedidFinishLaunchingWithOptions
, passing your environment enum:
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
/// Instantiate to customise envButton `style` and `position` if needed.
let envButtonConfig = EnvButtonConfiguration(style: .title("env"),
startingPosition: .init(y: .center, x: .center))
/// Instantiate and pass handler for environment selection.
let envChanger = EnvChangerController(envs: NetworkService.Environments.self,
window: window,
buttonConfiguration: envButtonConfig) { selectedEnvironment in
NetworkService.activeEnvironment = selectedEnvironment.environmentTitle
}
/// If an environment has been saved, set is as the active environment.
if !envChanger.savedEnvironment.isEmpty {
NetworkService.activeEnvironment = envChanger.savedEnvironment
}
return true
}
envChanger.savedEnvironment
Note: The chosen environment is saved in UserDefaults.
resizeFrame(newWidth: CGFloat, newHeight: CGFloat)
Note: If an image is set, the imageEdgeInsets are calculated and set as '(height + width) / 2'.
- If no button image/title is passed in the constructor, by default it will set the button title to 'EN'.
- If a button title AND image is specified, the image is implemented and the title set will not be set.
EnvChanger is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'EnvChanger'
Gavril Tonev, gavril.tonev@upnetix.com
Teodor Marinov, teodor.marinov@upnetix.com
EnvChanger is available under the MIT license.
A new version is released by pushing an updated tag to master
:
# Make changes, commit files
git tag -a 1.2.3 -m "Release 1.2.3"
git push --follow-tags