目录

如何通过User-Agent识别系统类型和设备类型

问题现象

在开发中,需要根据系统类型和设备类型进行适配,通常分为以下场景:

场景一:如何判断当前的操作系统是HarmonyOS系统?

场景二:如何判断当前的设备类型是手机、平板、PC?

背景知识

User-Agent开发指导:User-Agent(简称UA)是一个特殊的字符串,包含设备类型、操作系统及版本等关键信息。在Web开发中,这个字符串使服务器能够识别请求的来源设备及其特性,从而根据这些信息提供定制化的内容和服务。如果页面无法正确识别UA,可能会导致多种异常情况。

解决方案

场景一:

场景二:

通过User-Agent中的{DeviceType}字段识别设备类型,Phone代表手机、Tablet代表平板、PC代表2in1设备。

完整实现代码如下:在onControllerAttached通过setCustomUserAgent设置User-Agent。

import { webview } from '@kit.ArkWeb';

@Entry
@Component
struct Index {
  controller: webview.WebviewController = new webview.WebviewController();
  private userAgent: string = '';

  build() {
    Column() {
      Web({ src: $rawfile('judgeUA.html'), controller: this.controller })
        .onControllerAttached(() => {
          this.userAgent = this.controller.getUserAgent() + ' test';
          this.controller.setCustomUserAgent(this.userAgent);
        })
        .geolocationAccess(false)
        .fileAccess(false)
        .domStorageAccess(true)
        .width('100%');
    }
    .width('100%')
    .height('100%');
  }
}

HTML示例代码如下:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>设备检测</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            margin-top: 50px;
        }
        #result {
            margin-top: 20px;
            font-size: 24px;
            color: #333;
        }
    </style>
</head>
<body>
<h1>判断设备类型与系统类型</h1>
<div id="result"></div>
<script>
    const isPhone = () => /Phone/i.test(navigator.userAgent);
    const isHarmonyOS = () => /OpenHarmony/i.test(navigator.userAgent);
    const matches = navigator.userAgent.match(/OpenHarmony (\d+\.?\d*)/);
    const isHarmonyOS5Plus = matches?.length && Number(matches[1]) >= 5;
    const isTablet = () => /Tablet/i.test(navigator.userAgent);
    const is2in1 = () => /PC/i.test(navigator.userAgent);
    const resultElement = document.getElementById('result');
    const additionalInfo = [];
    if (isHarmonyOS()) {
        additionalInfo.push("运行在HarmonyOS系统上");
        if (isHarmonyOS5Plus) {
            additionalInfo.push("HarmonyOS版本 >= 5");
        }
    } else {
        additionalInfo.push("没有运行在HarmonyOS系统上");
    }
    if (isTablet()) {
        resultElement.innerHTML = "当前设备:平板";
    }
    if (isPhone()) {
        resultElement.innerHTML = "当前设备:手机";
    }
    if (is2in1()) {
        resultElement.innerHTML = "当前设备:PC";
    }
    if (additionalInfo.length > 0) {
        resultElement.innerHTML += "<br><br>判断是否在HarmonyOS系统:<br>" + additionalInfo.join("<br>");
    }
</script>
</body>
</html>