0
1

More than 3 years have passed since last update.

Xcode11で作成したプロジェクトをiOS12に対応させる方法

Posted at

はじめに

久しぶりに新規にプロジェクトを作ったのですが、iOS12で動かなかったのでその対応方法です。
Xcode11から SceneDelegate というものがテンプレートで生成されるようになりました。iOS13以降で同一アプリを複数表示するSceneという概念が導入されたことに伴う変更なのですが、iOS12ではSceneの概念がないためクラッシュします。
そのため、iOS12に対応させるためにはひと手間必要になってしまいました。iPhone5sやiPhone6など、まだまだ現役なので対応させない手はありませんよね。

開発環境

  • Xcode11.5
  • Swift

対応内容

AppDelegate

UISceneSession Lifecycleの2つのメソッドをiOS13以降のみビルドされるように修正します。また、SceneDelegateで定義されるようになったUIWindowをAppDelegateに追加します。

AppDelegate.swift
@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow? // この行を追加

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        return true
    }

    // MARK: UISceneSession Lifecycle
    @available(iOS 13.0,*) // この行を追加
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    @available(iOS 13.0,*) // この行を追加
    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }
}

SceneDelegate

SceneDelegateはiOS12に対応していないため、クラスごとiOS13以降にします、

SceneDelegate.swift
@available(iOS 13.0,*)  // この行を追加
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
  :
}

最後に

プロジェクトをゼロから作ることが案外少ないため、久しぶりにテンプレートから作成したらいきなり動かなくてびっくりしました。
iOSは進化が早いというか、後方互換性が切り捨てられるというか、ついていくのが大変ですね。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1