Skip to content

Instantly share code, notes, and snippets.

@HevaWu
Last active July 27, 2021 02:56
Show Gist options
  • Save HevaWu/7730bb459eaa1b9ca624bba18e76c6c0 to your computer and use it in GitHub Desktop.
Save HevaWu/7730bb459eaa1b9ca624bba18e76c6c0 to your computer and use it in GitHub Desktop.
TestAppDelegate_TestSceneDelegate
// Add this file to the main target
import UIKit
let appDelegateClass: AnyClass = NSClassFromString("TestsAppDelegate") ?? AppDelegate.self
UIApplicationMain(CommandLine.argc, CommandLine.unsafeArgv, nil, NSStringFromClass(appDelegateClass))
import UIKit
@objc(TestingAppDelegate)
final class TestingAppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
if #available(iOS 13, *) {
// Remove any cached scene configurations to ensure that TestingAppDelegate.application(_:configurationForConnecting:options:) is called and TestingSceneDelegate will be used when running unit tests. NOTE: THIS IS PRIVATE API AND MAY BREAK IN THE FUTURE!
for sceneSession in application.openSessions {
application.perform(Selector(("_removeSessionFromSessionSet:")), with: sceneSession)
}
} else {
window = UIWindow()
window?.rootViewController = TestingRootViewController()
window?.makeKeyAndVisible()
}
return true
}
// MARK: UISceneSession Lifecycle
@available(iOS 13, *)
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
let sceneConfiguration = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role)
sceneConfiguration.delegateClass = TestingSceneDelegate.self
sceneConfiguration.storyboard = nil
return sceneConfiguration
}
}
// Add this file to the unit test target
import UIKit
class TestsSceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
window?.rootViewController = UIViewController()
window?.makeKeyAndVisible()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment