文档管理中心
您当前浏览的HarmonyOS 5.0.0(API 12)文档归档不再维护,推荐您使用最新版本。详细请参考文档维护策略变更
指南应用框架ArkWeb(方舟Web)设置基本属性和事件UserAgent开发指导

UserAgent开发指导

UserAgent(简称UA)是一个特殊的字符串,它包含了设备类型、操作系统及版本等关键信息。如果页面无法正确识别UA,可能会导致一系列异常情况,例如页面布局错误、渲染问题以及逻辑错误等。

默认UserAgent结构

  • 默认UserAgent定义
    收起
    自动换行
    深色代码主题
    复制
    1. Mozilla/5.0 ({DeviceType}; {OSName} {OSVersion}; {DistributionOSName} {DistributionOSVersion}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/{ChromeCompatibleVersion}.0.0.0 Safari/537.36 ArkWeb/{ArkWeb VersionCode} {DeviceCompat} {扩展区}
  • 举例说明
    收起
    自动换行
    深色代码主题
    复制
    1. Mozilla/5.0 (Phone; OpenHarmony 5.0; HarmonyOS 5.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 ArkWeb/4.1.6.1 Mobile
  • 字段说明
    展开

    字段

    含义

    DeviceType

    当前的设备类型。

    取值范围:

    - Phone:手机设备

    - Tablet:平板设备

    - PC:2in1设备

    OSName

    基础操作系统名称

    默认取值:OpenHarmony

    OSVersion

    基础操作系统版本,两位数字,M.S。

    通过系统参数const.ohos.fullname解析版本号得到,取版本号部分M.S前两位。

    默认取值:例如5.0

    DistributionOSName

    发行版操作系统名称

    默认取值:HarmonyOS

    DistributionOSVersion

    发行版操作系统版本,两位数字,M.S。

    通过系统参数const.product.os.dist.apiname解析版本号得到,如果该参数值为空,通过系统参数const.product.os.dist.version解析版本号得到,取M.S前两位。

    默认取值:例如5.0

    ChromeCompatibleVersion

    兼容Chrome主版本的版本号,从114版本开始演进。

    默认取值:114

    ArkWeb

    HarmonyOS版本Web内核名称。

    默认取值:ArkWeb

    ArkWeb VersionCode

    ArkWeb版本号,格式a.b.c.d。

    默认取值:例如4.1.6.1

    DeviceCompat

    前向兼容字段。

    默认取值:Mobile

    扩展区

    三方应用可以扩展的字段。

    三方应用使用ArkWeb组件时,可以做UA扩展,例如加入应用相关信息标识。

说明
  • 当前通过UserAgent中是否含有"Mobile"字段来判断是否开启前端HTML页面中meta标签的viewport属性。当UserAgent中不含有"Mobile"字段时,meta标签中viewport属性默认关闭,此时可通过显性设置metaViewport属性为true来覆盖关闭状态。
  • 建议通过OpenHarmony关键字识别是否是HarmonyOS设备,同时可以通过DeviceType识别设备类型用于不同设备上的页面显示(ArkWeb关键字表示设备使用的web内核,OpenHarmony关键字表示设备使用的操作系统,因此推荐通过OpenHarmony关键字识别是否是HarmonyOS设备)。

自定义UserAgent结构

在下面的示例中,通过getUserAgent()接口获取当前默认用户代理,支持开发者基于默认的UserAgent去定制UserAgent。

收起
自动换行
深色代码主题
复制
  1. // xxx.ets
  2. import { webview } from '@kit.ArkWeb';
  3. import { BusinessError } from '@kit.BasicServicesKit';
  4. @Entry
  5. @Component
  6. struct WebComponent {
  7. controller: webview.WebviewController = new webview.WebviewController();
  8. build() {
  9. Column() {
  10. Button('getUserAgent')
  11. .onClick(() => {
  12. try {
  13. let userAgent = this.controller.getUserAgent();
  14. console.log("userAgent: " + userAgent);
  15. } catch (error) {
  16. console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
  17. }
  18. })
  19. Web({ src: 'www.example.com', controller: this.controller })
  20. }
  21. }
  22. }

以下示例通过setCustomUserAgent()接口设置自定义用户代理,会覆盖系统的用户代理。建议将扩展字段追加在默认用户代理的末尾。

当Web组件src设置了url时,建议在onControllerAttached回调事件中设置UserAgent,设置方式请参考示例。如果未在onControllerAttached回调事件中设置UserAgent,再调用setCustomUserAgent方法时,可能会出现加载的页面与实际设置UserAgent不符的异常现象。

当Web组件src设置为空字符串时,建议先调用setCustomUserAgent方法设置UserAgent,再通过loadUrl加载具体页面。

收起
自动换行
深色代码主题
复制
  1. // xxx.ets
  2. import { webview } from '@kit.ArkWeb';
  3. import { BusinessError } from '@kit.BasicServicesKit';
  4. @Entry
  5. @Component
  6. struct WebComponent {
  7. controller: webview.WebviewController = new webview.WebviewController();
  8. // 三方应用相关信息标识
  9. @State customUserAgent: string = ' DemoApp';
  10. build() {
  11. Column() {
  12. Web({ src: 'www.example.com', controller: this.controller })
  13. .onControllerAttached(() => {
  14. console.log("onControllerAttached");
  15. try {
  16. let userAgent = this.controller.getUserAgent() + this.customUserAgent;
  17. this.controller.setCustomUserAgent(userAgent);
  18. } catch (error) {
  19. console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
  20. }
  21. })
  22. }
  23. }
  24. }

在下面的示例中,通过getCustomUserAgent()接口获取自定义用户代理。

收起
自动换行
深色代码主题
复制
  1. // xxx.ets
  2. import { webview } from '@kit.ArkWeb';
  3. import { BusinessError } from '@kit.BasicServicesKit';
  4. @Entry
  5. @Component
  6. struct WebComponent {
  7. controller: webview.WebviewController = new webview.WebviewController();
  8. @State userAgent: string = '';
  9. build() {
  10. Column() {
  11. Button('getCustomUserAgent')
  12. .onClick(() => {
  13. try {
  14. this.userAgent = this.controller.getCustomUserAgent();
  15. console.log("userAgent: " + this.userAgent);
  16. } catch (error) {
  17. console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
  18. }
  19. })
  20. Web({ src: 'www.example.com', controller: this.controller })
  21. }
  22. }
  23. }

常见问题

如何通过UserAgent来识别HarmonyOS操作系统中不同设备

HarmonyOS设备的识别主要通过UserAgent中的系统、系统版本和设备类型三个维度来判断。建议同时检查系统、系统版本和设备类型,以确保更准确的设备识别。

  1. 系统识别

    通过UserAgent中的{OSName}字段识别HarmonyOS系统。

    收起
    自动换行
    深色代码主题
    复制
    1. const isHarmonyOS = () => /OpenHarmony/i.test(navigator.userAgent);
  2. 系统版本识别

    通过UserAgent中的{OSName}和{OSVersion}字段识别HarmonyOS系统。格式为:OpenHarmony + 版本号。

    收起
    自动换行
    深色代码主题
    复制
    1. const matches = navigator.userAgent.match(/OpenHarmony (\d+\.?\d*)/);
    2. matches?.length && Number(matches[1]) >= 5;
  3. 设备类型识别

    通过DeviceType字段来识别不同设备类型。

    收起
    自动换行
    深色代码主题
    复制
    1. // 检测是否为手机设备
    2. const isPhone = () => /Phone/i.test(navigator.userAgent);
    3. // 检测是否为平板设备
    4. const isTablet = () => /Tablet/i.test(navigator.userAgent);
    5. // 检测是否为2in1设备
    6. const is2in1 = () => /PC/i.test(navigator.userAgent);

如何模拟HarmonyOS操作系统的UserAgent进行前端调试

在Windows/Mac/Linux等操作系统中,可以通过Chrome/Edge/Firefox等浏览器DevTools提供的UserAgent复写能力,模拟HarmonyOS UserAgent。

示例代码

在 指南 中进行搜索
请输入您想要搜索的关键词