1e41f4b71Sopenharmony_ci# Persisting Temporary Permissions (ArkTS) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## When to Use 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciIf 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. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci## Persisting a Temporary Permission Granted by Picker 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci### Persisting a Temporary Permission 10e41f4b71Sopenharmony_ciYou 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). 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ciWhen 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. 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ciSometimes, 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. 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ciTo persist a temporary permission: 17e41f4b71Sopenharmony_ci- 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. 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci```ts 20e41f4b71Sopenharmony_ciif (!canIUse('SystemCapability.FileManagement.File.Environment.FolderObtain')) { 21e41f4b71Sopenharmony_ci console.error('this api is not supported on this device'); 22e41f4b71Sopenharmony_ci return; 23e41f4b71Sopenharmony_ci} 24e41f4b71Sopenharmony_ci``` 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci- The application must have the ohos.permission.FILE_ACCESS_PERSIST permission. 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci The ohos.permission.FILE_ACCESS_PERSIST permission is of the system_basic Ability Privilege Level (APL) and is available only to the applications of the same or higher APL. To enable an application of the normal APL to have this permission, you need to declare the permission in the Access Control List (ACL). For details, see [Workflow for Requesting Permissions](../security/AccessToken/determine-application-mode.md). 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci**Example** 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci```ts 33e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 34e41f4b71Sopenharmony_ciimport { picker } from '@kit.CoreFileKit'; 35e41f4b71Sopenharmony_ciimport { fileShare } from '@kit.CoreFileKit'; 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ciasync function persistPermissionExample() { 38e41f4b71Sopenharmony_ci try { 39e41f4b71Sopenharmony_ci let DocumentSelectOptions = new picker.DocumentSelectOptions(); 40e41f4b71Sopenharmony_ci let documentPicker = new picker.DocumentViewPicker(); 41e41f4b71Sopenharmony_ci let uris = await documentPicker.select(DocumentSelectOptions); 42e41f4b71Sopenharmony_ci let policyInfo: fileShare.PolicyInfo = { 43e41f4b71Sopenharmony_ci uri: uris[0], 44e41f4b71Sopenharmony_ci operationMode: fileShare.OperationMode.READ_MODE, 45e41f4b71Sopenharmony_ci }; 46e41f4b71Sopenharmony_ci let policies: Array<fileShare.PolicyInfo> = [policyInfo]; 47e41f4b71Sopenharmony_ci fileShare.persistPermission(policies).then(() => { 48e41f4b71Sopenharmony_ci console.info("persistPermission successfully"); 49e41f4b71Sopenharmony_ci }).catch((err: BusinessError<Array<fileShare.PolicyErrorResult>>) => { 50e41f4b71Sopenharmony_ci console.error("persistPermission failed with error message: " + err.message + ", error code: " + err.code); 51e41f4b71Sopenharmony_ci if (err.code == 13900001 && err.data) { 52e41f4b71Sopenharmony_ci for (let i = 0; i < err.data.length; i++) { 53e41f4b71Sopenharmony_ci console.error("error code : " + JSON.stringify(err.data[i].code)); 54e41f4b71Sopenharmony_ci console.error("error uri : " + JSON.stringify(err.data[i].uri)); 55e41f4b71Sopenharmony_ci console.error("error reason : " + JSON.stringify(err.data[i].message)); 56e41f4b71Sopenharmony_ci } 57e41f4b71Sopenharmony_ci } 58e41f4b71Sopenharmony_ci }); 59e41f4b71Sopenharmony_ci } catch (error) { 60e41f4b71Sopenharmony_ci let err: BusinessError = error as BusinessError; 61e41f4b71Sopenharmony_ci console.error('persistPermission failed with err: ' + JSON.stringify(err)); 62e41f4b71Sopenharmony_ci } 63e41f4b71Sopenharmony_ci} 64e41f4b71Sopenharmony_ci``` 65e41f4b71Sopenharmony_ci**NOTE** 66e41f4b71Sopenharmony_ci> - You are advised to save the URI of the file with persistent permission for the related application locally to facilitate the subsequent activation. 67e41f4b71Sopenharmony_ci> - 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). 68e41f4b71Sopenharmony_ci> - 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. 69e41f4b71Sopenharmony_ci> - When an application is uninstalled, all the permission authorization data will be deleted. After the application is reinstalled, re-authorization is required. 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ciFor details about how to persist a temporary permission using C/C++ APIs, see [OH_FileShare_PersistPermission](native-fileshare-guidelines.md). 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci### Revoking a Temporary Permission 74e41f4b71Sopenharmony_ciYou 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. 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ciThe caller must have the ohos.permission.FILE_ACCESS_PERSIST permission, which is of the system_basic APL and is available only to the applications of the same or higher APL. To enable an application of the normal APL to have this permission, you need to declare the permission in the Access Control List (ACL). For details, see [Workflow for Requesting Permissions](../security/AccessToken/determine-application-mode.md). 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci**Example** 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci```ts 81e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 82e41f4b71Sopenharmony_ciimport { picker } from '@kit.CoreFileKit'; 83e41f4b71Sopenharmony_ciimport { fileShare } from '@kit.CoreFileKit'; 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ciasync function revokePermissionExample() { 86e41f4b71Sopenharmony_ci try { 87e41f4b71Sopenharmony_ci let uri = "file://docs/storage/Users/username/tmp.txt"; 88e41f4b71Sopenharmony_ci let policyInfo: fileShare.PolicyInfo = { 89e41f4b71Sopenharmony_ci uri: uri, 90e41f4b71Sopenharmony_ci operationMode: fileShare.OperationMode.READ_MODE, 91e41f4b71Sopenharmony_ci }; 92e41f4b71Sopenharmony_ci let policies: Array<fileShare.PolicyInfo> = [policyInfo]; 93e41f4b71Sopenharmony_ci fileShare.revokePermission(policies).then(() => { 94e41f4b71Sopenharmony_ci console.info("revokePermission successfully"); 95e41f4b71Sopenharmony_ci }).catch((err: BusinessError<Array<fileShare.PolicyErrorResult>>) => { 96e41f4b71Sopenharmony_ci console.error("revokePermission failed with error message: " + err.message + ", error code: " + err.code); 97e41f4b71Sopenharmony_ci if (err.code == 13900001 && err.data) { 98e41f4b71Sopenharmony_ci for (let i = 0; i < err.data.length; i++) { 99e41f4b71Sopenharmony_ci console.error("error code : " + JSON.stringify(err.data[i].code)); 100e41f4b71Sopenharmony_ci console.error("error uri : " + JSON.stringify(err.data[i].uri)); 101e41f4b71Sopenharmony_ci console.error("error reason : " + JSON.stringify(err.data[i].message)); 102e41f4b71Sopenharmony_ci } 103e41f4b71Sopenharmony_ci } 104e41f4b71Sopenharmony_ci }); 105e41f4b71Sopenharmony_ci } catch (error) { 106e41f4b71Sopenharmony_ci let err: BusinessError = error as BusinessError; 107e41f4b71Sopenharmony_ci console.error('revokePermission failed with err: ' + JSON.stringify(err)); 108e41f4b71Sopenharmony_ci } 109e41f4b71Sopenharmony_ci} 110e41f4b71Sopenharmony_ci``` 111e41f4b71Sopenharmony_ci**NOTE** 112e41f4b71Sopenharmony_ci> - The URI in the example comes from the permission persistence data stored for the application. 113e41f4b71Sopenharmony_ci> - You are advised to activate the persistent permissions based on service requirements. Do not activate all persistent permissions. 114e41f4b71Sopenharmony_ci> - 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. 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ciFor details about how to revoke temporary permission using C/C++ APIs, see [OH_FileShare_RevokePermission](native-fileshare-guidelines.md). 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci## Activating a Persistent Permission for Accessing a File or Folder 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ciEach 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. 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ciThe caller must have the ohos.permission.FILE_ACCESS_PERSIST permission, which is of the system_basic APL and is available only to the applications of the same or higher APL. To enable an application of the normal APL to have this permission, you need to declare the permission in the Access Control List (ACL). For details, see [Workflow for Requesting Permissions](../security/AccessToken/determine-application-mode.md). 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ci**Example** 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ci```ts 127e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 128e41f4b71Sopenharmony_ciimport { picker } from '@kit.CoreFileKit'; 129e41f4b71Sopenharmony_ciimport { fileShare } from '@kit.CoreFileKit'; 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ciasync function activatePermissionExample() { 132e41f4b71Sopenharmony_ci try { 133e41f4b71Sopenharmony_ci let uri = "file://docs/storage/Users/username/tmp.txt"; 134e41f4b71Sopenharmony_ci let policyInfo: fileShare.PolicyInfo = { 135e41f4b71Sopenharmony_ci uri: uri, 136e41f4b71Sopenharmony_ci operationMode: fileShare.OperationMode.READ_MODE, 137e41f4b71Sopenharmony_ci }; 138e41f4b71Sopenharmony_ci let policies: Array<fileShare.PolicyInfo> = [policyInfo]; 139e41f4b71Sopenharmony_ci fileShare.activatePermission(policies).then(() => { 140e41f4b71Sopenharmony_ci console.info("activatePermission successfully"); 141e41f4b71Sopenharmony_ci }).catch((err: BusinessError<Array<fileShare.PolicyErrorResult>>) => { 142e41f4b71Sopenharmony_ci console.error("activatePermission failed with error message: " + err.message + ", error code: " + err.code); 143e41f4b71Sopenharmony_ci if (err.code == 13900001 && err.data) { 144e41f4b71Sopenharmony_ci for (let i = 0; i < err.data.length; i++) { 145e41f4b71Sopenharmony_ci console.error("error code : " + JSON.stringify(err.data[i].code)); 146e41f4b71Sopenharmony_ci console.error("error uri : " + JSON.stringify(err.data[i].uri)); 147e41f4b71Sopenharmony_ci console.error("error reason : " + JSON.stringify(err.data[i].message)); 148e41f4b71Sopenharmony_ci if (err.data[i].code == fileShare.PolicyErrorCode.PERMISSION_NOT_PERSISTED) { 149e41f4b71Sopenharmony_ci // Persist the permission for a file or folder and then activate it. 150e41f4b71Sopenharmony_ci } 151e41f4b71Sopenharmony_ci } 152e41f4b71Sopenharmony_ci } 153e41f4b71Sopenharmony_ci }); 154e41f4b71Sopenharmony_ci } catch (error) { 155e41f4b71Sopenharmony_ci let err: BusinessError = error as BusinessError; 156e41f4b71Sopenharmony_ci console.error('activatePermission failed with err: ' + JSON.stringify(err)); 157e41f4b71Sopenharmony_ci } 158e41f4b71Sopenharmony_ci} 159e41f4b71Sopenharmony_ci``` 160e41f4b71Sopenharmony_ci**NOTE** 161e41f4b71Sopenharmony_ci> - The URI in the example comes from the permission persistence data stored for the application. 162e41f4b71Sopenharmony_ci> - You are advised to activate the persistent permissions based on service requirements. Do not activate all persistent permissions. 163e41f4b71Sopenharmony_ci> - If the activation fails because the permission has not been persisted, persist the permission first. 164e41f4b71Sopenharmony_ci> - 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. 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_ciFor details about how to activate a persistent permission using C/C++ APIs, see [OH_FileShare_ActivatePermission](native-fileshare-guidelines.md).