Skip to content

Instantly share code, notes, and snippets.

@m4rr
Created March 31, 2025 15:06
Show Gist options
  • Save m4rr/f43d5728732e4bea84dbb500f19e20da to your computer and use it in GitHub Desktop.
Save m4rr/f43d5728732e4bea84dbb500f19e20da to your computer and use it in GitHub Desktop.
Write
Sign in
Home
Following
Library
Stories
Stats
Top highlight
Migrating from SwiftUI to UIKit App Lifecycle
Marcus Ziadé
Marcus Ziadé
·
Follow
4 min read
·
Apr 13, 2023
Are you considering switching your app’s lifecycle from SwiftUI to UIKit? In this article, I’ll guide you through the process of migrating from SwiftUI to UIKit, while also discussing the differences between these two popular frameworks. By the end, you’ll better understand which approach best suits your app’s needs.
Introducing the Dynamic Duo: SwiftUI and UIKit
SwiftUI is the newcomer on the block, while UIKit has been the go-to choice for iOS engineers for years. Both have unique qualities, but which should you choose for your app’s lifecycle?
SwiftUI is a declarative UI framework introduced by Apple in 2019. It has a more modern syntax and simplifies the development process by automatically handling many aspects of the app’s user interface. On the other hand, UIKit has been the foundation of iOS development since the beginning, offering a more traditional, imperative approach to UI creation.
The Great Migration: Switching from SwiftUI to UIKit
If you started with SwiftUI but want to switch to the UIKit lifecycle, fear not — the process is relatively straightforward. The following steps outline how to migrate your app’s lifecycle from SwiftUI to UIKit:
1. Remove the SwiftUI App protocol conformance
Remove the @main attribute and the conformance to the App protocol from your existing SwiftUI App struct. Your code should look like this:
import SwiftUI
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
Change it to or remove the entire file:
import SwiftUI
struct MyApp {
}
2. Create a new AppDelegate and SceneDelegate:
Create a new AppDelegate.swift file, if not already present, and add the following code:
import UIKit
@UIApplicationMain
final class AppDelegate: UIResponder, UIApplicationDelegate {
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
return true
}
}
Create a new SceneDelegate.swift file with the following code:
import UIKit
import SwiftUI
final class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(
_ scene: UIScene,
willConnectTo session: UISceneSession,
options connectionOptions: UIScene.ConnectionOptions
) {
let contentView = ContentView()
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}
}
3. Update the Info.plist
Open your app’s Info.plist file and add the following entries:
(You can edit the raw info.plist XML by right-clicking Info.plist > Open As > Source Code)
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
<false/>
<key>UISceneConfigurations</key>
<dict>
<key>UIWindowSceneSessionRoleApplication</key>
<array>
<dict>
<key>UISceneConfigurationName</key>
<string>Default Configuration</string>
<key>UISceneDelegateClassName</key>
<string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
</dict>
</array>
</dict>
</dict>
This configuration specifies the SceneDelegate class that will be used for the application’s window management.
4. Build and run your app
At this point, you’ve successfully switched your app to use the UIKit lifecycle. Build and run your app to ensure everything is working as expected. If you encounter any issues, double-check the code changes you made in the previous steps.
Remember that when using the UIKit lifecycle, you must handle app-level and scene-level events in the AppDelegate and SceneDelegate classes.
Evaluating the Pros and Cons Before you make the switch, consider the pros and cons of each approach:
SwiftUI Pros:
Modern, concise, and easy-to-read syntax
The declarative UI paradigm simplifies development
Built-in support for features like dark mode and accessibility
Seamless integration with Swift
SwiftUI Cons:
Less mature framework with fewer resources and third-party libraries
It may not cover all use cases, necessitating fallbacks to UIKit
It has several minor issues that UIKit does not (Perhaps this is why you’re reading this article? It sure inspired me to write it.)
UIKit Pros:
Mature, well-documented framework with a vast ecosystem
Greater control over UI elements and behavior
Open AI’s gpt-4.0 model knows it much better than SwiftUI
A wide range of third-party libraries and resources are available
UIKit Cons:
More verbose syntax and a steeper learning curve
The imperative programming style may be less intuitive for some engineers
Requires more manual configuration and management of UI elements
Conclusion
The decision between SwiftUI and UIKit will depend on your app’s requirements, familiarity with the frameworks, and willingness to adapt to new paradigms.
If you’re starting a new project, SwiftUI is an excellent choice, especially if you want to take advantage of the latest Swift features and streamline your development process. However, if you need to support older iOS versions or require more control over your app’s UI, UIKit may be the better option.
By understanding the strengths and weaknesses of SwiftUI and UIKit, you’ll be better equipped to choose the right lifecycle for your iOS app and create a fantastic user experience. Thanks for reading.
Swift
IOS
Lifecycle
App
Scenedelegate
Marcus Ziadé
Written by Marcus Ziadé
19 Followers
·
0 Following
Responses (1)
Write a response
What are your thoughts?
Also publish to my profile
楊明達
楊明達
Feb 11, 2024
Thanks for the article!
Though I suggest add an additional step: Uninstall any previously installed old app.
As mentioned in https://stackoverflow.com/questions/67862497/xcode-12-project-with-ios-13-swiftui-support-scenedelegate-functions-not-invoke, this will ensure that the new scene delegate methods will be called.
More from Marcus Ziadé
UILabel: The Ultimate Guide
Marcus Ziadé
Marcus Ziadé
UILabel: The Ultimate Guide
UILabel is a fundamental component of the UIKit framework, and it’s the go-to control for displaying static text in iOS applications. With…
Jul 26, 2023
68
2
A Deep Dive into Swift’s UIKit UISegmentedControl
Marcus Ziadé
Marcus Ziadé
A Deep Dive into Swift’s UIKit UISegmentedControl
UISegmentedControl is an essential UI component within the UIKit framework for iOS development. As a convenient interface for users to make…
Jul 26, 2023
5
Xcode Management With Xcodes & Aria2
Marcus Ziadé
Marcus Ziadé
Xcode Management With Xcodes & Aria2
Want to spend less time frustrated with Xcode downloading from the Mac App Store? Then continue reading. I’m going to show you the best way…
Dec 18, 2021
62
Big O Time Complexity 101 for Swift Developers
Marcus Ziadé
Marcus Ziadé
Big O Time Complexity 101 for Swift Developers
When I was less experienced as a software engineer, I had the misconception that learning data structures and algorithms (DSA) were not…
Jan 20, 2023
13
See all from Marcus Ziadé
Recommended from Medium
12 Powerful SwiftUI Component
Stackademic
In
Stackademic
by
Jerry PM
12 Powerful SwiftUI Component
You should start using this component to improve your code
Mar 24
59
Creating an iOS Screen Time Tracking App Using SwiftUI and Apple’s DeviceActivity Framework
Muhammad Danish Qureshi
Muhammad Danish Qureshi
Creating an iOS Screen Time Tracking App Using SwiftUI and Apple’s DeviceActivity Framework
In iOS 15, Apple introduced the Screen Time API with three new frameworks: 1)Family Controls, 2) Managed Settings, and 3) Device Activity…
Nov 18, 2024
24
2
Introduction to Charts in SwiftUI
Bruno Lorenzo
Bruno Lorenzo
Introduction to Charts in SwiftUI
Image by Author
Jan 23, 2024
442
3
Creating Interactive Toast Notifications with SwiftUI
Commit Studio
Commit Studio
Creating Interactive Toast Notifications with SwiftUI
In this article, we’ll walk through how to build an interactive toast notification system in SwiftUI. Toasts are temporary popups that…
Oct 27, 2024
12
Seamless Device Location Monitoring in iOS (Part 1)
Boundless Programming
In
Boundless Programming
by
Programming passion
Seamless Device Location Monitoring in iOS (Part 1)
We will learn how to seamlessly monitor device location using the available tools from CoreLocation.
Jan 4
12 Xcode Hacks Every iOS Developer Must Know
Swiftfy
In
Swiftfy
by
Baljit Kaur
12 Xcode Hacks Every iOS Developer Must Know
Boost Your Productivity and Debug Smarter with These Insider Tips for Xcode
Dec 8, 2024
418
8
See more recommendations
Help
Status
About
Careers
Press
Blog
Privacy
Rules
Terms
Text to speech
To make Medium work, we log user data. By using Medium, you agree to our Privacy Policy, including cookie policy.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment