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