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