我的 AppDelegate.swift 文件中有以下声明。
var window: UIWindow?
并且我只想在部署目标设置为低于 iOS 13.0 时才包含它。我看过
@available(...)
属性,但我认为它不能做我想做的事。本质上,我希望能够在 AppDelegate.swift 和 SceneDelegate.swift 中对我的项目进行最少的更改,从而为 iOS 12 或 iOS 13 构建我的项目。我已经对 AppDelegate.swift 和 SceneDelegate.swift 进行了更改@available(iOS 13.0, *)
,#available(iOS 13.0, *)
但是当部署目标高于 iOS 12 时,我仍然需要能够排除这个其他声明。
这是我的 AppDelegate.swift 文件:
import UIKit
import Firebase
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow? // only include for builds < iOS 13.0
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if #available(iOS 13.0, *) {
// Do nothing
} else {
window = UIWindow() // only include for builds < iOS 13.0
window?.rootViewController = UINavigationController(rootViewController: LoginController()) // only include for builds < iOS 13.0
window?.makeKeyAndVisible() // only include for builds < iOS 13.0
}
FirebaseApp.configure()
return true
}
// MARK: UISceneSession Lifecycle
@available(iOS 13.0, *)
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.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
@available(iOS 13.0, *)
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
}
这是我的 SceneDelegate.swift 文件:
import UIKit
@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let scene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: scene)
window?.rootViewController = UINavigationController(rootViewController: LoginController())
window?.makeKeyAndVisible()
}
// ...
}
基本上我想要的只是更改部署目标,并在构建过程中包含或排除适当的代码位。