如何仅在 iOS 13.0 之前包含此 swift 声明
Posted
技术标签:
【中文标题】如何仅在 iOS 13.0 之前包含此 swift 声明【英文标题】:How can I include this swift declaration only before iOS 13.0 【发布时间】:2020-11-25 14:59:55 【问题描述】:我的 AppDelegate.swift 文件中有以下声明。
var window: UIWindow?
并且我只想在部署目标设置为低于 ios 13.0 时才包含它。我看过了
@available(...)
属性,但我不认为它可以做我想做的事。本质上,我希望能够在 AppDelegate.swift 和 SceneDelegate.swift 中对我的项目进行最少的更改,从而为 iOS 12 或 iOS 13 构建我的项目。我已经使用 @available(iOS 13.0, *)
和 #available(iOS 13.0, *)
对 AppDelegate.swift 和 SceneDelegate.swift 进行了更改,但是当部署目标高于 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()
// ...
基本上我只想更改部署目标,并在构建过程中包含或排除适当的代码。
【问题讨论】:
【参考方案1】:var window: UIWindow? // only include for builds < iOS 13.0
不要理会声明。在 iOS 12 和之前的版本中,它将引用应用程序窗口。在 iOS 13 及更高版本上,它将为零,不会造成任何伤害。
【讨论】:
以上是关于如何仅在 iOS 13.0 之前包含此 swift 声明的主要内容,如果未能解决你的问题,请参考以下文章
Xcode 11 beta -playbackState 仅在 iOS 13.0 或更新版本中可用
如何在 Mac Catalyst 13.0+ swift 中打开 Finder
如何仅在 Swift 中访问从相机拍摄的图像,就像 iOS 中的画廊一样?