s***@qq.com
s***@qq.com
  • 发布:2024-12-02 15:18
  • 更新:2025-01-15 14:13
  • 阅读:90

uts插件,ios端,无法触发代理

分类:uts
/* 引入 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端,做的是华为统一扫码的功能,扫码完成之后一直触发不了代理,能帮看下是哪里写的不对吗?

这是统一扫码的文档https://developer.huawei.com/consumer/cn/doc/HMSCore-Guides/ios-default-view-0000001050414343

2024-12-02 15:18 负责人:无 分享
已邀请:
s***@qq.com

s***@qq.com (作者)

已解决

不会吃鱼的猫

不会吃鱼的猫

请问怎么解决的?

  • s***@qq.com (作者)

    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 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 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
    }

    }

    Delegate参数类型要正确,建议用swift来做

    2025-01-15 14:13

s***@qq.com

s***@qq.com (作者)

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 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 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  
    }  
}  

要回复问题请先登录注册