1# @ohos.application.uriPermissionManager (URI Permission Management) (System API)
2
3The **uriPermissionManager** module provides APIs for granting permissions on a file or revoking the granted permission from an application. The file is identified by a uniform resource identifier (URI).
4
5> **NOTE**
6>
7> - The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8> - The APIs of this module are system APIs and cannot be called by third-party applications.
9
10
11## Modules to Import
12
13
14```ts
15import { uriPermissionManager } from '@kit.AbilityKit';
16```
17
18
19## uriPermissionManager.grantUriPermission
20
21grantUriPermission(uri: string, flag: wantConstant.Flags, targetBundleName: string, callback: AsyncCallback<number>): void
22
23Grants the URI permission to an application. The permission will be revoked once the application exits. This API uses an asynchronous callback to return the result.
24
25An application can grant its own URIs to another application. If it has the ohos.permission.PROXY_AUTHORIZATION_URI permission, it can also grant the any accessible URI of another application.
26**System API**: This is a system API and cannot be called by third-party applications.
27
28**System capability**: SystemCapability.Ability.AbilityRuntime.Core
29
30**Required permissions**: ohos.permission.PROXY_AUTHORIZATION_URI
31
32**Parameters**
33
34| Name| Type| Mandatory| Description|
35| -------- | -------- | -------- | -------- |
36| uri | string | Yes| URI of the file, for example, **fileshare:///com.samples.filesharetest.FileShare/person/10**.|
37| flag | [wantConstant.Flags](js-apis-app-ability-wantConstant.md#flags) | Yes| Read or write permission on the file to grant.|
38| targetBundleName | string | Yes| Bundle name of the application, to which the permission is granted.|
39| callback | AsyncCallback<number> | Yes| Callback used to return the result. If the operation is successful, **0** is returned; otherwise, **-1** is returned.|
40
41**Error codes**
42
43  For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
44
45| ID| Error Message|
46| ------- | -------------------------------- |
47| 201 | Permission denied. |
48| 202 | Not System App. Interface caller is not a system app. |
49| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.|
50| 16000050 | Internal error. |
51| 16000058 | Invalid URI flag. |
52| 16000059 | Invalid URI type. |
53| 16000060 | A sandbox application cannot grant URI permission. |
54
55
56**Example**
57
58  ```ts
59  import { uriPermissionManager, wantConstant } from '@kit.AbilityKit';
60  import { fileIo, fileUri } from '@kit.CoreFileKit';
61
62  let targetBundleName = 'com.example.test_case1'
63  let path = "file://com.example.test_case1/data/storage/el2/base/haps/entry_test/files/newDir";
64  fileIo.mkdir(path, (err) => {
65    if (err) {
66      console.log("mkdir error" + err.message);
67    } else {
68      console.log("mkdir succeed");
69    }
70  });
71  let uri = fileUri.getUriFromPath(path);
72  uriPermissionManager.grantUriPermission(uri, wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION, targetBundleName, (result) => {
73    console.log("result.code = " + result.code);
74  });
75  ```
76
77
78## uriPermissionManager.grantUriPermission
79
80grantUriPermission(uri: string, flag: wantConstant.Flags, targetBundleName: string): Promise<number>
81
82Grants the URI permission to an application. The permission will be revoked once the application exits. This API uses a promise to return the result.
83
84An application can grant its own URIs to another application. If it has the ohos.permission.PROXY_AUTHORIZATION_URI permission, it can also grant the any accessible URI of another application.
85**System API**: This is a system API and cannot be called by third-party applications.
86
87**System capability**: SystemCapability.Ability.AbilityRuntime.Core
88
89**Required permissions**: ohos.permission.PROXY_AUTHORIZATION_URI
90
91**Parameters**
92
93| Name| Type| Mandatory| Description|
94| -------- | -------- | -------- | -------- |
95| uri | string | Yes| URI of the file, for example, **fileshare:///com.samples.filesharetest.FileShare/person/10**.|
96| flag | [wantConstant.Flags](js-apis-app-ability-wantConstant.md#flags) | Yes| Read or write permission on the file to grant.|
97| targetBundleName | string | Yes| Bundle name of the application, to which the permission is granted.|
98
99**Return value**
100
101| Type| Description|
102| -------- | -------- |
103| Promise<number> | Promise used to return the result. If the operation is successful, **0** is returned; otherwise, **-1** is returned.|
104
105**Error codes**
106
107  For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
108
109| ID| Error Message|
110| ------- | -------------------------------- |
111| 201 | Permission denied. |
112| 202 | Not System App. Interface caller is not a system app. |
113| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.|
114| 16000050 | Internal error. |
115| 16000058 | Invalid URI flag. |
116| 16000059 | Invalid URI type. |
117| 16000060 | A sandbox application cannot grant URI permission. |
118
119**Example**
120
121  ```ts
122  import { uriPermissionManager, wantConstant } from '@kit.AbilityKit';
123  import { fileIo, fileUri } from '@kit.CoreFileKit';
124  import { BusinessError } from '@kit.BasicServicesKit';
125
126  let targetBundleName = 'com.example.test_case1'
127  let path = "file://com.example.test_case1/data/storage/el2/base/haps/entry_test/files/newDir";
128
129  fileIo.mkdir(path, (err) => {
130    if (err) {
131      console.log("mkdir error" + err.message);
132    } else {
133      console.log("mkdir succeed");
134    }
135  });
136  let uri = fileUri.getUriFromPath(path);
137  uriPermissionManager.grantUriPermission(uri, wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION, targetBundleName)
138    .then((data) => {
139      console.log('Verification succeeded.' + data);
140    }).catch((error: BusinessError) => {
141    console.log('Verification failed.');
142  });
143  ```
144
145## uriPermissionManager.revokeUriPermission
146
147revokeUriPermission(uri: string, targetBundleName: string, callback: AsyncCallback<number>): void
148
149Revokes the URI permission from an application. This API uses an asynchronous callback to return the result.
150
151This API can be used to revoke the URI permission of another application obtained by this application or URI permission granted by this application.
152**System API**: This is a system API and cannot be called by third-party applications.
153
154**System capability**: SystemCapability.Ability.AbilityRuntime.Core
155
156**Parameters**
157
158| Name| Type| Mandatory| Description|
159| -------- | -------- | -------- | -------- |
160| uri | string | Yes| URI of the file, for example, **fileshare:///com.samples.filesharetest.FileShare/person/10**.|
161| targetBundleName | string | Yes| Bundle name of the application, from which the permission is revoked.|
162| callback | AsyncCallback<number> | Yes| Callback used to return the result. If the operation is successful, **0** is returned; otherwise, **-1** is returned.|
163
164**Error codes**
165
166  For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
167
168| ID| Error Message|
169| ------- | -------------------------------- |
170| 202 | Not System App. Interface caller is not a system app. |
171| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.|
172| 16000050 | Internal error. |
173| 16000059 | Invalid URI type. |
174
175**Example**
176
177  ```ts
178  import { uriPermissionManager } from '@kit.AbilityKit';
179
180  let targetBundleName = 'com.example.test_case2';
181  let uri = "file://com.example.test_case1/data/storage/el2/base/haps/entry_test/files/newDir";
182
183  uriPermissionManager.revokeUriPermission(uri, targetBundleName, (result) => {
184    console.log("result.code = " + result.code);
185  });
186  ```
187
188
189## uriPermissionManager.revokeUriPermission
190
191revokeUriPermission(uri: string, targetBundleName: string): Promise<number>
192
193Revokes the URI permission from an application. This API uses a promise to return the result.
194
195
196This API can be used to revoke the URI permission of another application obtained by this application or URI permission granted by this application.
197**System API**: This is a system API and cannot be called by third-party applications.
198
199**System capability**: SystemCapability.Ability.AbilityRuntime.Core
200
201**Parameters**
202
203| Name| Type| Mandatory| Description|
204| -------- | -------- | -------- | -------- |
205| uri | string | Yes| URI of the file, for example, **fileshare:///com.samples.filesharetest.FileShare/person/10**.|
206| targetBundleName | string | Yes| Bundle name of the application, from which the permission is revoked.|
207
208**Return value**
209
210| Type| Description|
211| -------- | -------- |
212| Promise<number> | Promise used to return the result. If the operation is successful, **0** is returned; otherwise, **-1** is returned.|
213
214**Error codes**
215
216  For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
217
218| ID| Error Message|
219| ------- | -------------------------------- |
220| 202 | Not System App. Interface caller is not a system app. |
221| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.|
222| 16000050 | Internal error. |
223| 16000059 | Invalid URI type. |
224
225
226**Example**
227
228  ```ts
229  import { uriPermissionManager } from '@kit.AbilityKit';
230  import { BusinessError } from '@kit.BasicServicesKit';
231
232  let targetBundleName = 'com.example.test_case2';
233  let uri = "file://com.example.test_case1/data/storage/el2/base/haps/entry_test/files/newDir";
234
235  uriPermissionManager.revokeUriPermission(uri, targetBundleName)
236    .then((data) => {
237      console.log('Verification succeeded.' + data);
238    }).catch((error: BusinessError) => {
239    console.log('Verification failed.');
240  });
241  ```
242