1# @ohos.app.ability.autoFillManager (autoFillManager) 2 3autoFillManager模块提供手动保存账号密码等功能。 4 5> **说明:** 6> 7> 本模块首批接口从API version 11 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> 9> 本模块接口仅可在Stage模型下使用。 10 11## 导入模块 12 13```ts 14import { autoFillManager } from '@kit.AbilityKit'; 15``` 16 17## AutoSaveCallback 18 19当保存请求完成时所触发的回调接口。 20 21### AutoSaveCallback.onSuccess 22 23onSuccess(): void 24 25当保存请求成功时,该回调被调用。 26 27**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 28 29**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 30 31**示例:** 32 33参见[AutoSaveCallback.onFailure](#autosavecallbackonfailure)。 34 35### AutoSaveCallback.onFailure 36 37onFailure(): void 38 39当保存请求失败时,该回调被调用。 40 41**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 42 43**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 44 45**示例:** 46 47 ```ts 48// Index.ets, 含有账号、密码框等组件的页面 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 // 发起保存请求 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> **说明:** 81> 82> 示例中从AppStorage中取得的UiContext为预先在EntryAbility(拉起此页面的Ability)中OnWindowStageCreate生命周期获得,并存储到AppStorage中,具体可参考[requestAutoSave](#requestautosave)。 83 84## requestAutoSave 85 86requestAutoSave(context: UIContext, callback?: AutoSaveCallback): void 87 88请求保存表单数据,使用callback异步回调。 89如果当前表单没有提供表单切换的功能,可以通过此接口保存历史表单输入数据,保存请求完成时会触发该回调。 90 91**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 92 93**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 94 95**参数:** 96 97| 参数名 | 类型 | 必填 | 说明 | 98| -------- | -------- | -------- | -------- | 99| context | [UIContext](../apis-arkui/js-apis-arkui-UIContext.md) | 是 | 将在其中执行保存操作的UI上下文。 | 100| callback | [AutoSaveCallback](#autosavecallback) | 否 | 保存请求的回调函数。 | 101 102**错误码:** 103 104| 错误码ID | 错误信息 | 105| ------- | -------------------------------- | 106| 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. | 107| 16000050 | Internal error. | 108 109以上错误码详细介绍请参考[元能力子系统错误码](errorcode-ability.md)。 110 111**示例:** 112 113 ```ts 114// EntryAbility.ets 115import { UIAbility, common } from '@kit.AbilityKit'; 116import { BusinessError } from '@kit.BasicServicesKit'; 117import { window, UIContext } from '@kit.ArkUI'; 118import { hilog } from '@kit.PerformanceAnalysisKit'; 119 120export default class EntryAbility extends UIAbility { 121 onWindowStageCreate(windowStage: window.WindowStage): void { 122 // Main window is created, set main page for this ability 123 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 124 let localStorageData: Record<string, string | common.UIAbilityContext> = { 125 'message': "AutoFill Page", 126 'context': this.context, 127 }; 128 let storage = new LocalStorage(localStorageData); 129 windowStage.loadContent('pages/Index', storage, (err, data) => { 130 if (err.code) { 131 hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 132 return; 133 } 134 // Obtain the main window. 135 windowStage.getMainWindow((err: BusinessError, data: window.Window) => { 136 let errCode: number = err.code; 137 if (errCode) { 138 console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err)); 139 return; 140 } 141 console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data)); 142 // get UIContext instance. 143 let uiContext: UIContext = windowStage.getMainWindowSync().getUIContext(); 144 PersistentStorage.persistProp("uiContext", uiContext); 145 }) 146 hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); 147 }); 148 } 149} 150 ``` 151 152 ```ts 153 // Index.ets 154import { autoFillManager } from '@kit.AbilityKit'; 155import { UIContext } from '@kit.ArkUI'; 156import { BusinessError } from '@kit.BasicServicesKit'; 157 158@Entry 159@Component 160struct Index { 161 build() { 162 Row() { 163 Column() { 164 Text('Hello World') 165 .fontSize(50) 166 .fontWeight(FontWeight.Bold) 167 } 168 169 Button('requestAutoSave') 170 .onClick(() => { 171 let uiContext = AppStorage.get<UIContext>("uiContext"); 172 console.log("uiContext: ", JSON.stringify(uiContext)); 173 try { 174 // 发起保存请求 175 autoFillManager.requestAutoSave(uiContext, { 176 onSuccess: () => { 177 console.log("save request on success"); 178 }, 179 onFailure: () => { 180 console.log("save request on failure"); 181 } 182 }); 183 } catch (error) { 184 console.error(`catch error, code: ${(error as BusinessError).code}, message: ${(error as BusinessError).message}`); 185 } 186 }) 187 .width('100%') 188 } 189 .height('100%') 190 } 191} 192 ``` 193