1

I have an app that is currently made in UIKit, and I would like to fully migrate it to SwiftUI, removing all traces of UIKit.

I assume that replacing Main.storyboard and all of the ViewControllers with a SceneDelegate and ContentView won't be suffice for the migration.

What do I need to change in the project settings or overall to remove all traces of UIKit and replace it with SwiftUI? Is this even possible? Do I need to just create a new project and copy over all the custom settings?

Ken Mueller
  • 3,659
  • 3
  • 21
  • 33
  • I'd advise against something like this. SwiftUI is really a totally different "stack", and it's made to co-exist with UIKit. Also, SwiftUI will **only** run on iOS 13. If you currently have an app available that runs on iOS 12 and under, to keep support for those users - and there will be quite a few of them - why not either (a) incorporate SwiftUI views by using `UIHostingController1 or (b) start up a new project/fork as a SwiftUI project and pull in what you need - and there will be quite a bit - from UIKit as either a `UIViewRepresentable` or a `UIViewControllerRepresentable`? –  Sep 06 '19 at 09:45
  • #2... regards to pulling in UIKit components. My *new* app already pulls in four. (1) A replacement for `UIImagePickerController` because there is **no** SwiftUI component to do Image picking. (2) `UIActivityViewController` for the same reason. (3) A view controller wrapper for a `MTKView` because I need GPU CoreImage rendering. Finally, (4) A combination UINavigstionController with two table views because of the restrictions/bugginess of the Swiftui components - I'd do this as a child view controller in UIKit and embedding this in a SwiftUI view absolutely sucks. –  Sep 06 '19 at 09:49
  • How would I pull in a replacement for `UIImagePickerController` in SwiftUI? – Ken Mueller Sep 06 '19 at 17:49
  • Don't mean to sound as harsh as it might. (1) Accept that you'll need to use a `UIKit` picker - and as of this morning, 13.1 beta 2 fixes `UIImagePickerController`. (2) Since you do, accept that you'll need to use `UIViewControllerRepresentable` in all cases, unless you care to code a SwiftUI collection view - something else that really doesn't exist yet - along with everything else in your picker. (3) Expose your delegate methods - or use Notifications or Combine - in your representable. Continued.... –  Sep 06 '19 at 18:59
  • ... since Apple fixed whatever issue 13.1 beta 1 broke, I'd suggest using `UIImagePickerController`. (BTW, that breaks one of your major regiments in your question - removing all traces of UIKit.) here's two link that should be of help - one very old (beta 1) and one rather new: https://stackoverflow.com/questions/56515871/how-to-open-the-imagepicker-in-swiftui https://stackoverflow.com/questions/56515871/how-to-open-the-imagepicker-in-swiftui/57362192#57362192 –  Sep 06 '19 at 19:02
  • Sorry for the dup links! There are two very good answers in there and I failed to notice! (Obviously I bookmarked things and both answers were 6 weeks apart.) –  Sep 06 '19 at 19:29
  • Thanks for the help! This issue will probably be fixed by the time SwiftUI comes out of beta, but if it doesn't, I'll use the workaround that the answer you linked gave. – Ken Mueller Sep 06 '19 at 21:06
  • I'm not sure what issue you are talking about - but if you need to use a `UIImagePickerController` (which 13.1 beta 1 broke in *both* SwiftUI and UIKit) I *stongly* recommend using a `UIViewControllerRepresentable`. I'm not planning on any "pure" SwiftgUI functionality like this until... version 3. –  Sep 06 '19 at 21:38
  • Not exactly an issue, I was just referring to how SwiftUI is still lacking in certain functionality that UIKit has. What do you mean by version 3? When does it come out? – Ken Mueller Sep 07 '19 at 05:07
  • By version 3, I mean what they'll introduce at WWDC 2019. –  Sep 07 '19 at 08:36

1 Answers1

2

Don't worry about "removing traces." Fun fact: Importing SwiftUI at the top of a file ALSO imports UIKit.

What you said about the SceneDelegate and a UIHostingController is all you need actually. The only view controller you should ever need to load is the hosting controller. You literally just have a root view (ContentView or whatever) and start your whole app from that initial view. Be creative about your structure. Now is the time to experiment.

cookednick
  • 1,025
  • 9
  • 16
  • Will this work with the preview? What are the steps needed to migrate to Swift 5.1 and should I move over `Preview Content` as well? Thanks for the answer! – Ken Mueller Sep 06 '19 at 17:58
  • Not exactly sure what you're asking, but just start making views. At least one for every screen. Each one can have its own preview, and navigating from the first screen's preview should let you navigate through your app. – cookednick Sep 07 '19 at 19:21
  • I was searching to do the same thing(migration from UIKit to swiftUI) and found this that might be useful: https://nikrodionov.com/project-migration-to-swiftui/ – Jafar Khoshtabiat Nov 18 '19 at 08:50