Xcode11で作成したプロジェクトのDeployment Targetを下げるとブラックアウトする問題の対処法


Xcode11とUISceneとUIWindowにまつわるトラブルシュートに追われている。
個別のケースに分解してお届け。

現象

Xcode11で作成したプロジェクトをiOS13未満の環境で実行するとブラックアウトしてしまう。

再現手順

  1. Xcode11にて、新規プロジェクトの作成を行う。
    • test off
    • SwiftUI off
  2. Project設定を開き、Deployment Targetを iOS12 にする。
    • iOS13未満ならなんでもいい
  3. iOS シミュレーターのうち、iOS12のもので実行する。
    • iOS13未満ならなんでもいい

原因

コンソールに「AppDelegateUIWindow *window が必要だ」と説明されている。

2019-09-27 07:34:57.175062+0900 sample-xcode11[52812:454025] [Application] The app delegate must implement the window property if it wants to use a main storyboard file.

新規プロジェクトではScene Based Lifecycleが有効になっている。そしてDeployment Targetも iOS 13 になっている。
Deployment targetがiOS 13であれば、Application Based Lifecycleは使われないため、AppDelegateにwindowは必要ないというAppleのおせっかいである。
※SceneDelegateがwindowを所有している。

対応

  • AppDelegatewindowを生やす。
// AppDelegate.swift
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
}
// AppDelegate.h
@interface AppDelegate: UIResponder <UIApplicationDelegate>

@property (nonatomic, strong) UIWindow *window;

@end

あとは SceneDelegate などに @available(iOS 13.0, *) を付けるようXcodeが出してくれるエラーを解決する。