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