Home > other >  Inserting UIKit content into a SwiftUI view hierarchy
Inserting UIKit content into a SwiftUI view hierarchy

Time:10-29

I'm sorry if the title is confusing, i'm kind of new to the whole thingy.

I'm trying to integrate PassBase ID verification to my app, which is built using SwiftUI, their documentation offers instructions using Swift and view Controllers. My question is, is there a way to insert the Swift code part into my SwiftUI view?

The code example from their Documentation:

import Passbase
import UIKit
class ViewController: UIViewController, PassbaseDelegate {
override func viewDidLoad() {
    super.viewDidLoad()
    PassbaseSDK.delegate = self
    // Optional - You can prefill the email to skip that step.
    Passbase.prefillUserEmail = "[email protected]"
    let button = PassbaseButton(frame: CGRect(x: 40, y: 90, width: 300, height: 60))
    self.view.addSubview(button)
}

func onFinish(identityAccessKey: String) {
  print("onFinish with identityAccessKey \(identityAccessKey)")
}

func onSubmitted(identityAccessKey: String) {
  print("onSubmitted with identityAccessKey \(identityAccessKey)")
}

func onError(errorCode: String) {
  print("onError with code \(errorCode)")
}

func onStart() {
  print("onStart")
}
}

As i understand this part of code should create a button in a VC. My goal is to add this button with functionality to my SwiftUI view.

Full Documentation: https://docs.passbase.com/ios#general

Thank you all in advance for the help!

CodePudding user response:

The basic strategy is to use a View that represents the content you want to bring in from a UIViewController. Your View is going to conform to UIViewCotrollerRepresentable and use the functions of that protocol to create and manage the UIKit content.

The UIViewControllerRepresentable documentation is here

And, as was commented on your original post by vadian, there is A tutorial with sample code

With the sample code above, I would rename "ViewController" to be something like PassBaseViewController or PBViewController, then you would create a View that derives from UIViewControllerRepresentable

You end up with a file called PBViewController.swift that has your code from above:

import Passbase
import UIKit

class PBViewController: UIViewController, PassbaseDelegate {
override func viewDidLoad() {
    super.viewDidLoad()
    PassbaseSDK.delegate = self
    // Optional - You can prefill the email to skip that step.
    Passbase.prefillUserEmail = "[email protected]"
    let button = PassbaseButton(frame: CGRect(x: 40, y: 90, width: 300, height: 60))
    self.view.addSubview(button)
}

... and the rest of the code from your question here ...

Then (probably in another file, but not necessarily) you could create the SwiftUIView that uses that view controller:

struct PassBaseView : UIViewControllerRepresentable {
    typealias UIViewControllerType = PBViewController

    func makeUIViewController(context: Context) -> PBViewController {
        return PBViewController()
    }

    func updateUIViewController(_ uiViewController: PBViewController, context: Context) {
        /* code here to make changes to the view controller if necessary when this view is updated*/
    }
}
  • Related