Integrating SwiftUI

Description: SwiftUI is designed to integrate with your existing code base on any of Apple’s platforms. Learn how to adopt SwiftUI on any Apple platform by adding SwiftUI views into your app’s hierarchy, leveraging your existing data model and more.

We can host SwiftUI content in your UIKit views via a UIHostingController.

Representable Protocol

The protocol has three methods:

  • Make: where we create the view or view controller that we want to present in SwiftUI.
  • Update: where we update this view/ or view controller to the current configuration.
  • Dismantle: optional.

During initialization, the make method is called first, followed by the update method.

The update method can be called multiple times, whenever an update is requested by SwiftUI.

Representable Context

Used for advanced integration: perhaps we want to expose target action or delegation in SwiftUI, or we may want to read from the environment of SwiftUI and respond accordingly.

Coordinator

  • Helps with coordinating between UIVIew/Controller and SwiftUI
  • Can be used to implement common patterns, like delegation, data sources, and target action.

Environment

  • Helps read SwiftUI environment

Transaction

  • Lets our view know if there was an animation in SwiftUI.

Item Providers

  • Item providers are a great technology that's provided by Foundation, which provides us a means of moving data around your application in various forms.
  • It's also a tool that we use to help transfer data across processes.
  • We can use them to integrate with system services like drag and drop (by using .onDrag and .onDrop), pasteboard (via .onPasteCommand)

Missing anything? Corrections? Contributions are welcome 😃

Related

Written by

Federico Zanetello

Federico Zanetello

Software engineer with a strong passion for well-written code, thought-out composable architectures, automation, tests, and more.