Xcode 11向后兼容:“ UIWindowScene仅在iOS 13或更高版本中可用”

2020/12/02 23:52 · ios ·  · 0评论

在Xcode 11中,我从Single View App模板创建了一个新的应用程序项目。我希望这个应用程序可以在iOS 12和iOS 13中运行。但是,当我将部署目标切换到iOS 12时,我收到了很多类似以下的错误消息:

UIWindowScene仅在iOS 13或更高版本中可用

我该怎么办?

Xcode 11中的模板使用场景委托。场景委托和相关类是iOS 13中的新增功能;它们在iOS 12及更高版本中不存在,并且启动过程有所不同。

为了使从Xcode 11应用模板生成的项目向后兼容,您需要将整个SceneDelegate类以及AppDelegate类中引用UISceneSession的任何方法标记为@available(iOS 13.0, *)

您还需要window在AppDelegate类中声明一个属性(如果不这样做,则该应用将运行并启动,但屏幕将变为黑色):

var window : UIWindow?

结果是,当此应用程序在iOS 13中运行时,场景委托具有window,但在iOS 12或更低版本中运行时,该应用程序委托具有window-,然后您的其他代码可能需要考虑该因素,以便向下兼容。

您能否像下面这样添加此行代码

步骤1:-

@可用在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
    }
本文地址:http://ios.askforanswer.com/xcode-11xianghoujianrong-uiwindowscenejinzaiios-13huogenggaobanbenzhongkeyong.html
文章标签: ,   ,  
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!

文件下载

老薛主机终身7折优惠码boke112

上一篇:
下一篇:

评论已关闭!