Create environments in iOS app(Swift)
So what is environment..??π€
An environment is a area, scope or place for developing, testing and debugging an application or program.
Basically we have 3 environments,
1: Development
2: Pre-Production
3: Production
Sometimes we need to seperate the base url and do the initial work, Which should be done for that state or condition only.
1: Create enum to differentiate environments
enum Environment {case developmentcase preProductioncase production}
2: Create the environment variable
let currentEnvironment = Environment.development
3: Use switch to execute different branches of code based on certain conditions
switch currentEnvironment {case .development:print("do the code which you want to execute only if the environment is development")case .preProduction:print("do the code which you want to execute only if the environment is preProduction")case .production:print("do the code which you want to execute only if the environment is production")default:break}
4: Where you can use this code ?
Well you can use it anywhere in the project,
just initialize thecurrentEnvironment
variable and using conditions use it.
like i use to change thebaseUrl
inAppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {let currentEnvironment = Environment.developmentswitch currentEnvironment {case .development:
baseUrl = "developmentBaseUrl"case .preProduction:
baseUrl = "preProductionBaseUrl"case .production:
baseUrl = "productionBaseUrl"default:
break}return true
}
Thank you fore reading π,
keep coding and keep sharing..π
Sharing is caringβ¦π