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