1e41f4b71Sopenharmony_ci# @ohos.app.ability.autoFillManager (autoFillManager) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe autoFillManager module provides APIs for saving accounts and passwords. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> The initial APIs of this module are supported since API version 11. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci> The APIs of this module can be used only in the stage model. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci## Modules to Import 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci```ts 14e41f4b71Sopenharmony_ciimport { autoFillManager } from '@kit.AbilityKit'; 15e41f4b71Sopenharmony_ci``` 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci## AutoSaveCallback 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ciImplements callbacks triggered when auto-save is complete. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci### AutoSaveCallback.onSuccess 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_cionSuccess(): void 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ciCalled when auto-save is successful. 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci**Example** 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ciSee [AutoSaveCallback.onFailure](#autosavecallbackonfailure). 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci### AutoSaveCallback.onFailure 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_cionFailure(): void 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ciCalled when auto-save fails. 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci**Example** 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci ```ts 48e41f4b71Sopenharmony_ci// Index.ets, a page containing components such as the account and password text boxes. 49e41f4b71Sopenharmony_ciimport { autoFillManager } from '@kit.AbilityKit'; 50e41f4b71Sopenharmony_ciimport { UIContext } from '@kit.ArkUI'; 51e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_cilet uiContext = AppStorage.get<UIContext>("uiContext"); 54e41f4b71Sopenharmony_cilet callback: autoFillManager.AutoSaveCallback = { 55e41f4b71Sopenharmony_ci onSuccess: () => { 56e41f4b71Sopenharmony_ci console.log("save request on success"); 57e41f4b71Sopenharmony_ci }, 58e41f4b71Sopenharmony_ci onFailure: () => { 59e41f4b71Sopenharmony_ci console.log("save request on failure"); 60e41f4b71Sopenharmony_ci } 61e41f4b71Sopenharmony_ci}; 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci@Entry 64e41f4b71Sopenharmony_ci@Component 65e41f4b71Sopenharmony_cistruct Index { 66e41f4b71Sopenharmony_ci build() { 67e41f4b71Sopenharmony_ci Button('requestAutoSave') 68e41f4b71Sopenharmony_ci .onClick(() => { 69e41f4b71Sopenharmony_ci try { 70e41f4b71Sopenharmony_ci // Initiate an auto-save request. 71e41f4b71Sopenharmony_ci autoFillManager.requestAutoSave(uiContext, callback); 72e41f4b71Sopenharmony_ci } catch (error) { 73e41f4b71Sopenharmony_ci console.error(`catch error, code: ${(error as BusinessError).code}, message: ${(error as BusinessError).message}`); 74e41f4b71Sopenharmony_ci } 75e41f4b71Sopenharmony_ci }) 76e41f4b71Sopenharmony_ci } 77e41f4b71Sopenharmony_ci} 78e41f4b71Sopenharmony_ci ``` 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci> **NOTE** 81e41f4b71Sopenharmony_ci> 82e41f4b71Sopenharmony_ci> In the example, the UiContext obtained from the AppStorage is obtained from the **OnWindowStageCreate** lifecycle of the EntryAbility (ability that starts the page) and stored in the AppStorage. For details, see [requestAutoSave](#requestautosave). 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci## requestAutoSave 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_cirequestAutoSave(context: UIContext, callback?: AutoSaveCallback): void 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ciRequests to automatically save the widget data. This API uses an asynchronous callback to return the result. 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ciIf the current widget does not support widget switching, you can call this API to save historical widget input data. The callback is triggered when the auto-save request is complete. 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci**Parameters** 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 99e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 100e41f4b71Sopenharmony_ci| context | [UIContext](../apis-arkui/js-apis-arkui-UIContext.md) | Yes| UI context in which the auto-save operation will be performed.| 101e41f4b71Sopenharmony_ci| callback | [AutoSaveCallback](#autosavecallback) | No| Callback used for the auto-save request.| 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci**Error codes** 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci| ID| Error Message| 106e41f4b71Sopenharmony_ci| ------- | -------------------------------- | 107e41f4b71Sopenharmony_ci| 401 | The parameter check failed. Possible causes: 1. Get instance id failed; 2. Parse instance id failed; 3. The second parameter is not of type callback. | 108e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ciFor details about the error codes, see [Ability Error Codes](errorcode-ability.md). 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ci**Example** 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci ```ts 115e41f4b71Sopenharmony_ci// EntryAbility.ets 116e41f4b71Sopenharmony_ciimport { UIAbility, common } from '@kit.AbilityKit'; 117e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 118e41f4b71Sopenharmony_ciimport { window, UIContext } from '@kit.ArkUI'; 119e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 122e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage): void { 123e41f4b71Sopenharmony_ci // Main window is created. Set a main page for this ability. 124e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 125e41f4b71Sopenharmony_ci let localStorageData: Record<string, string | common.UIAbilityContext> = { 126e41f4b71Sopenharmony_ci 'message': "AutoFill Page", 127e41f4b71Sopenharmony_ci 'context': this.context, 128e41f4b71Sopenharmony_ci }; 129e41f4b71Sopenharmony_ci let storage = new LocalStorage(localStorageData); 130e41f4b71Sopenharmony_ci windowStage.loadContent('pages/Index', storage, (err, data) => { 131e41f4b71Sopenharmony_ci if (err.code) { 132e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 133e41f4b71Sopenharmony_ci return; 134e41f4b71Sopenharmony_ci } 135e41f4b71Sopenharmony_ci // Obtain the main window. 136e41f4b71Sopenharmony_ci windowStage.getMainWindow((err: BusinessError, data: window.Window) => { 137e41f4b71Sopenharmony_ci let errCode: number = err.code; 138e41f4b71Sopenharmony_ci if (errCode) { 139e41f4b71Sopenharmony_ci console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err)); 140e41f4b71Sopenharmony_ci return; 141e41f4b71Sopenharmony_ci } 142e41f4b71Sopenharmony_ci console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data)); 143e41f4b71Sopenharmony_ci // get UIContext instance. 144e41f4b71Sopenharmony_ci let uiContext: UIContext = windowStage.getMainWindowSync().getUIContext(); 145e41f4b71Sopenharmony_ci PersistentStorage.persistProp("uiContext", uiContext); 146e41f4b71Sopenharmony_ci }) 147e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); 148e41f4b71Sopenharmony_ci }); 149e41f4b71Sopenharmony_ci } 150e41f4b71Sopenharmony_ci} 151e41f4b71Sopenharmony_ci ``` 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci ```ts 154e41f4b71Sopenharmony_ci // Index.ets 155e41f4b71Sopenharmony_ciimport { autoFillManager } from '@kit.AbilityKit'; 156e41f4b71Sopenharmony_ciimport { UIContext } from '@kit.ArkUI'; 157e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci@Entry 160e41f4b71Sopenharmony_ci@Component 161e41f4b71Sopenharmony_cistruct Index { 162e41f4b71Sopenharmony_ci build() { 163e41f4b71Sopenharmony_ci Row() { 164e41f4b71Sopenharmony_ci Column() { 165e41f4b71Sopenharmony_ci Text('Hello World') 166e41f4b71Sopenharmony_ci .fontSize(50) 167e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 168e41f4b71Sopenharmony_ci } 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ci Button('requestAutoSave') 171e41f4b71Sopenharmony_ci .onClick(() => { 172e41f4b71Sopenharmony_ci let uiContext = AppStorage.get<UIContext>("uiContext"); 173e41f4b71Sopenharmony_ci console.log("uiContext: ", JSON.stringify(uiContext)); 174e41f4b71Sopenharmony_ci try { 175e41f4b71Sopenharmony_ci // Initiate an auto-save request. 176e41f4b71Sopenharmony_ci autoFillManager.requestAutoSave(uiContext, { 177e41f4b71Sopenharmony_ci onSuccess: () => { 178e41f4b71Sopenharmony_ci console.log("save request on success"); 179e41f4b71Sopenharmony_ci }, 180e41f4b71Sopenharmony_ci onFailure: () => { 181e41f4b71Sopenharmony_ci console.log("save request on failure"); 182e41f4b71Sopenharmony_ci } 183e41f4b71Sopenharmony_ci }); 184e41f4b71Sopenharmony_ci } catch (error) { 185e41f4b71Sopenharmony_ci console.error(`catch error, code: ${(error as BusinessError).code}, message: ${(error as BusinessError).message}`); 186e41f4b71Sopenharmony_ci } 187e41f4b71Sopenharmony_ci }) 188e41f4b71Sopenharmony_ci .width('100%') 189e41f4b71Sopenharmony_ci } 190e41f4b71Sopenharmony_ci .height('100%') 191e41f4b71Sopenharmony_ci } 192e41f4b71Sopenharmony_ci} 193e41f4b71Sopenharmony_ci ``` 194