Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to migrate an old Xcode project to use SwiftUI?

Tags:

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

like image 290
Ailton Vieira Pinto Filho Avatar asked Jun 09 '19 14:06

Ailton Vieira Pinto Filho


People also ask

Is SwiftUI same as Xcode?

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.

Why is SwiftUI so fast?

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.

Should I start new project with SwiftUI?

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.


1 Answers

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:

enter image description here

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.

enter image description here

enter image description here


Addendum II:

Clean Derived Data, as many devs in the comments suggest.

like image 166
Matteo Pacini Avatar answered Nov 17 '22 09:11

Matteo Pacini