Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Integrating SwiftUI with UIKit and back again

Integrating SwiftUI with UIKit and back again

The introduction of SwiftUI brought a whole new paradigm to developing apps on all of Apple's platforms. Certainly now that iOS 14 is here, it's never been a better way to learnQ However in most day-to-day codebases, getting SwiftUI to integrate nicely in existing UIKit code will be of critical importance. This presentation covers integrating SwiftUI into UIKit, and vice versa, in order to use the best of both worlds to build better products faster.

Tim Oliver

October 01, 2020
Tweet

More Decks by Tim Oliver

Other Decks in Programming

Transcript

  1. Integrating SwiftUI with UIKit and back again Tim Oliver iOS

    Software Engineer, Drivemode @TimOliverAU
  2. Who? •From Perth, Australia, currently living in Japan. •Been a

    fan of iOS development since iPhone 3G. •Full-time iOS developer since 2013, at Realm and Mercari, and Drivemode. •Loves publishing open source libraries. •Enjoys karaoke and video games in free time.
  3. The Good ‘ol Days •400 Mhz single core CPU •128MB

    of system memory. •320x480 screen size. •Extremely slow GPU. •No Grand Central Dispatch. •Manual memory management. •Some language called “Objective-C”
  4. Our recommended best practice is to support the latest shipping

    OS version (currently iOS 13), plus one prior version (iOS 12). The vast majority of devices support this mix of iOS versions. Apple Staff Member, June 2020
 https://developer.apple.com/forums/thread/650223
  5. •It’s absolutely amazing. •Saves so much time and effort. ☺

    •Potentially quite limiting. •MANY edge cases. •Potential show-stopping bugs. Thinking About It
  6. By default, aim to build in SwiftUI. If the quality

    dips too much, or the implementation gets too hacky, defer back to UIKit. New Modus Operandi
  7. An Example •Exploring building entirely in SwiftUI. •Big requirement that

    app allows dynamic routing/UI state restoration. •“Possible”, but quite hacky in SwiftUI. •Quite trivial to combine SwiftUI with UINavigationController.
  8. Wrapping in a classic view controller •It’s completely possible to

    make the external interface look like a regular UIKit view controller.
  9. What about pushing new view controllers? •Not accessible to View

    by default. •“Could” manually inject from UIHostingController •Actually helps promote decoupling between views. •Check out the “coordinator” or “router” code patterns.
  10. Delegates / Event Callbacks • You can expose regular closures

    from a SwiftUI view. •Can access directly via rootView property. •Can be used to route interactions to any of the regular Cocoa methods
  11. One big gotcha. •SwiftUI is value type (Structs), UIKit is

    reference type (objects) PageView
 (UIView Container) Receives Array of
 SwiftUI views UIPageViewController Generated Hosting Controllers UIPageViewController Delegate Updated state, which refreshed hosted SwiftUI views •SwiftUI elements will be refreshed, but UIKit ones do not. •Compare UIKit state to SwiftUI to avoid infinite loops.
  12. Getting Position of Elements •Use gesture recognisers to capture the

    location. •Can also potentially use GeometryReader feature. •Somewhat brittle if layout changes after the fact.
  13. In Conclusion •Take advantage of SwiftUI’s speed and flexibility. •Be

    aware of limitations, so you can mitigate if necessary. •Best of both worlds can create some amazing experiences quickly. •Have fun!