我正在开发华为统一扫码的uts插件,iOS端.用的是swift混编方式,现在遇到了问题:
import Foundation
import UIKit
import AVFoundation
import ScanKitFrameWork
import DCloudUTSFoundation
public class NativeCode: UIViewController, DefaultScanDelegate {
// 定义一个闭包属性,用于回调结果
public var scanResultCallback: ((String) -> Void)?
@objc func scan() {
// 使用 DCloudUTSFoundation 中的 console.log
console.log("Scan method called")
let options = HmsScanOptions.init(scanFormatType:
UInt32(HMSScanFormatTypeCode.QR_CODE.rawValue |
HMSScanFormatTypeCode.DATA_MATRIX.rawValue),
photo: false)
if let scanVc = HmsDefaultScanViewController(defaultScanWithFormatType: options) {
console.log("Scan view controller initialized")
scanVc.defaultScanDelegate = self
DispatchQueue.main.async {
if let currentVC = self.getTopViewController() {
console.log("Presenting scan view controller")
currentVC.present(scanVc, animated: true, completion: {
console.log("Scan view controller presented")
})
currentVC.navigationController?.isNavigationBarHidden = true
} else {
console.log("Failed to get top view controller")
}
}
} else {
console.log("Failed to initialize scan view controller")
}
}
private func getTopViewController() -> UIViewController? {
var topVC = UIApplication.shared.keyWindow?.rootViewController
while let presentedVC = topVC?.presentedViewController {
topVC = presentedVC
}
return topVC
}
public func defaultScanImagePickerDelegate(for image: UIImage!) {
DispatchQueue.main.async {
console.log("defaultScanImagePickerDelegate called")
//在主线程内处理数据
}
}
public func defaultScanDelegate(forDicResult resultDic: [AnyHashable: Any]!) {
DispatchQueue.main.async {
let formatValue: String = String(describing: resultDic!["formatValue"]!)
console.log("formatValue: \(resultDic?["formatValue"] ?? "")")
console.log("formatValue: \(formatValue)")
console.log("formatValue: \(self.getScanType(formatValue: formatValue))")
console.log("text: \(String(describing: resultDic["text"]))")
let parserDic: [String: String]? = resultDic["parserDic"] as? [String: String]
console.log("sceneType: \(parserDic?["sceneType"] ?? "")")
console.log("sceneType: \(self.getScanTypeFormat(formatValue: parserDic?["sceneType"]! ?? ""))")
let bytes: [String]? = resultDic["rawBytes"] as? [String]
console.log("rawBytes: \(bytes ?? [String]())")
// self.result?([
// "scanTypeForm": self.getScanType(formatValue: formatValue),
// "value": resultDic!["text"],
// "scanType": self.getScanTypeFormat(formatValue: parserDic?["sceneType"] ?? ""),
// ])
}
}
//获取扫码类型编码
func getScanType(formatValue: String?) -> Int {
switch formatValue {
case "QR_CODE":
return 1
case "AZTEC":
return 2
case "DATA_MATRIX":
return 4
case "PDF_417":
return 8
case "CODE_39":
return 16
case "CODE_93":
return 32
case "CODE_128":
return 64
case "EAN_13":
return 128
case "EAN_8":
return 256
case "ITF":
return 512
case "UPC_A":
return 1024
case "UPC_E":
return 2048
case "CODABAR":
return 4096
default:
return 0
}
}
//获取扫码类型编码
func getScanTypeFormat(formatValue: String) -> Int {
switch formatValue.uppercased() {
case "NUMBER":
return 1001
case "EMAIL":
return 1002
case "TEL":
return 1003
case "TEXT":
return 1004
case "SMS":
return 1005
case "WEBSITE"://url
return 1006
case "WI-FI":
return 1007
case "EVENT":
return 1008
case "CONTACT":
return 1009
case "DRIVER":
return 10010
case "LOCATION":
return 10011
case "BOOK":
return 10012
case "VEHICLE":
return 10013
case "PURE_TEXT":
return 10014
default:
return 0
}
}
}
扫码完成之后,defaultScanDelegate代理没有被触发,无法获取到扫码结果
想问一下这可能是什么原因
s***@qq.com (作者)
代码我上传到附件里,在下面一条回复中
2024-11-29 17:32