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