• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16// [Start a_pop_up_window_is_used_to_notify_the_app_side_location_permission_request_message]
17import { webview } from '@kit.ArkWeb';
18import { BusinessError } from '@kit.BasicServicesKit';
19import { abilityAccessCtrl, common } from '@kit.AbilityKit';
20
21let context = getContext(this) as common.UIAbilityContext;
22let atManager = abilityAccessCtrl.createAtManager();
23
24// 向用户请求位置权限设置。
25atManager.requestPermissionsFromUser(context, ['ohos.permission.APPROXIMATELY_LOCATION']).then((data) => {
26  console.info('data:' + JSON.stringify(data));
27  console.info('data permissions:' + data.permissions);
28  console.info('data authResults:' + data.authResults);
29}).catch((error: BusinessError) => {
30  console.error(`Failed to request permissions from user. Code is ${error.code}, message is ${error.message}`);
31})
32
33@Entry
34@Component
35struct WebComponent {
36  controller: webview.WebviewController = new webview.WebviewController();
37
38  build() {
39    Column() {
40      Web({ src: $rawfile('getLocation.html'), controller: this.controller })
41        .geolocationAccess(true)
42        .onGeolocationShow((event) => { // 地理位置权限申请通知
43          AlertDialog.show({
44            title: 'Location permission requests',
45            message: 'Whether to allow access to location information',
46            primaryButton: {
47              value: 'cancel',
48              action: () => {
49                if (event) {
50                  event.geolocation.invoke(event.origin, false, false); // 不允许此站点地理位置权限请求
51                }
52              }
53            },
54            secondaryButton: {
55              value: 'ok',
56              action: () => {
57                if (event) {
58                  event.geolocation.invoke(event.origin, true, false); // 允许此站点地理位置权限请求
59                }
60              }
61            },
62            cancel: () => {
63              if (event) {
64                event.geolocation.invoke(event.origin, false, false); // 不允许此站点地理位置权限请求
65              }
66            }
67          })
68        })
69    }
70  }
71}
72// [End a_pop_up_window_is_used_to_notify_the_app_side_location_permission_request_message]