• 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 the_type_of_resource_for_which_permission_is_requested]
17import { webview } from '@kit.ArkWeb';
18import { abilityAccessCtrl, PermissionRequestResult } from '@kit.AbilityKit';
19import { BusinessError } from '@kit.BasicServicesKit';
20
21@Entry
22@Component
23struct WebComponent {
24  controller: webview.WebviewController = new webview.WebviewController();
25  aboutToAppear() {
26    // 配置Web开启调试模式
27    webview.WebviewController.setWebDebuggingAccess(true);
28    // 访问控制管理, 获取访问控制模块对象。
29    let atManager = abilityAccessCtrl.createAtManager();
30    try {
31      atManager.requestPermissionsFromUser(getContext(this)
32        , ['ohos.permission.ACCELEROMETER', 'ohos.permission.GYROSCOPE']
33        , (err: BusinessError, data: PermissionRequestResult) => {
34          console.info('data:' + JSON.stringify(data));
35          console.info('data permissions:' + data.permissions);
36          console.info('data authResults:' + data.authResults);
37        })
38    } catch (error) {
39      console.error(
40        `ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
41    }
42  }
43
44  build() {
45    Column() {
46      Web({ src: $rawfile('index.html'), controller: this.controller })
47        .onPermissionRequest((event) => {
48          if (event) {
49            AlertDialog.show({
50              title: 'title',
51              message: 'text',
52              primaryButton: {
53                value: 'deny',
54                action: () => {
55                  event.request.deny();
56                }
57              },
58              secondaryButton: {
59                value: 'onConfirm',
60                action: () => {
61                  event.request.grant(event.request.getAccessibleResource());
62                }
63              },
64              autoCancel: false,
65              cancel: () => {
66                event.request.deny();
67              }
68            })
69          }
70        })
71    }
72  }
73}
74// [End the_type_of_resource_for_which_permission_is_requested]
75