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