I have an app made in Xcode 10 using Main.storyboard and would like to migrate it to use Apple's new framework: SwiftUI.
Is that already possible?
I have already tried to add the UIApplicationSceneManifest
key in Info.plist
, I changed the AppDelegate.swift
to use scenes, I created the SceneDelegate.swift
and even then I could not
Swift is a programming language that is used for building iOS and Mac applications, while Xcode is not a programming language – it is a comprehensive IDE. Xcode is the canvas to write and program the code, while Swift is the actual code that your team is writing.
Even though SwiftUI relies on UIKit and AppKit, it takes a very different approach to building user interfaces. Its declarative syntax simplifies user interface development and its integration with Xcode makes building and debugging user interfaces easier and faster than ever before.
But if you want to ship it fast, if targeting 80% of the iOS ecosystem is enough, then you should definitely go with SwiftUI (once you're comfortable with it). It's a future proof framework and it's faster to iterate on your concept, ideas, and design because of the live preview and declarative API.
I assume you're using Xcode 11 GM
and macOS Mojave
or Catalina
.
Along with the changes in the plist
, you have to add UISceneSession
lifecycle functions in the application delegate.
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { // The name must match the one in the Info.plist return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) } func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) { }
Also, you need to make sure the window
is created correctly in the SceneDelegate
.
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { guard let windowScene = scene as? UIWindowScene else { return } let window = UIWindow(windowScene: windowScene) window.rootViewController = UIHostingController(rootView: ContentView()) self.window = window window.makeKeyAndVisible() }
where ContentView
is the main SwiftUI
view you want to display.
P.S. Make sure the plist
specifies $(PRODUCT_MODULE_NAME).SceneDelegate
as delegate class name, and the scene delegate is called SceneDelegate
Example:
If you're on Catalina
, you can turn on Previews
in the build settings for your target.
Build Options -> Enable Previews
Addendum I:
Make sure you remove the Storyboard key from the Info.Plist and that you're targeting iOS 13
.
Addendum II:
Clean Derived Data
, as many devs in the comments suggest.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With