uni-app uts插件在ios端无法触发代理
uni-app uts插件在ios端无法触发代理
代码示例
/* 引入 interface.uts 文件中定义的变量 */
import { ScanCode, HWcanCodeOptions, HWcanSuccess } from '../interface.uts';
import { HmsScanOptions, HmsDefaultScanViewController, HMSScanFormatTypeCode, DefaultScanDelegate } from "ScanKitFrameWork"
class ScanDelegate implements DefaultScanDelegate{
defaultScanDelegate(resultDic:any[]){
DispatchQueue.main.async(execute = () : void => {
console.log(resultDic);
})
}
}
let delegate: ScanDelegate = new ScanDelegate()
class ScanCodeT{
scan(){
const scanFormatType = UInt32(HMSScanFormatTypeCode.ALL.rawValue)
const options = HmsScanOptions.init(scanFormatType = scanFormatType, photo = true)
const scanVc:HmsDefaultScanViewController = HmsDefaultScanViewController(defaultScanWithFormatType = options)
scanVc.defaultScanDelegate = delegate
console.log("Scan view controller initialized")
// 检查 scanVc 是否为 null
if (scanVc != null) {
const rootVC = UTSiOS.getCurrentViewController()
rootVC.present(scanVc, animated = true)
console.log(scanVc);
} else {
console.error("Failed to initialize HmsDefaultScanViewController")
}
}
}
export function scanCode(options : HWcanCodeOptions) {
// uts方法默认会在子线程中执行,涉及 UI 操作必须在主线程中运行,通过 DispatchQueue.main.async 方法可将代码在主线程中运行
DispatchQueue.main.async(execute = () : void => {
// 创建 NativeCode 的实例
const nativeCode = new ScanCodeT()
// 设置回调函数
// nativeCode.scanResultCallback = (result) => {
// console.log("Scan result received in UTS:", result)
// }
// 调用 scan 方法
nativeCode.scan()
})
}
描述
uts插件,iOS端,做的是华为统一扫码的功能,扫码完成之后一直触发不了代理,能帮看下是哪里写的不对吗?
这是统一扫码的文档华为统一扫码文档
已解决
请问怎么解决的?
import Foundation import UIKit import AVFoundation import ScanKitFrameWork import DCloudUTSFoundation
// 定义类型别名 typealias ScanResult = [String: Any]
@objc class ScanDelegate: NSObject, DefaultScanDelegate { // 回调属性 public var scanResultCallback: (([String: String]) -> Void)?
@objc public class NativeCode: UIViewController { // 创建静态的 ScanDelegate 实例 private static let scanDelegate = ScanDelegate()
} Delegate参数类型要正确,建议用swift来做
import Foundation
import UIKit
import AVFoundation
import ScanKitFrameWork
import DCloudUTSFoundation
// 定义类型别名
typealias ScanResult = [String: Any]
@objc class ScanDelegate: NSObject, DefaultScanDelegate {
// 回调属性
public var scanResultCallback: (([String: String]) -> Void)?
[@objc](/user/objc) func defaultScanDelegateForDicResult(_ result: Any) {
console.log("Scan delegate called")
DispatchQueue.main.async {
// 确保 result 是字典类型
if let resultDict = result as? [String: Any] {
let text = resultDict["text"] as? String ?? ""
let formatValue = resultDict["formatValue"] as? String ?? ""
// 构造返回结果
let scanResult: [String: String] = [
"result": text,
"scanType": formatValue
]
// 执行回调
self.scanResultCallback?(scanResult)
} else {
console.log("Error: result is not a dictionary")
}
}
}
}
@objc public class NativeCode: UIViewController {
// 创建静态的 ScanDelegate 实例
private static let scanDelegate = ScanDelegate()
[@objc](/user/objc) public func scan(_ callback: @escaping ([String: String]) -> Void) {
// 设置回调
NativeCode.scanDelegate.scanResultCallback = callback
DispatchQueue.main.async {
let options = HmsScanOptions(scanFormatType: UInt32(HMSScanFormatTypeCode.ALL.rawValue), photo: false)
if let scanVc = HmsDefaultScanViewController(defaultScanWithFormatType: options) {
scanVc.modalPresentationStyle = .fullScreen
scanVc.defaultScanDelegate = NativeCode.scanDelegate
if let topVC = self.getTopViewController() {
topVC.present(scanVc, animated: true) {
console.log("Scan view controller presented")
}
}
}
}
}
func getTopViewController() -> UIViewController? {
var topVC = UIApplication.shared.windows.first(where: { $0.isKeyWindow })?.rootViewController
while let presentedVC = topVC?.presentedViewController {
topVC = presentedVC
}
return topVC
}
}