1# Persisting Temporary Permissions (ArkTS) 2 3## When to Use 4 5If an application accesses a file by using Picker, the permission for accessing the file will be automatically invalidated after the application exits or the device restarts. To retain the permission for accessing the file, you need to persist the temporarily granted permission. 6 7## Persisting a Temporary Permission Granted by Picker 8 9### Persisting a Temporary Permission 10You can use Picker to select a file or folder, and persist the temporary permission granted by Picker by using the API provided by [ohos.fileshare](../reference/apis-core-file-kit/js-apis-fileShare.md). 11 12When an application needs to temporarily access data in a user directory, for example, a communication application needs to send a user file or image, it calls [select()](../reference/apis-core-file-kit/js-apis-file-picker.md#select-3) of Picker to select the file or image to be sent. In this case, the application obtains the temporary permission for accessing the file or image. To access the file or image after the application or device is restarted, the application still needs to call a Picker API. 13 14Sometimes, an application needs to access a file or folder multiple times. For example, after editing a user file, a file editor application needs to select and open the file directly from the history records. To address this need, you can use Picker to select the file, and use [ohos.fileshare.persistPermission](../reference/apis-core-file-kit/js-apis-fileShare.md#filesharepersistpermission11) to persist the temporary permission granted by Picker. 15 16Before persisting a temporary permission, ensure that: 17- The device must have the SystemCapability.FileManagement.File.Environment.FolderObtain system capability. You can use **canIUse()** to check whether the device has the required system capability. 18 19```ts 20if (!canIUse('SystemCapability.FileManagement.File.Environment.FolderObtain')) { 21 console.error('this api is not supported on this device'); 22 return; 23} 24``` 25- The application must have the ohos.permission.FILE_ACCESS_PERSIST permission. For details about how to request the permission, see [Workflow for Requesting Permissions](../security/AccessToken/determine-application-mode.md). 26 27 28**Example** 29 30```ts 31import { BusinessError } from '@kit.BasicServicesKit'; 32import { picker } from '@kit.CoreFileKit'; 33import { fileShare } from '@kit.CoreFileKit'; 34 35async function persistPermissionExample() { 36 try { 37 let DocumentSelectOptions = new picker.DocumentSelectOptions(); 38 let documentPicker = new picker.DocumentViewPicker(); 39 let uris = await documentPicker.select(DocumentSelectOptions); 40 let policyInfo: fileShare.PolicyInfo = { 41 uri: uris[0], 42 operationMode: fileShare.OperationMode.READ_MODE, 43 }; 44 let policies: Array<fileShare.PolicyInfo> = [policyInfo]; 45 fileShare.persistPermission(policies).then(() => { 46 console.info("persistPermission successfully"); 47 }).catch((err: BusinessError<Array<fileShare.PolicyErrorResult>>) => { 48 console.error("persistPermission failed with error message: " + err.message + ", error code: " + err.code); 49 if (err.code == 13900001 && err.data) { 50 for (let i = 0; i < err.data.length; i++) { 51 console.error("error code : " + JSON.stringify(err.data[i].code)); 52 console.error("error uri : " + JSON.stringify(err.data[i].uri)); 53 console.error("error reason : " + JSON.stringify(err.data[i].message)); 54 } 55 } 56 }); 57 } catch (error) { 58 let err: BusinessError = error as BusinessError; 59 console.error('persistPermission failed with err: ' + JSON.stringify(err)); 60 } 61} 62``` 63**NOTE** 64> - You are advised to save the URI of the file with persistent permission for the related application locally to facilitate the subsequent activation. 65> - The permission persistence data is also stored in the system database. After the application or device is restarted, the persistent permission can be used only after being activated. For details, see [Activating a Persistent Permission](#activating-a-persistent-permission-for-accessing-a-file-or-folder). 66> - The APIs used for persisting permissions are available only for 2-in-1 devices. You can use **canIUse()** to check whether the device has the required system capability. The caller must also have the required permissions. 67> - When an application is uninstalled, all the permission authorization data will be deleted. After the application is reinstalled, re-authorization is required. 68 69For details about how to persist a temporary permission using C/C++ APIs, see [OH_FileShare_PersistPermission](native-fileshare-guidelines.md). 70 71### Revoking a Temporary Permission 72You can use [ohos.fileshare.revokePermission](../reference/apis-core-file-kit/js-apis-fileShare.md#filesharerevokepermission11) to revoke the persistent permission from a file, and update the data stored in the application to delete the file URI from the recently accessed data. 73 74**Required Permissions** 75 76ohos.permission.FILE_ACCESS_PERSIST. For details about how to request the permission, see [Workflow for Requesting Permissions](../security/AccessToken/determine-application-mode.md). 77 78**Example** 79 80```ts 81import { BusinessError } from '@kit.BasicServicesKit'; 82import { picker } from '@kit.CoreFileKit'; 83import { fileShare } from '@kit.CoreFileKit'; 84 85async function revokePermissionExample() { 86 try { 87 let uri = "file://docs/storage/Users/username/tmp.txt"; 88 let policyInfo: fileShare.PolicyInfo = { 89 uri: uri, 90 operationMode: fileShare.OperationMode.READ_MODE, 91 }; 92 let policies: Array<fileShare.PolicyInfo> = [policyInfo]; 93 fileShare.revokePermission(policies).then(() => { 94 console.info("revokePermission successfully"); 95 }).catch((err: BusinessError<Array<fileShare.PolicyErrorResult>>) => { 96 console.error("revokePermission failed with error message: " + err.message + ", error code: " + err.code); 97 if (err.code == 13900001 && err.data) { 98 for (let i = 0; i < err.data.length; i++) { 99 console.error("error code : " + JSON.stringify(err.data[i].code)); 100 console.error("error uri : " + JSON.stringify(err.data[i].uri)); 101 console.error("error reason : " + JSON.stringify(err.data[i].message)); 102 } 103 } 104 }); 105 } catch (error) { 106 let err: BusinessError = error as BusinessError; 107 console.error('revokePermission failed with err: ' + JSON.stringify(err)); 108 } 109} 110``` 111**NOTE** 112> - The URI in the example comes from the permission persistence data stored for the application. 113> - You are advised to activate the persistent permissions based on service requirements. Do not activate all persistent permissions. 114> - The APIs used for persisting permissions are available only for 2-in-1 devices. You can use **canIUse()** to check whether the device has the required system capability. The caller must also have the required permissions. 115 116 117 118For details about how to revoke temporary permission using C/C++ APIs, see [OH_FileShare_RevokePermission](native-fileshare-guidelines.md). 119 120## Activating a Persistent Permission for Accessing a File or Folder 121 122Each time an application is started, its persistent permissions have not been loaded to the memory. To make a persistent permission still valid after the application is restarted, use [ohos.fileshare.activatePermission](../reference/apis-core-file-kit/js-apis-fileShare.md#fileshareactivatepermission11) to activate the permission. 123 124**Required Permissions** 125 126ohos.permission.FILE_ACCESS_PERSIST. For details about how to request the permission, see [Workflow for Requesting Permissions](../security/AccessToken/determine-application-mode.md). 127 128**Example** 129 130```ts 131import { BusinessError } from '@kit.BasicServicesKit'; 132import { picker } from '@kit.CoreFileKit'; 133import { fileShare } from '@kit.CoreFileKit'; 134 135async function activatePermissionExample() { 136 try { 137 let uri = "file://docs/storage/Users/username/tmp.txt"; 138 let policyInfo: fileShare.PolicyInfo = { 139 uri: uri, 140 operationMode: fileShare.OperationMode.READ_MODE, 141 }; 142 let policies: Array<fileShare.PolicyInfo> = [policyInfo]; 143 fileShare.activatePermission(policies).then(() => { 144 console.info("activatePermission successfully"); 145 }).catch((err: BusinessError<Array<fileShare.PolicyErrorResult>>) => { 146 console.error("activatePermission failed with error message: " + err.message + ", error code: " + err.code); 147 if (err.code == 13900001 && err.data) { 148 for (let i = 0; i < err.data.length; i++) { 149 console.error("error code : " + JSON.stringify(err.data[i].code)); 150 console.error("error uri : " + JSON.stringify(err.data[i].uri)); 151 console.error("error reason : " + JSON.stringify(err.data[i].message)); 152 if (err.data[i].code == fileShare.PolicyErrorCode.PERMISSION_NOT_PERSISTED) { 153 // Persist the permission for a file or folder and then activate it. 154 } 155 } 156 } 157 }); 158 } catch (error) { 159 let err: BusinessError = error as BusinessError; 160 console.error('activatePermission failed with err: ' + JSON.stringify(err)); 161 } 162} 163``` 164**NOTE** 165> - The URI in the example comes from the permission persistence data stored for the application. 166> - You are advised to activate the persistent permissions based on service requirements. Do not activate all persistent permissions. 167> - If the activation fails because the permission has not been persisted, persist the permission first. 168> - The APIs used for persisting permissions are available only for 2-in-1 devices. You can use **canIUse()** to check whether the device has the required system capability. The caller must also have the required permissions. 169 170For details about how to activate a persistent permission using C/C++ APIs, see [OH_FileShare_ActivatePermission](native-fileshare-guidelines.md).