Apple Platform Dev @ HDUHelp

Things we learnt in building iOS, watchOS and macOS apps.

Call UIKit ViewController APIs in SwiftUI?

Wrap those UIKit API calls into functions and pass them to SwiftUI views.

// SwiftUI
struct FantasticView: View {
  var dismissFunc: (() -> Void)?

  var body: some View {
    Button(action: {
      self.dismissFunc?()
    }) { Text("Flee!") }
  }
}

// subclassing UIHostingController
class FantasticViewController: UIHostingController<FantasticView> {
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder, rootView: FantasticView())
        self.rootView = FantasticView {
            self.dismiss(animated: true)
        }
    }
}