HarmonyOS鸿蒙Next中webview无法使用定位功能提示Only secure origins are allowed

HarmonyOS鸿蒙Next中webview无法使用定位功能提示Only secure origins are allowed 尝试设置

web_webview.GeolocationPermissions.allowGeolocation('http://');

也无法使用http 站点请求定位服务

示例代码通过 华为开发者联盟

请求站点url 为 http://xxxxxxx:xx

import web_webview from '@ohos.web.webview';
import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
import geoLocationManager from '@ohos.geoLocationManager';
import common from '@ohos.app.ability.common';

@Entry
@Component
struct Index {
  webviewController: web_webview.WebviewController = new web_webview.WebviewController();
 
  aboutToAppear() {
    // 配置web开启调试模式
    web_webview.WebviewController.setWebDebuggingAccess(true);
    web_webview.GeolocationPermissions.allowGeolocation('http://');

    let context = getContext(this) as common.UIAbilityContext;
    let atManager = abilityAccessCtrl.createAtManager();

    try {
      atManager.requestPermissionsFromUser(context, ['ohos.permission.APPROXIMATELY_LOCATION', 'ohos.permission.LOCATION'], (err, data) => {
        let requestInfo = {
          'priority': geoLocationManager.LocationRequestPriority.ACCURACY,
          'timeInterval': 0,
          'distanceInterval': 0,
          'maxAccuracy': 0
        };
        try {
          geoLocationManager.on('locationChange', requestInfo, (location: geoLocationManager.Location) => {
          if (location) {
            console.log('locationChanger: location=' + JSON.stringify(location));
          }
          });
        } catch (err) {
          console.error("errCode:" + err.code + ", errMessage:" + err.message);
        }
      })
    } catch (err) {
      console.error("err:", err);
    }

  }

  build() {
    Column() {
      Web({ src: 'http://192.168.50.34:4200', controller: this.webviewController })
        .fileAccess(true)
        .mixedMode(MixedMode.All)
        .domStorageAccess(true)
        .javaScriptAccess(true)
        .width('100%')
        .geolocationAccess(true)
        .onGeolocationShow((event) => { // 地理位置权限申请通知
          AlertDialog.show({
            title: '位置权限请求',
            message: '是否允许获取位置信息',
            primaryButton: {
              value: 'cancel',
              action: () => {
                if (event) {
                  event.geolocation.invoke(event.origin, false, false); // 不允许此站点地理位置权限请求
                }
              }
            },
            secondaryButton: {
              value: 'ok',
              action: () => {
                if (event) {
                  event.geolocation.invoke(event.origin, true, false); // 允许此站点地理位置权限请求
                }
              }
            },
            cancel: () => {
              if (event) {
                event.geolocation.invoke(event.origin, false, false); // 不允许此站点地理位置权限请求
              }
            }
          })
        })
    }.height('100%').width('100%').backgroundColor('#010021')

  }
}
3 回复

1、应用web_webview.GeolocationPermissions.allowGeolocation的入参有问题

2、h5访问地理位置 需要https来源的网页

更多关于HarmonyOS鸿蒙Next中webview无法使用定位功能提示Only secure origins are allowed的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


开发者您好,该问题已反馈先关人员,请耐心等待。

在HarmonyOS鸿蒙Next中,WebView无法使用定位功能并提示“Only secure origins are allowed”,这是因为现代浏览器和WebView组件要求定位功能必须通过HTTPS协议访问,以确保数据传输的安全性。要解决此问题,您需要确保WebView加载的网页使用HTTPS协议,而不是HTTP。如果您的网页尚未支持HTTPS,建议申请SSL证书并配置服务器以启用HTTPS。此外,您还可以在开发阶段通过本地调试工具(如Chrome DevTools)模拟HTTPS环境进行测试。

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!