如果標題令人困惑,我很抱歉,我對整個事情有點陌生。
我正在嘗試將 PassBase ID 驗證集成到我的應用程式中,該應用程式是使用 SwiftUI 構建的,他們的檔案提供了使用 Swift 和視圖控制器的說明。我的問題是,有沒有辦法將 Swift 代碼部分插入到我的 SwiftUI 視圖中?
他們檔案中的代碼示例:
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")
}
}
據我了解,這部分代碼應該在 VC 中創建一個按鈕。我的目標是將此具有功能的按鈕添加到我的 SwiftUI 視圖中。
完整檔案:https : //docs.passbase.com/ios#general
在此先感謝大家的幫助!
uj5u.com熱心網友回復:
基本策略是使用View代表要從UIViewController. 您View將遵守UIViewCotrollerRepresentable并使用該協議的功能來創建和管理UIKit內容。
該UIViewControllerRepresentable檔案是在這里
并且,正如 vadian 對您的原始帖子所評論的那樣,有一個帶有示例代碼的教程
使用上面的示例代碼,我將“ViewController”重命名為類似PassBaseViewControlleror PBViewController,然后您將創建一個View派生自UIViewControllerRepresentable
您最終會得到一個名為 PBViewController.swift 的檔案,其中包含上面的代碼:
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 ...
然后(可能在另一個檔案中,但不一定)您可以創建使用該視圖控制器的 SwiftUIView:
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*/
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/340167.html
上一篇:獲取函式而不是鏈接
