Last active
November 3, 2022 12:48
-
-
Save vfadc/8923786b81300e6b36236e6f9aba24c0 to your computer and use it in GitHub Desktop.
ios LaunchArgs handle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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