Create environments in iOS app(Swift)

Akshay Somkuwar
1 min readSep 29, 2018

--

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 the currentEnvironment variable and using conditions use it.
like i use to change the baseUrl in AppDelegate

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β€¦πŸ˜œ

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Akshay Somkuwar
Akshay Somkuwar

Written by Akshay Somkuwar

iOS | MacOS | React Native Developer..

Responses (1)

Write a response