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