iOS 13 Xcode11变化 项目迁移准备

1.创建根视图控制器

Xcode11之前
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
      self.window = UIWindow(frame: UIScreen.main.bounds)
      let rootvc = RootViewController()
      self.window.rootViewController = rootvc
      self.window.makeKeyAndVisible()
      return true
}

Xcode11

iOS 13 Xcode11变化 项目迁移准备_第1张图片
QQ20191015-211731.png

报错了 Use of unresolved identifier 'window',查看代码发现Xcode11 AppDelegate.swift 中删除了 var window: UIWindow?之后都由 SceneDelegate处理了,所以不能在 AppDelegatefunc application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool创建根视图控制器了

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let windowscene = (scene as? UIWindowScene) else { return }
        window = UIWindow(windowScene: windowscene)
        window?.frame = UIScreen.main.bounds
        let rootvc = RootViewController()
        let nav = UINavigationController(rootViewController: rootvc)
        window?.rootViewController = nav
        window?.makeKeyAndVisible()
    }

2.获取UIWindow

Xcode11之前
let window = UIApplication.shared.keyWindow
Xcode11
UIApplication.shared.windows.first

for scene in UIApplication.shared.connectedScenes {
     let windowscene: UIWindowScene = scene as! UIWindowScene
     if windowscene.activationState == .foregroundInactive {
         let window = windowscene.windows.first!
         break
     }
 }

let window = (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.window

你可能感兴趣的:(iOS 13 Xcode11变化 项目迁移准备)