HarmonyOS鸿蒙Next中pc不支持canOpenLink来判断微信是否安装了吗

HarmonyOS鸿蒙Next中pc不支持canOpenLink来判断微信是否安装了吗

isWXAppInstalled(): boolean {
  try {
    return bundleManager.canOpenLink("weixin://")
  } catch (e) {
    let code = (e as BusinessError)?.code
    let msg = (e as BusinessError)?.message ?? ''
    if (code !== undefined) {
      if (code === 17700056) {
        msg += ` Please include "weixin" inside the "querySchemes" element of module.json5 in your app module.`
      }
      Log.e(kTag, `isWXAppInstalled get error ${msg}`)
    } else {
      Log.e(kTag, `isWXAppInstalled get error ${e}`)
    }
    return false
  }
}

更多关于HarmonyOS鸿蒙Next中pc不支持canOpenLink来判断微信是否安装了吗的实战教程也可以访问 https://www.itying.com/category-93-b0.html

6 回复

支持的,其实你可以使用Wx的SDK来判断,微信的SDK里面集成了这个判断条件的,

参考这个方法:

import * as wxopensdk from '@tencent/wechat_open_sdk'
/**
 * 检查微信是否安装
 * 未安装则尝试引导用户前往商店安装
 */
private checkWxInstalled(appId: string): boolean {
  const wxApi = wxopensdk.WXAPIFactory.createWXAPI(appId)
  const installed = wxApi.isWXAppInstalled()

  if (!installed) {
    this.openAppInStore(WxSdkHelper.WX_BUNDLE_NAME)
  }

  return installed
}

还有个必备的是使用微信SDK的配置在目录:entry/src/main/module.json5下面配置,不配置没响应

"querySchemes": [
  "weixin",
  "wxopensdk",
],

更多关于HarmonyOS鸿蒙Next中pc不支持canOpenLink来判断微信是否安装了吗的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


代码看着没什么问题,是否配置了module.json5

{
  "module": {
    "querySchemes": ["weixin"]
  }
}

找HarmonyOS工作还需要会Flutter的哦,有需要Flutter教程的可以学学大地老师的教程,很不错,B站免费学的哦:BV1S4411E7LY/?p=17

可以的,遇到了什么问题?

export function isWXAppInstalled() {
  try {
    return bundleManager.canOpenLink("weixin://")
  } catch (e) {
    let code = (e as BusinessError)?.code
    let msg = (e as BusinessError)?.message ?? ''
    if (code !== undefined) {
      if (code === 17700056) {
        msg += ` Please include "weixin" inside the "querySchemes" element of module.json5 in your app module.`
      }
      Log.e(kTag, `isWXAppInstalled get error ${msg}`)
    } else {
      Log.e(kTag, `isWXAppInstalled get error ${e}`)
    }
    return false
  }
}

// 是否已经安装了微信
export function isInstallWeiXin() {
  return async () => {
    try {
      return await bundleManager.canOpenLink('weixin://');
    } catch (err) {
      console.error(`Error checking WeChat installation. Code is ${err.code}, message is ${err.message}`);
      return false;
    }
  }
}

在HarmonyOS Next中,PC端不支持canOpenLink方法来判断微信是否安装。鸿蒙系统提供了abilityManager相关API来检测应用是否安装,具体可使用bundleManager.queryAbilityInfo接口查询微信的ability信息。若返回结果不为空,则表明微信已安装。注意该方法需要声明ohos.permission.GET_BUNDLE_INFO权限。PC端需使用鸿蒙提供的跨设备能力检测方案替代传统移动端的URL Scheme检测方式。

在HarmonyOS Next中,PC端确实不支持使用canOpenLink来判断微信是否安装。这是因为PC端的应用管理机制与移动端不同,无法通过URL Scheme的方式来检测应用安装状态。

对于PC端开发,建议改用以下替代方案:

  1. 使用bundleManager.getBundleInfo直接查询应用包信息
  2. 检查注册表中相关应用信息(Windows平台)
  3. 通过文件系统检查应用安装目录

移动端代码中的错误处理逻辑是正确的,但需要注意PC端需要完全不同的实现方式。如果需要在PC端实现类似功能,需要针对不同平台编写特定的检测逻辑。

错误码17700056的处理是恰当的,这在移动端表示需要在module.json5中声明querySchemes,但这不适用于PC环境。

回到顶部