• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# PermissionRequestResult
2
3The **PermissionRequestResult** module defines the result of a permission request. The result is returned when [requestPermissionsFromUser](js-apis-abilityAccessCtrl.md#requestpermissionsfromuser9) is called to request permissions.
4
5> **NOTE**
6>
7> - The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8> - The APIs of this module can be used only in the stage model.
9
10## Attributes
11
12**System capability**: SystemCapability.Security.AccessToken
13
14| Name| Type| Readable| Writable| Description|
15| -------- | -------- | -------- | -------- | -------- |
16| permissions | Array<string> | Yes| No| Permissions requested.|
17| authResults | Array&lt;number&gt; | Yes| No| Result of the permission request.<br>- **-1**: The permission is not authorized and must be set in **Settings** without displaying a dialog box.<br>- **0**: The permission is authorized.<br>- **2**: The permission is not authorized due to an invalid request. The possible causes are as follows:<br>  - The permission is not declared in the configuration file.<br>  - The permission name is invalid.<br>  - Special conditions for applying for the permission are not satisfied. See [ohos.permission.LOCATION](../../security/permission-list.md#ohospermissionlocation) and [ohos.permission.APPROXIMATELY_LOCATION](../../security/permission-list.md#ohospermissionapproximately_location).|
18
19## Usage
20
21The permission request result is obtained through an **atManager** instance.
22
23**Example**
24
25The ArkTS syntax does not support direct use of **globalThis**. A singleton map is required to enable the use of **globalThis**. You need to perform the following operations:
26
27a. Import the created singleton object **GlobalThis** to **EntryAbility.ets**.
28  ```typescript
29    import {GlobalThis} from '../utils/globalThis'; // Set it based on the path of globalThis.ets.
30  ```
31b. Add the following to **onCreate**:
32  ```typescript
33    GlobalThis.getInstance().setContext('context', this.context);
34  ```
35
36> **NOTE**
37>
38> An alert will be generated when a **.ets** file is imported to a TS file. To prevent the alert, you need to change the file name extension of **EntryAbility.ts** to **EntryAbility.ets** and modify the file name extension in **module.json5**.
39
40The sample code of **globalThis.ets** is as follows:
41```typescript
42import common from '@ohos.app.ability.common';
43
44// Construct a singleton object.
45export class GlobalThis {
46  private constructor() {}
47  private static instance: GlobalThis;
48  private _uiContexts = new Map<string, common.UIAbilityContext>();
49
50  public static getInstance(): GlobalThis {
51    if (!GlobalThis.instance) {
52      GlobalThis.instance = new GlobalThis();
53    }
54    return GlobalThis.instance;
55  }
56
57  getContext(key: string): common.UIAbilityContext | undefined {
58    return this._uiContexts.get(key);
59  }
60
61  setContext(key: string, value: common.UIAbilityContext): void {
62    this._uiContexts.set(key, value);
63  }
64
65  // Set other content in the same way.
66}
67```
68
69```ts
70import { BusinessError } from '@ohos.base';
71import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
72import common from '@ohos.app.ability.common';
73import { GlobalThis } from '../utils/globalThis';
74
75let atManager = abilityAccessCtrl.createAtManager();
76try {
77  let context: common.UIAbilityContext = GlobalThis.getInstance().getContext('context');
78  atManager.requestPermissionsFromUser(context, ["ohos.permission.CAMERA"]).then((data) => {
79      console.info("data:" + JSON.stringify(data));
80      console.info("data permissions:" + data.permissions);
81      console.info("data authResults:" + data.authResults);
82  }).catch((err: BusinessError) => {
83      console.info("data:" + JSON.stringify(err));
84  })
85} catch(err) {
86  console.log(`catch err->${JSON.stringify(err)}`);
87}
88```
89