­

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

  1. AppDelegate中通过application(_:configurationForConnecting:options)返回一个UISceneConfiguration实例
  2. 完成启动后,控制权被交接给 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

  1. 删除SceneDelegate.swift
  2. 删除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>) {}