智能客服
你问我答,随时在线为你解决问题
UserAgent(简称UA)是一个特殊的字符串,它包含了设备类型、操作系统及版本等关键信息。如果页面无法正确识别UA,可能会导致一系列异常情况,例如页面布局错误、渲染问题以及逻辑错误等。
- 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} {扩展区}
- 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扩展,例如加入应用相关信息标识。 |
在下面的示例中,通过getUserAgent()接口获取当前默认用户代理,支持开发者基于默认的UserAgent去定制UserAgent。
- // xxx.ets
- import { webview } from '@kit.ArkWeb';
- import { BusinessError } from '@kit.BasicServicesKit';
-
- @Entry
- @Component
- struct WebComponent {
- controller: webview.WebviewController = new webview.WebviewController();
-
- build() {
- Column() {
- Button('getUserAgent')
- .onClick(() => {
- try {
- let userAgent = this.controller.getUserAgent();
- console.log("userAgent: " + userAgent);
- } catch (error) {
- console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
- }
- })
- Web({ src: 'www.example.com', controller: this.controller })
- }
- }
- }
以下示例通过setCustomUserAgent()接口设置自定义用户代理,会覆盖系统的用户代理。建议将扩展字段追加在默认用户代理的末尾。
当Web组件src设置了url时,建议在onControllerAttached回调事件中设置UserAgent,设置方式请参考示例。如果未在onControllerAttached回调事件中设置UserAgent,再调用setCustomUserAgent方法时,可能会出现加载的页面与实际设置UserAgent不符的异常现象。
当Web组件src设置为空字符串时,建议先调用setCustomUserAgent方法设置UserAgent,再通过loadUrl加载具体页面。
- // xxx.ets
- import { webview } from '@kit.ArkWeb';
- import { BusinessError } from '@kit.BasicServicesKit';
-
- @Entry
- @Component
- struct WebComponent {
- controller: webview.WebviewController = new webview.WebviewController();
- // 三方应用相关信息标识
- @State customUserAgent: string = ' DemoApp';
-
- build() {
- Column() {
- Web({ src: 'www.example.com', controller: this.controller })
- .onControllerAttached(() => {
- console.log("onControllerAttached");
- try {
- let userAgent = this.controller.getUserAgent() + this.customUserAgent;
- this.controller.setCustomUserAgent(userAgent);
- } catch (error) {
- console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
- }
- })
- }
- }
- }
在下面的示例中,通过getCustomUserAgent()接口获取自定义用户代理。
- // xxx.ets
- import { webview } from '@kit.ArkWeb';
- import { BusinessError } from '@kit.BasicServicesKit';
-
- @Entry
- @Component
- struct WebComponent {
- controller: webview.WebviewController = new webview.WebviewController();
- @State userAgent: string = '';
-
- build() {
- Column() {
- Button('getCustomUserAgent')
- .onClick(() => {
- try {
- this.userAgent = this.controller.getCustomUserAgent();
- console.log("userAgent: " + this.userAgent);
- } catch (error) {
- console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
- }
- })
- Web({ src: 'www.example.com', controller: this.controller })
- }
- }
- }
HarmonyOS设备的识别主要通过UserAgent中的系统、系统版本和设备类型三个维度来判断。建议同时检查系统、系统版本和设备类型,以确保更准确的设备识别。
通过UserAgent中的{OSName}字段识别HarmonyOS系统。
- const isHarmonyOS = () => /OpenHarmony/i.test(navigator.userAgent);
通过UserAgent中的{OSName}和{OSVersion}字段识别HarmonyOS系统。格式为:OpenHarmony + 版本号。
- const matches = navigator.userAgent.match(/OpenHarmony (\d+\.?\d*)/);
- matches?.length && Number(matches[1]) >= 5;
通过DeviceType字段来识别不同设备类型。
- // 检测是否为手机设备
- const isPhone = () => /Phone/i.test(navigator.userAgent);
-
- // 检测是否为平板设备
- const isTablet = () => /Tablet/i.test(navigator.userAgent);
-
- // 检测是否为2in1设备
- const is2in1 = () => /PC/i.test(navigator.userAgent);
在Windows/Mac/Linux等操作系统中,可以通过Chrome/Edge/Firefox等浏览器DevTools提供的UserAgent复写能力,模拟HarmonyOS UserAgent。
智能客服
你问我答,随时在线为你解决问题
合作咨询
我们的专家服务团队将竭诚为您提供专业的合作咨询服务
解决方案
精准高效的一站式服务支持,助力开发者商业成功