• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Requesting Permissions
2
3
4If an application needs to obtain user privacy information or use system capabilities, for example, obtaining location information or using the camera to take photos or record videos, it must request the respective permission from users.
5
6
7During application development, you must declare the required permission in the **config.json** file and call **requestPermissionsFromUser** to request the permission from users in the form of a dialog box.
8
9
10To declare a permission in **config.json**, add **reqPermissions** under **module** and list the permission.
11
12
13For example, to declare the permission to access the calendar, request the **ohos.permission.READ_CALENDAR** permission. For details, see [Permission Application Guide](../security/accesstoken-guidelines.md#declaring-permissions-in-the-configuration-file).
14
15
16The sample code in the **config.json** file is as follows:
17
18```json
19{
20  "module": {
21    // ...
22    "reqPermissions": [
23      {
24        "name": "ohos.permission.READ_CALENDAR"
25        // ...
26      }
27    ]
28  }
29}
30```
31
32
33Request the permission from uses in the form of a dialog box:
34
35```ts
36import featureAbility from '@ohos.ability.featureAbility';
37
38let context = featureAbility.getContext();
39let permissions: Array<string> = ['ohos.permission.READ_CALENDAR']
40context.requestPermissionsFromUser(permissions, 1).then((data) => {
41    console.info("Succeed to request permission from user with data: " + JSON.stringify(data))
42}).catch((error) => {
43    console.info("Failed to request permission from user with error: " + JSON.stringify(error))
44})
45```
46