Загрузка...

Modernizing Your iOS Project: Transitioning to UISceneDelegate

Discover how to convert your old iOS project to use `UISceneDelegate` without moving to Storyboards by following this simple guide.
---
This video is based on the question https://stackoverflow.com/q/71685394/ asked by the user 'canderse' ( https://stackoverflow.com/u/1011885/ ) and on the answer https://stackoverflow.com/a/71688414/ provided by the user 'ColeX' ( https://stackoverflow.com/u/8187800/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.

Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: iOS Convering an Old project to use UI

Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/licensing
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license.

If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Modernizing Your iOS Project: Transitioning to UISceneDelegate

As the world of iOS development continues to evolve, many developers find themselves needing to modernize their applications to utilize the latest features and practices. One common challenge is transitioning an old project to support the UISceneDelegate — an important component introduced in iOS 13 that facilitates better state restoration and allows apps to run across multiple windows.

In this post, we’ll explore how to successfully convert your legacy iOS application to incorporate UISceneDelegate, without having to rely on Storyboards. This will be especially helpful if you are still using UIViewControllers without any XIBs or Storyboards.

Understanding the Challenge

You may have an older iOS application where your AppDelegate class is responsible for creating the UIWindow and setting the UIViewController as its rootViewController in the finishedLaunching method. However, with the introduction of UISceneDelegate, this process has changed and requires a new approach.

The Existing Code

Traditionally, you might have code in your AppDelegate that looks something like this:

[[See Video to Reveal this Text or Code Snippet]]

While this works, you need to adapt your project to integrate UISceneDelegate.

The Solution: Implementing UISceneDelegate

Step-by-Step Transition

To effectively implement UISceneDelegate while maintaining your existing architecture, follow these steps:

Retain Your AppDelegate Code: Start by keeping the existing code in your AppDelegate. You will not need to refactor this part much.

Add SceneDelegate Functionality: Create or update the SceneDelegate class in your project. You will implement the necessary methods to set up the UIWindow here.

Here's a sample implementation for the WillConnect method within your SceneDelegate:

[[See Video to Reveal this Text or Code Snippet]]

Key Components Explained

windowScene: This is the scene that your app's window will be attached to. It’s provided when the scene connects.

Creating UIWindow: By using your windowScene, you create a new UIWindow instance that handles the visual interface.

Setting Up the Root View Controller: Here, you specify which UIViewController should be displayed. This maintains the traditional model without the use of Storyboards.

Making the Window Key and Visible: This method is crucial to ensure that your window appears on the screen.

Conclusion

Transitioning to UISceneDelegate can modernize your iOS application significantly while still leveraging the existing UIViewController structure. By following the above steps, you can adapt your code without resorting to Storyboards or XIBs, preserving the behavior of your app while embracing the new architecture.

If you find yourself struggling with legacy code or feel overwhelmed by the update process, remember to take it step by step and make adjustments as needed. Happy coding!

Видео Modernizing Your iOS Project: Transitioning to UISceneDelegate канала vlogize
Яндекс.Метрика

На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.

Об использовании CookiesПринять