头一次尝试调用,原理还没懂


function app_rtn(msg, data) {
    // $("#container").prepend($("<span>方法调用成功!" + msg + "</span><br/>"));
    // msg 是APP端方法名
    var params = { name: msg, data: data };
        const isiOS = /(iPhone|iPad|iPod|iOS|Mac)/i.test(navigator.userAgent)
        if (isiOS) {
            this.callNativeFunc(null, "webCall", JSON.stringify(params));
        } else {
            //android.showToast(msg);
            this.callNativeFunc("android", "webCall", JSON.stringify(params));
        }
}

/**
 * 调用native方法
 * @param interfaceName oc名称
 * @param functionName 接口名称    eg: webCall
 * @param paramString 参数
 * @constructor
 */
function callNativeFunc(interfaceName, functionName, paramString) {
    const isiOS = /(iPhone|iPad|iPod|iOS|Mac)/i.test(navigator.userAgent)
        // eslint-disable-next-line no-async-promise-executor
    return new Promise(async(resolve, reject) => {
        let result = null
        try {
            result = isiOS ? await window.prompt(functionName, paramString) : await window[interfaceName][functionName](paramString) // 同步调用返回数据

            // $("#container").prepend($("<span>发送成功!" + functionName + "==>" + paramString + "</span><br/>"));
        } catch (e) {
            // $("#container").prepend($("<span>发送失败!" + e + "</span><br/>"));
            console.error('调用native接口异常', interfaceName, functionName, paramString)
            console.log('interfaceName:', interfaceName)
            console.log('functionName:', functionName)
            console.log('paramString:', paramString)
        }
        console.info('调用native接口【' + functionName + '】,返回结果:', result)
        resolve(result)
    })
}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐