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>) {}