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