1# @ohos.application.uriPermissionManager (URI Permission Management) (System API) 2 3The **uriPermissionManager** module provides capabilities for granting the permission on a file to another application and revoking the granted permissions. 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. If the call is successful, the application obtains the permission to access the file specified by the URI. Once the application exits, the permission will be automatically revoked. For details about how to access the file based on the URI, see [Sharing an Application File](../../file-management/share-app-file.md). This API uses an asynchronous callback to return the result. 24 25> **NOTE** 26> 27> If an application has the ohos.permission.PROXY_AUTHORIZATION_URI permission, it can grant the accessible URIs of another application. If the application does not have this permission, it can grant only its own URI permissions. 28 29**System API**: This is a system API. 30 31**System capability**: SystemCapability.Ability.AbilityRuntime.Core 32 33**Required permissions**: ohos.permission.PROXY_AUTHORIZATION_URI 34 35**Parameters** 36 37| Name| Type| Mandatory| Description| 38| -------- | -------- | -------- | -------- | 39| uri | string | Yes| URI of the file. The scheme has a fixed value of **file**. For details, see [FileUri](../apis-core-file-kit/js-apis-file-fileuri.md#constructor10).| 40| flag | [wantConstant.Flags](js-apis-app-ability-wantConstant.md#flags) | Yes| Read or write permission on the file to grant.| 41| targetBundleName | string | Yes| Bundle name of the target application.| 42| callback | AsyncCallback<number> | Yes| Callback used to return the result. If the operation is successful, **0** is returned; otherwise, **-1** is returned.| 43 44**Error codes** 45 46 For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 47 48| ID| Error Message| 49| ------- | -------------------------------- | 50| 201 | Permission denied. | 51| 202 | Not System App. Interface caller is not a system app. | 52| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.| 53| 16000050 | Internal error. | 54| 16000058 | Invalid URI flag. | 55| 16000059 | Invalid URI type. | 56| 16000060 | A sandbox application cannot grant URI permission. | 57 58 59**Example** 60 61 ```ts 62 import { uriPermissionManager, wantConstant } from '@kit.AbilityKit'; 63 import { fileIo, fileUri } from '@kit.CoreFileKit'; 64 65 let targetBundleName = 'com.example.test_case1' 66 let path = "file://com.example.test_case1/data/storage/el2/base/haps/entry_test/files/newDir"; 67 fileIo.mkdir(path, (err) => { 68 if (err) { 69 console.log("mkdir error" + err.message); 70 } else { 71 console.log("mkdir succeed"); 72 } 73 }); 74 let uri = fileUri.getUriFromPath(path); 75 uriPermissionManager.grantUriPermission(uri, wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION, targetBundleName, (error) => { 76 if (error && error.code !== 0) { 77 console.error("grantUriPermission failed, error.code = " + error.code); 78 return; 79 } 80 console.info("grantUriPermission success"); 81 }); 82 ``` 83 84 85## uriPermissionManager.grantUriPermission 86 87grantUriPermission(uri: string, flag: wantConstant.Flags, targetBundleName: string): Promise<number> 88 89Grants the URI permission to an application. If the call is successful, the application obtains the permission to access the file specified by the URI. Once the application exits, the permission will be automatically revoked. For details about how to access the file based on the URI, see [Sharing an Application File](../../file-management/share-app-file.md). This API uses a promise to return the result. 90 91> **NOTE** 92> 93> If an application has the ohos.permission.PROXY_AUTHORIZATION_URI permission, it can grant the accessible URIs of another application. If the application does not have this permission, it can grant only its own URI permissions. 94 95**System API**: This is a system API. 96 97**System capability**: SystemCapability.Ability.AbilityRuntime.Core 98 99**Required permissions**: ohos.permission.PROXY_AUTHORIZATION_URI 100 101**Parameters** 102 103| Name| Type| Mandatory| Description| 104| -------- | -------- | -------- | -------- | 105| uri | string | Yes| URI of the file. The scheme has a fixed value of **file**. For details, see [FileUri](../apis-core-file-kit/js-apis-file-fileuri.md#constructor10).| 106| flag | [wantConstant.Flags](js-apis-app-ability-wantConstant.md#flags) | Yes| Read or write permission on the file to grant.| 107| targetBundleName | string | Yes| Bundle name of the target application.| 108 109**Return value** 110 111| Type| Description| 112| -------- | -------- | 113| Promise<number> | Promise used to return the result. If the operation is successful, **0** is returned; otherwise, **-1** is returned.| 114 115**Error codes** 116 117 For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 118 119| ID| Error Message| 120| ------- | -------------------------------- | 121| 201 | Permission denied. | 122| 202 | Not System App. Interface caller is not a system app. | 123| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.| 124| 16000050 | Internal error. | 125| 16000058 | Invalid URI flag. | 126| 16000059 | Invalid URI type. | 127| 16000060 | A sandbox application cannot grant URI permission. | 128 129**Example** 130 131 ```ts 132 import { uriPermissionManager, wantConstant } from '@kit.AbilityKit'; 133 import { fileIo, fileUri } from '@kit.CoreFileKit'; 134 import { BusinessError } from '@kit.BasicServicesKit'; 135 136 let targetBundleName = 'com.example.test_case1' 137 let path = "file://com.example.test_case1/data/storage/el2/base/haps/entry_test/files/newDir"; 138 139 fileIo.mkdir(path, (err) => { 140 if (err) { 141 console.log("mkdir error" + err.message); 142 } else { 143 console.log("mkdir succeed"); 144 } 145 }); 146 let uri = fileUri.getUriFromPath(path); 147 uriPermissionManager.grantUriPermission(uri, wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION, targetBundleName) 148 .then((data) => { 149 console.log('Verification succeeded.' + data); 150 }).catch((error: BusinessError) => { 151 console.log('Verification failed.'); 152 }); 153 ``` 154 155## uriPermissionManager.grantUriPermission<sup>14+</sup> 156 157grantUriPermission(uri: string, flag: wantConstant.Flags, targetBundleName: string, appCloneIndex: number): Promise<void> 158 159Grants the URI permission to an application. If the call is successful, the application obtains the permission to access the file specified by the URI. Once the application exits, the permission will be automatically revoked. For details about how to access the file based on the URI, see [Sharing an Application File](../../file-management/share-app-file.md). This API uses a promise to return the result. 160 161> **NOTE** 162> 163>- If an application has the ohos.permission.PROXY_AUTHORIZATION_URI permission, it can grant the accessible URIs of another application. If the application does not have this permission, it can grant only its own URI permissions. 164>- This API can be used to grant URI access permission to a cloned application. You need to specify the application bundle name and index of the cloned application. 165 166**System API**: This is a system API. 167 168**System capability**: SystemCapability.Ability.AbilityRuntime.Core 169 170**Required permissions**: ohos.permission.PROXY_AUTHORIZATION_URI 171 172**Parameters** 173 174| Name| Type| Mandatory| Description| 175| -------- | -------- | -------- | -------- | 176| uri | string | Yes| URI of the file. The scheme has a fixed value of **file**. For details, see [FileUri](../apis-core-file-kit/js-apis-file-fileuri.md#constructor10).| 177| flag | [wantConstant.Flags](js-apis-app-ability-wantConstant.md#flags) | Yes| Read or write permission on the file to grant.| 178| targetBundleName | string | Yes| Bundle name of the target application.| 179| appCloneIndex | number | Yes| Index of the cloned application. The value range is [0, 1000]. The value **0** indicates the application itself.| 180 181**Return value** 182 183| Type| Description| 184| -------- | -------- | 185| Promise<void> | Promise that returns no value.| 186 187**Error codes** 188 189 For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 190 191| ID| Error Message| 192| ------- | -------------------------------- | 193| 201 | Permission denied. | 194| 202 | Not System App. Interface caller is not a system app. | 195| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.| 196| 16000050 | Internal error. | 197| 16000058 | Invalid URI flag. | 198| 16000059 | Invalid URI type. | 199| 16000060 | A sandbox application cannot grant URI permission. | 200| 16000081 | Get target application info failed. | 201 202**Example** 203 204 ```ts 205 import { AbilityConstant, UIAbility, Want, wantConstant, uriPermissionManager } from '@kit.AbilityKit'; 206 import { fileUri } from '@kit.CoreFileKit'; 207 import { BusinessError } from '@kit.BasicServicesKit'; 208 209 export default class EntryAbility extends UIAbility { 210 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 211 } 212 213 onForeground(): void { 214 let targetBundleName: string = 'com.example.demo1'; 215 let filePath: string = this.context.filesDir + "/test.txt"; 216 let uri: string = fileUri.getUriFromPath(filePath); 217 // grant uri permission to main application 218 try { 219 let appCloneIndex: number = 0; 220 uriPermissionManager.grantUriPermission(uri, wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION, targetBundleName, 221 appCloneIndex) 222 .then(() => { 223 console.log('grantUriPermission succeeded.'); 224 }).catch((error: BusinessError) => { 225 console.error(`grantUriPermission failed. error: ${JSON.stringify(error)}.`); 226 }); 227 } catch (error) { 228 console.error(`grantUriPermission failed. error: ${JSON.stringify(error)}.`); 229 } 230 231 // grant uri permission to clone application 232 try { 233 let appCloneIndex: number = 1; 234 uriPermissionManager.grantUriPermission(uri, wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION, targetBundleName, 235 appCloneIndex) 236 .then(() => { 237 console.log('grantUriPermission succeeded.'); 238 }).catch((error: BusinessError) => { 239 console.error(`grantUriPermission failed. error: ${JSON.stringify(error)}.`); 240 }); 241 } catch (error) { 242 console.error(`grantUriPermission failed. error: ${JSON.stringify(error)}.`); 243 } 244 } 245 } 246 247 ``` 248 249## uriPermissionManager.revokeUriPermission 250 251revokeUriPermission(uri: string, targetBundleName: string, callback: AsyncCallback<number>): void 252 253Revokes the URI permission from an application. This API uses an asynchronous callback to return the result. 254 255> **NOTE** 256> 257> This API can be used to revoke the URI permission of another application obtained by this application or URI permission granted by this application. 258 259**System API**: This is a system API. 260 261**System capability**: SystemCapability.Ability.AbilityRuntime.Core 262 263**Parameters** 264 265| Name| Type| Mandatory| Description| 266| -------- | -------- | -------- | -------- | 267| uri | string | Yes| URI of the file. The scheme has a fixed value of **file**. For details, see [FileUri](../apis-core-file-kit/js-apis-file-fileuri.md#constructor10).| 268| targetBundleName | string | Yes| Bundle name of the application, from which the permission is revoked.| 269| callback | AsyncCallback<number> | Yes| Callback used to return the result. If the operation is successful, **0** is returned; otherwise, **-1** is returned.| 270 271**Error codes** 272 273 For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 274 275| ID| Error Message| 276| ------- | -------------------------------- | 277| 202 | Not System App. Interface caller is not a system app. | 278| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.| 279| 16000050 | Internal error. | 280| 16000059 | Invalid URI type. | 281 282**Example** 283 284 ```ts 285 import { uriPermissionManager } from '@kit.AbilityKit'; 286 287 let targetBundleName = 'com.example.test_case2'; 288 let uri = "file://com.example.test_case1/data/storage/el2/base/haps/entry_test/files/newDir"; 289 290 uriPermissionManager.revokeUriPermission(uri, targetBundleName, (error) => { 291 if (error && error.code !== 0) { 292 console.error("revokeUriPermission failed, error.code = " + error.code); 293 return; 294 } 295 console.info("revokeUriPermission success"); 296 }); 297 ``` 298 299 300## uriPermissionManager.revokeUriPermission 301 302revokeUriPermission(uri: string, targetBundleName: string): Promise<number> 303 304Revokes the URI permission from an application. This API uses a promise to return the result. 305 306> **NOTE** 307> 308> This API can be used to revoke the URI permission of another application obtained by this application or URI permission granted by this application. 309 310**System API**: This is a system API. 311 312**System capability**: SystemCapability.Ability.AbilityRuntime.Core 313 314**Parameters** 315 316| Name| Type| Mandatory| Description| 317| -------- | -------- | -------- | -------- | 318| uri | string | Yes| URI of the file. The scheme has a fixed value of **file**. For details, see [FileUri](../apis-core-file-kit/js-apis-file-fileuri.md#constructor10).| 319| targetBundleName | string | Yes| Bundle name of the target application.| 320 321**Return value** 322 323| Type| Description| 324| -------- | -------- | 325| Promise<number> | Promise used to return the result. If the operation is successful, **0** is returned; otherwise, **-1** is returned.| 326 327**Error codes** 328 329 For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 330 331| ID| Error Message| 332| ------- | -------------------------------- | 333| 202 | Not System App. Interface caller is not a system app. | 334| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.| 335| 16000050 | Internal error. | 336| 16000059 | Invalid URI type. | 337 338 339**Example** 340 341 ```ts 342 import { uriPermissionManager } from '@kit.AbilityKit'; 343 import { BusinessError } from '@kit.BasicServicesKit'; 344 345 let targetBundleName = 'com.example.test_case2'; 346 let uri = "file://com.example.test_case1/data/storage/el2/base/haps/entry_test/files/newDir"; 347 348 uriPermissionManager.revokeUriPermission(uri, targetBundleName) 349 .then((data) => { 350 console.log('Verification succeeded.' + data); 351 }).catch((error: BusinessError) => { 352 console.log('Verification failed.'); 353 }); 354 ``` 355## uriPermissionManager.revokeUriPermission<sup>14+</sup> 356 357revokeUriPermission(uri: string, targetBundleName: string, appCloneIndex: number): Promise<void> 358 359Revokes the URI permission from an application. This API uses a promise to return the result. 360 361> **NOTE** 362> 363>- This API can be used to revoke the URI permission of another application obtained by this application or URI permission granted by this application. 364>- This API can be used to revoke the URI permissions granted to a cloned application. You need to specify the application bundle name and index of the cloned application. 365 366**System API**: This is a system API. 367 368**System capability**: SystemCapability.Ability.AbilityRuntime.Core 369 370**Parameters** 371 372| Name| Type| Mandatory| Description| 373| -------- | -------- | -------- | -------- | 374| uri | string | Yes| URI of the file. The scheme has a fixed value of **file**. For details, see [FileUri](../apis-core-file-kit/js-apis-file-fileuri.md#constructor10).| 375| targetBundleName | string | Yes| Bundle name of the target application.| 376| appCloneIndex | number | Yes| Index of the cloned application. The value range is [0, 1000]. The value **0** indicates the application itself.| 377 378**Return value** 379 380| Type| Description| 381| -------- | -------- | 382| Promise<void> | Promise that returns no value.| 383 384**Error codes** 385 386 For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 387 388| ID| Error Message| 389| ------- | -------------------------------- | 390| 202 | Not System App. Interface caller is not a system app. | 391| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.| 392| 16000050 | Internal error. | 393| 16000059 | Invalid URI type. | 394| 16000081 | Get target application info failed. | 395 396**Example** 397 398 ```ts 399 400 import { AbilityConstant, UIAbility, Want, wantConstant, uriPermissionManager } from '@kit.AbilityKit'; 401 import { fileUri } from '@kit.CoreFileKit'; 402 import { BusinessError } from '@kit.BasicServicesKit'; 403 404 export default class EntryAbility extends UIAbility { 405 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 406 } 407 408 onForeground(): void { 409 let targetBundleName: string = 'com.example.demo1'; 410 let filePath: string = this.context.filesDir + "/test.txt"; 411 let uri: string = fileUri.getUriFromPath(filePath); 412 // revoke uri permission of main application 413 try { 414 let appCloneIndex: number = 0; 415 uriPermissionManager.revokeUriPermission(uri, targetBundleName, appCloneIndex) 416 .then(() => { 417 console.log('revokeUriPermission succeeded.'); 418 }).catch((error: BusinessError) => { 419 console.error(`revokeUriPermission failed. error: ${JSON.stringify(error)}.`); 420 }); 421 } catch (error) { 422 console.error(`revokeUriPermission failed. error: ${JSON.stringify(error)}.`); 423 } 424 425 // revoke uri permission of clone application 426 try { 427 let appCloneIndex: number = 0; 428 uriPermissionManager.revokeUriPermission(uri, targetBundleName, appCloneIndex) 429 .then(() => { 430 console.log('revokeUriPermission succeeded.'); 431 }).catch((error: BusinessError) => { 432 console.error(`revokeUriPermission failed. error: ${JSON.stringify(error)}.`); 433 }); 434 } catch (error) { 435 console.error(`revokeUriPermission failed. error: ${JSON.stringify(error)}.`); 436 } 437 } 438 } 439 ``` 440