DEV Community

Cover image for Setting Up ViewCode Projects for Versions Below iOS 13
Gustavo Guedes
Gustavo Guedes

Posted on

Setting Up ViewCode Projects for Versions Below iOS 13

In iOS development, a significant change was introduced with iOS 13, bringing the SceneDelegate to facilitate multi-window support on iPads and other functionalities. However, when working with earlier versions of iOS, the SceneDelegate is not used, and the initial setup of the application must be done directly in the AppDelegate.

In this article, I'll guide you through the process of configuring your iOS application using the AppDelegate, ensuring compatibility with versions prior to iOS 13.

What is AppDelegate?

The AppDelegate is the entry point of the application. It is responsible for responding to events that affect the application's lifecycle, such as launching, transitioning to the background, and termination.

Setting Up the Initial Interface in AppDelegate

For iOS versions earlier than 13, where SceneDelegate is not available, the entire initial setup process should occur in the application(_:didFinishLaunchingWithOptions:) method of AppDelegate. Here, you must configure the main window of the application and set the initial UIViewController.

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        // Initialize the main window
        window = UIWindow(frame: UIScreen.main.bounds)

        // Create the initial ViewController
        let initialViewController = YourInitialViewController()

        // Set the initial ViewController as the rootViewController
        window?.rootViewController = initialViewController

        // Make the window visible
        window?.makeKeyAndVisible()

        return true
    }
}
Enter fullscreen mode Exit fullscreen mode

Understanding the Code

  • Window Initialization: window = UIWindow(frame: UIScreen.main.bounds) creates a new instance of UIWindow with the dimensions of the device's screen.

  • Initial ViewController: Next, we create an instance of the ViewController that we want to be displayed when the application starts. This is where you can configure your initial interface.

  • Root ViewController: We set the window's rootViewController to the initial ViewController.

  • Displaying the Window: window?.makeKeyAndVisible() makes the window visible and sets it as the main window of the application.

Why Not Use SceneDelegate?

For devices running iOS 13 or later, using the SceneDelegate is recommended. However, if you need to support earlier versions, it’s necessary to configure the initial interface in AppDelegate as shown above. This ensures your application works correctly across a broader range of devices.

Conclusion

When configuring your application to support iOS versions earlier than 14, understanding the role of AppDelegate is essential. The absence of SceneDelegate in these versions means that the initial setup of the application must occur in AppDelegate. With the instructions provided, you are ready to correctly configure your application and ensure compatibility with older versions of iOS.

Top comments (0)