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