1# @ohos.app.ability.autoFillManager (Auto-Fill Framework) 2 3<!--Kit: Ability Kit--> 4<!--Subsystem: Ability--> 5<!--Owner: @hanchen45; @Luobniz21--> 6<!--Designer: @ccllee1--> 7<!--Tester: @lixueqing513--> 8<!--Adviser: @huipeizi--> 9 10The autoFillManager module provides auto-fill capabilities for user information such as accounts, passwords, addresses, and phone numbers. 11 12Unlike the system's auto-save feature that triggers during page transitions, this feature requires manual activation by the user. For example, the user must input their account and password on a website and click the **Save** button to initiate the saving process. 13 14> **NOTE** 15> 16> 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. 17> 18> The APIs of this module can be used only in the stage model. 19 20## Modules to Import 21 22```ts 23import { autoFillManager } from '@kit.AbilityKit'; 24``` 25 26## AutoSaveCallback 27 28Implements callbacks triggered when auto-save is complete. 29 30### onSuccess 31 32onSuccess(): void 33 34Called when auto-save is successful. 35 36**Atomic service API**: This API can be used in atomic services since API version 12. 37 38**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 39 40**Example** 41 42See [AutoSaveCallback.onFailure](#onfailure). 43 44### onFailure 45 46onFailure(): void 47 48Called when auto-save fails. 49 50**Atomic service API**: This API can be used in atomic services since API version 12. 51 52**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 53 54**Example** 55 56 ```ts 57// Index.ets, a page containing components such as the account and password text boxes. 58import { autoFillManager } from '@kit.AbilityKit'; 59import { UIContext } from '@kit.ArkUI'; 60import { BusinessError } from '@kit.BasicServicesKit'; 61 62let uiContext = AppStorage.get<UIContext>("uiContext"); 63let callback: autoFillManager.AutoSaveCallback = { 64 onSuccess: () => { 65 console.info(`save request on success.`); 66 }, 67 onFailure: () => { 68 console.error(`save request on failure.`); 69 } 70}; 71 72@Entry 73@Component 74struct Index { 75 build() { 76 Button('requestAutoSave') 77 .onClick(() => { 78 try { 79 // Initiate an auto-save request. 80 autoFillManager.requestAutoSave(uiContext, callback); 81 } catch (error) { 82 console.error(`catch error, code: ${(error as BusinessError).code}, message: ${(error as BusinessError).message}`); 83 } 84 }) 85 } 86} 87 ``` 88 89> **NOTE** 90> 91> 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](#autofillmanagerrequestautosave). 92 93## autoFillManager.requestAutoSave 94 95requestAutoSave(context: UIContext, callback?: AutoSaveCallback): void 96 97Requests to automatically save the widget data. This API uses an asynchronous callback to return the result. 98 99If 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. 100 101**Atomic service API**: This API can be used in atomic services since API version 12. 102 103**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 104 105**Parameters** 106 107| Name| Type| Mandatory| Description| 108| -------- | -------- | -------- | -------- | 109| context | [UIContext](../apis-arkui/arkts-apis-uicontext-uicontext.md) | Yes| UI context in which the auto-save operation will be performed.| 110| callback | [AutoSaveCallback](#autosavecallback) | No| Implements callbacks triggered when auto-save is complete.| 111 112**Error codes** 113 114For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 115| ID| Error Message| 116| ------- | -------------------------------- | 117| 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. | 118| 16000050 | Internal error. | 119 120**Example** 121 122 ```ts 123// EntryAbility.ets 124import { UIAbility, common } from '@kit.AbilityKit'; 125import { BusinessError } from '@kit.BasicServicesKit'; 126import { window, UIContext } from '@kit.ArkUI'; 127import { hilog } from '@kit.PerformanceAnalysisKit'; 128 129export default class EntryAbility extends UIAbility { 130 onWindowStageCreate(windowStage: window.WindowStage): void { 131 // Main window is created. Set a main page for this ability. 132 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 133 let localStorageData: Record<string, string | common.UIAbilityContext> = { 134 'message': "AutoFill Page", 135 'context': this.context, 136 }; 137 let storage = new LocalStorage(localStorageData); 138 windowStage.loadContent('pages/Index', storage, (err, data) => { 139 if (err && err.code) { 140 hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 141 return; 142 } 143 // Obtain the main window. 144 windowStage.getMainWindow((err: BusinessError, data: window.Window) => { 145 let errCode: number = err?.code; 146 if (errCode) { 147 console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err)); 148 return; 149 } 150 console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data)); 151 // get UIContext instance. 152 let uiContext: UIContext = windowStage.getMainWindowSync().getUIContext(); 153 PersistentStorage.persistProp("uiContext", uiContext); 154 }) 155 hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); 156 }); 157 } 158} 159 ``` 160 161 ```ts 162 // Index.ets 163import { autoFillManager } from '@kit.AbilityKit'; 164import { UIContext } from '@kit.ArkUI'; 165import { BusinessError } from '@kit.BasicServicesKit'; 166 167@Entry 168@Component 169struct Index { 170 build() { 171 Row() { 172 Column() { 173 Text('Hello World') 174 .fontSize(50) 175 .fontWeight(FontWeight.Bold) 176 } 177 178 Button('requestAutoSave') 179 .onClick(() => { 180 let uiContext = AppStorage.get<UIContext>("uiContext"); 181 console.info("uiContext: ", JSON.stringify(uiContext)); 182 try { 183 // Initiate an auto-save request. 184 autoFillManager.requestAutoSave(uiContext, { 185 onSuccess: () => { 186 console.info(`save request on success.`); 187 }, 188 onFailure: () => { 189 console.error(`save request on failure.`); 190 } 191 }); 192 } catch (error) { 193 console.error(`catch error, code: ${(error as BusinessError).code}, message: ${(error as BusinessError).message}`); 194 } 195 }) 196 .width('100%') 197 } 198 .height('100%') 199 } 200} 201 ``` 202