iOS开发之Xcode11后纯代码项目工程设置
- 2019 年 12 月 26 日
- 笔记
Xcode 11发布之后,新建iOS项目工程时,会有很多变化,最大的变化是多了文件SceneDelegate
,此时如果希望通过纯代码设置界面,流程与以往会有一些不一样,本文简单介绍一下。
纯代码的条件
删除Main Interface
中的Main
,同时需要删除info.plist中的如下代码
<key>UISceneStoryboardFile</key> <string>Main</string>
项目文件变化
-
AppDelegate.swift
文件负责App的启动与终止,并负责与SceneDelegate
交接。 -
SceneDelegate.swift
文件负责管理应用程序的生命周期。
保留SceneDelegate
-
AppDelegate
中通过application(_:configurationForConnecting:options)
返回一个UISceneConfiguration
实例 - 完成启动后,控制权被交接给
SceneDelegate
,它的scene(_:willConnectTo:options:)
将会被调用,设置window
的根视图控制器
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { guard let windowScene = (scene as? UIWindowScene) else { return } //创建window self.window = UIWindow(windowScene: windowScene) //设置window的rootViewController self.window?.rootViewController = ViewController() self.window?.makeKeyAndVisible() }
不保留SceneDelegate
- 删除
SceneDelegate.swift
- 删除info.plist中的如下内容

删除内容.png
3.AppDelegate.swift
中代码写成和Xcode11之前的样子
var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { //创建window self.window = UIWindow(frame: UIScreen.main.bounds) //设置window的rootViewController self.window?.rootViewController = ViewController() self.window?.makeKeyAndVisible() return true }
4. AppDelegate.swift
中的如下代码
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {} func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {}