iOS 删除新创建项目中的SceneDelegate
背景
Xcode 11之后新建工程,默认为有SceneDelegate,但是SceneDelegate是从iOS 13之后才有的,如果最低兼容版本到iOS 13以下,需要怎么做呢?
过程
首先来看一下,SceneDelegate是什么,为什么会有SceneDelegate
官方说明:
A UISceneSession object manages a unique runtime instance of your scene. When the user adds a new scene to your app, or when you request one programmatically, the system creates a session object to track that scene. The session contains a unique identifier and the configuration details of the scene. UIKit maintains the session information for the lifetime of the scene itself, destroying the session in response to the user closing the scene in the app switcher.
You do not create session objects directly. UIKit creates sessions in response to user interactions with your app. You can also ask UIKit to create a new scene and session programmatically by calling the requestSceneSessionActivation(_:userActivity:options:errorHandler:) method of UIApplication. UIKit initializes the session with default configuration data based on the contents of your app’s Info.plist file.
翻译解释:
Xcode 11新创建的项目涉及到SceneDelegate的地方如下:
- AppDelegate类中两个“scene sessions”方法:application(:configurationForConnecting:options:) 和 application(:didDiscardSceneSessions:)
- 一个SceneDelegate类,其中包括生命周期事件,例如active,resign和disconnect。
- Info.plist文件中提供了”Application Scene Manifest“配置项,用于配置App的场景,包括它们的场景配置名,delegate类名和storyboard入口
那不需要SceneDelegate,要怎么处理?
两种方法,
a. 一种是直接把SceneDelegate相关的删除
b. 另外一种则是根据系统版本判断兼容
方法一:删除SceneDelegate
- 把AppDelegate中UISceneSession Lifecycle的两个代理方法删除,添加window属性,在application:didFinishLaunchingWithOptions:方法中初始化window,设置根视图
- 删除SceneDelegate文件
- 选中target,切换到info,删除Application Scene Manifest这行
1 |
|
方法二:根据系统版本判断兼容,但是这种方法要注意,iOS 13之后有些程序状态的处理要在SeceneDelegate中
- 首先在SceneDelegate中加入@available(iOS 13, *)的声明,
- 然后把AppDelgate中UISceneSession Lifecycle的两个代理方法写到单独的Extension中,然后声明@avaiable(iOS 13, *),
- AppDelegate的启动方法中也需要修改,编译即可
1 |
|
1 |
|