在Xcode 11中,我创建了一个从单视图应用程序模板的新应用程序项目。我想让这个应用程序可以在iOS 12和iOS 13上运行。但是当我将部署目标切换到iOS 12时,我遇到了很多类似于以下错误消息:
UIWindowScene仅在iOS 13或更高版本中可用
我该怎么办?
UIWindowScene仅在iOS 13或更高版本中可用
我该怎么办?
Xcode 11的模板使用场景代理。场景代理及相关类是iOS 13中新增的,而在iOS 12及之前并不存在,启动过程也不同。
要使从Xcode 11应用程序模板生成的项目向后兼容,您需要标记整个SceneDelegate类以及任何引用UISceneSession的AppDelegate类中的方法为@available(iOS 13.0, *)
。
您还需要在AppDelegate类中声明一个window
属性(如果不这样做,应用程序将运行和启动,但屏幕将是黑色的):
var window : UIWindow?
结果是,当这个应用在iOS 13上运行时,场景代理有window
,但在运行iOS 12或更早版本时,应用代理有window
—— 因此你的其他代码可能需要考虑到这一点才能向后兼容。
请您将以下代码行添加到如下位置:
步骤1:-
@available(outside of SceneDelegate.swift)
@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
//...
}
步骤2:-
在AppDelegate.swift中提供一些可用的方法
// AppDelegate.swift
@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.
}
步骤3:
您应该在AppDelegate.swift文件中声明 window 属性,例如 var window: UIWindow?
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
var window: UIWindow?
var rootViewController: ViewController!
然后在didFinishLaunching方法中添加以下代码: rootViewController = ViewController()
self.window = UIWindow.init()
self.window?.bounds = UIScreen.main.bounds
self.window?.rootViewController = rootViewController
self.window?.backgroundColor = .white
self.window?.makeKeyAndVisible()
- Dilip Saket