How can I include this swift declaration only before iOS 13.0

232 views Asked by At

I have the following declaration in my AppDelegate.swift file.

var window: UIWindow?

and I want to include it only when the deployment target is set to below iOS 13.0. I have looked at

@available(...)

attribute but I don't think it can do what I want. Essentially I want to be able to build my project for iOS 12 or iOS 13 with the minimum number of changes to my project in AppDelegate.swift and SceneDelegate.swift. I have made changes already to AppDelegate.swift and SceneDelegate.swift using @available(iOS 13.0, *) and #available(iOS 13.0, *) but I still need to be able to exclude this other declaration when the deployment target is above iOS 12.

Here is my AppDelegate.swift file:

import UIKit
import Firebase

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow? // only include for builds < iOS 13.0
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {                          
        if #available(iOS 13.0, *) {
            // Do nothing
        } else {     
            window = UIWindow() // only include for builds < iOS 13.0
            window?.rootViewController = UINavigationController(rootViewController: LoginController()) // only include for builds < iOS 13.0
            window?.makeKeyAndVisible() // only include for builds < iOS 13.0      
        } 
        FirebaseApp.configure()
        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.
    }
}

and here is my SceneDelegate.swift file:

import UIKit

@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let scene = (scene as? UIWindowScene) else { return }
        window = UIWindow(windowScene: scene)
        window?.rootViewController = UINavigationController(rootViewController: LoginController())
        window?.makeKeyAndVisible()
    }
    // ...
}

Essentially all I want to have happen is I just change the deployment target and the appropriate bits of code get included or excluded in the build process.

1

There are 1 answers

0
matt On BEST ANSWER
var window: UIWindow? // only include for builds < iOS 13.0

Just leave the declaration alone. On iOS 12 and before it will refer to the app window. On iOS 13 and later it will be nil and no harm done.