Our REST based application can be used for testing on multiple internal environments each with a different REST end point. Is there a simple way to set up environment level co
This is my approach of doing things when we have multiple end points. I used to make a ConfigurationManager
class something like this
Swift 3.0 code
import Foundation
import UIKit
let kEnvironmentsPlist:NSString? = "Environments"
let kConfigurationKey:NSString? = "ActiveConfiguration"
let kAPIEndpointKey:NSString? = "APIEndPoint"
let kLoggingEnabledKey:NSString? = "LoggingEnabled"
let kAnalyticsTrackingEnabled:NSString? = "AnalyticsTrackingEnabled"
class ConfigurationManager:NSObject {
var environment : NSDictionary?
//Singleton Method
static let sharedInstance: ConfigurationManager = {
let instance = ConfigurationManager()
// setup code
return instance
}()
override init() {
super.init()
initialize()
}
// Private method
func initialize () {
var environments: NSDictionary?
if let envsPlistPath = Bundle.main.path(forResource: "Environments", ofType: "plist") {
environments = NSDictionary(contentsOfFile: envsPlistPath)
}
self.environment = environments!.object(forKey: currentConfiguration()) as? NSDictionary
if self.environment == nil {
assertionFailure(NSLocalizedString("Unable to load application configuration", comment: "Unable to load application configuration"))
}
}
// CurrentConfiguration
func currentConfiguration () -> String {
let configuration = Bundle.main.infoDictionary?[kConfigurationKey! as String] as? String
return configuration!
}
// APIEndpoint
func APIEndpoint () -> String {
let configuration = self.environment![kAPIEndpointKey!]
return (configuration)! as! String
}
// isLoggingEnabled
func isLoggingEnabled () -> Bool {
let configuration = self.environment![kLoggingEnabledKey!]
return (configuration)! as! Bool
}
// isAnalyticsTrackingEnabled
func isAnalyticsTrackingEnabled () -> String {
let configuration = self.environment![kAnalyticsTrackingEnabled!]
return (configuration)! as! String
}
func applicationName()->String{
let bundleDict = Bundle.main.infoDictionary! as NSDictionary
return bundleDict.object(forKey: "CFBundleName") as! String
}
}
In Project--> Info Add some new configurations as per your need.
I have added Staging and QA as extra endpoints.Generally I use to make Staging as Release config and QA as Debug. So it will look like:
Now go to Targets -> Build Settings and add a User Defined Setting
Give the name of the user defined like ACTIVE_CONFIGURATION.
Add a key named ActiveConfiguration
in info.plist
with a variable name as $(ACTIVE_CONFIGURATION)
same as given in User Defined Settings with a $ in the beginning. We gave the name of key as ActiveConfiguration
because we are using the same name in our ConfigurationManager.swift
class for kConfigurationKey
.
let kConfigurationKey:NSString? = "ActiveConfiguration"
You can define as per your naming convention.
It will look like:
Now in the ConfigurationManager
class I am getting a path for Environments.plist
file.
I will just make a Environments.plist
file like this:
The actual description source of this file is
Development
APIEndPoint
https://dev
LoggingEnabled
AnalyticsTrackingEnabled
Flurry
FlurryApplicationID
FlurryApplicationSecret
Facebook
FacebookAppID
FacebookAppSecret
QA
APIEndPoint
https://qa
LoggingEnabled
AnalyticsTrackingEnabled
Flurry
FlurryApplicationID
FlurryApplicationSecret
Facebook
FacebookAppID
FacebookAppSecret
Staging
APIEndPoint
https://staging
LoggingEnabled
AnalyticsTrackingEnabled
Flurry
FlurryApplicationID
FlurryApplicationSecret
Facebook
FacebookAppID
840474532726958
FacebookAppSecret
Production
APIEndPoint
https://production
LoggingEnabled
AnalyticsTrackingEnabled
Flurry
FlurryApplicationID
FlurryApplicationSecret
Facebook
FacebookAppID
FacebookAppSecret
We are now good to go. Now you have to just call
ConfigurationManager.sharedInstance.APIEndpoint()
for your respective end points.
Now you just have to change the schemes from Edit Schemes and you are done and change the Build Configuration in info.
This not only manages API End Points but also other things like whether to enable analytics or tracking for the respective end point or different ids of Facebook for different end points.