HarmonyOS鸿蒙Next中APP如何判断是否为鸿蒙系统(ArkTs)

HarmonyOS鸿蒙Next中APP如何判断是否为鸿蒙系统(ArkTs) 在开发H5时,直接通过navigator.userAgent就能获取浏览器所处环境,在鸿蒙系统中,需要如何判断当前App是否是鸿蒙系统,或者是设备信息呢。

我在使用deviceInfo接口时,获取到的是个空对象{},下面的属性都为undefined

请问如何判断鸿蒙系统,有什么好的解决办法吗?

5 回复

您好,找到解决办法了吗?我也需要在h5中判断是否在华为app

更多关于HarmonyOS鸿蒙Next中APP如何判断是否为鸿蒙系统(ArkTs)的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


网络请求你自己加上这个请求头试试呢

在这里只能拿到defaultdefault默认就是华为手机移动端,但是没办法拿到像navigator.userAgent的数据,后台接口是根据接口请求头中的user-agent去判断是否为移动端的,这种有什么好的解决办法吗?

在HarmonyOS鸿蒙Next中,使用ArkTS判断当前系统是否为鸿蒙系统,可以通过@ohos.system.device模块中的deviceInfo接口来实现。具体步骤如下:

  1. 导入@ohos.system.device模块。
  2. 使用deviceInfo.getDeviceInfo()方法获取设备信息。
  3. 检查返回的设备信息中osFullName字段是否包含"HarmonyOS"或"鸿蒙"字样。

示例代码如下:

import deviceInfo from '@ohos.system.device';

let deviceInfo = deviceInfo.getDeviceInfo();
if (deviceInfo.osFullName.includes("HarmonyOS") || deviceInfo.osFullName.includes("鸿蒙")) {
    console.log("当前系统是鸿蒙系统");
} else {
    console.log("当前系统不是鸿蒙系统");
}

通过这种方式,可以准确判断当前运行环境是否为鸿蒙系统。

在HarmonyOS鸿蒙Next中,可以通过@ohos.system.device模块的getDeviceInfo方法获取设备信息,判断是否为鸿蒙系统。示例代码如下:

import device from '@ohos.system.device';

device.getDeviceInfo((err, data) => {
  if (err) {
    console.error('Failed to get device info:', err);
    return;
  }
  if (data.osFullName.includes('HarmonyOS')) {
    console.log('This is a HarmonyOS device.');
  } else {
    console.log('This is not a HarmonyOS device.');
  }
});

通过检查osFullName是否包含HarmonyOS来判断是否为鸿蒙系统。

回到顶部