Skip to content

Instantly share code, notes, and snippets.

@vfadc
Last active November 3, 2022 12:48
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
ios LaunchArgs handle
enum LaunchArgs: String, CaseIterable {
case fakeApiUrl = "--fake_api_url"
case accessToken = "--access_token"
}
public protocol UITestsArgumentsHandler {
func handleProcessArgument(_ processArgument: String?)
}
public class UITestsArgumentsAuthHandler: UITestsArgumentsHandler {
public func handleProcessArgument(_ processArgument: String?) {
guard let authToken = processArgument else {
return
}
//code to setup auth token
}
}
public class UITestsArgumentsTestApiUrlHandler: UITestsArgumentsHandler {
public func handleProcessArgument(_ processArgument: String?) {
guard let testApiUrl = processArgument else {
return
}
//code to setup test api url
}
}
public class UITestsArgumentsHandlerFactory {
public static func argumentsHandler(for key: LaunchArgs) -> UITestsArgumentsHandler? {
switch key {
case .accessToken:
return UITestsArgumentsAuthHandler()
case .fakeApiUrl:
return UITestsArgumentsTestApiUrlHandler()
default:
return nil
}
}
}
public class UITestService {
public func activate() {
let environment = ProcessInfo.processInfo.environment
LaunchArgs.allCases.forEach { arg in
UITestsArgumentsHandlerFactory.argumentsHandler(for: arg)?.handleProcessArgument(environment[arg.rawValue])
}
//do other staff for testing
}
}
//and then you can run UITestService in didFinishLaunchingWithOptions only for debug app version
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
#if DEBUG
UITestService.activate()
#endif
//do other staff
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment