提交 7fbf865d 编写于 作者: L lixiang

modify serializeWebState&RestoreWebState sample code

Signed-off-by: Nlixiang <lixiang380@huawei.com>
上级 2ecf880b
...@@ -3588,10 +3588,11 @@ serializeWebState(): Uint8Array ...@@ -3588,10 +3588,11 @@ serializeWebState(): Uint8Array
**示例:** **示例:**
1.对文件的操作需要导入文件管理模块,详情请参考[文件管理](./js-apis-file-fs.md)
```ts ```ts
// xxx.ets // xxx.ets
import web_webview from '@ohos.web.webview'; import web_webview from '@ohos.web.webview';
import fileio from '@ohos.fileio'; import fs from '@ohos.file.fs';
@Entry @Entry
@Component @Component
...@@ -3604,11 +3605,13 @@ struct WebComponent { ...@@ -3604,11 +3605,13 @@ struct WebComponent {
.onClick(() => { .onClick(() => {
try { try {
let state = this.controller.serializeWebState(); let state = this.controller.serializeWebState();
let path = globalThis.AbilityContext.cacheDir; // globalThis.cacheDir从MainAbility.ts中获取。
let path = globalThis.cacheDir;
path += '/WebState'; path += '/WebState';
let fd = fileio.openSync(path, 0o2 | 0o100, 0o666); // 以同步方法打开文件。
fileio.writeSync(fd, state.buffer); let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
fileio.closeSync(fd); fs.writeSync(file.fd, state.buffer);
fs.closeSync(file.fd);
} catch (error) { } catch (error) {
console.error(`ErrorCode: ${error.code}, Message: ${error.message}`); console.error(`ErrorCode: ${error.code}, Message: ${error.message}`);
} }
...@@ -3619,6 +3622,21 @@ struct WebComponent { ...@@ -3619,6 +3622,21 @@ struct WebComponent {
} }
``` ```
2.修改MainAbility.ts。
获取应用缓存文件路径。
```ts
// xxx.ts
import UIAbility from '@ohos.app.ability.UIAbility';
import web_webview from '@ohos.web.webview';
export default class MainAbility extends UIAbility {
onCreate(want, launchParam) {
// 通过在globalThis对象上绑定cacheDir,可以实现UIAbility组件与Page之间的数据同步。
globalThis.cacheDir = this.context.cacheDir;
}
}
```
### restoreWebState ### restoreWebState
restoreWebState(state: Uint8Array): void restoreWebState(state: Uint8Array): void
...@@ -3643,10 +3661,11 @@ restoreWebState(state: Uint8Array): void ...@@ -3643,10 +3661,11 @@ restoreWebState(state: Uint8Array): void
**示例:** **示例:**
1.对文件的操作需要导入文件管理模块,详情请参考[文件管理](./js-apis-file-fs.md)
```ts ```ts
// xxx.ets // xxx.ets
import web_webview from '@ohos.web.webview'; import web_webview from '@ohos.web.webview';
import fileio from '@ohos.fileio'; import fs from '@ohos.file.fs';
@Entry @Entry
@Component @Component
...@@ -3658,17 +3677,22 @@ struct WebComponent { ...@@ -3658,17 +3677,22 @@ struct WebComponent {
Button('RestoreWebState') Button('RestoreWebState')
.onClick(() => { .onClick(() => {
try { try {
let path = globalThis.AbilityContext.cacheDir; // globalThis.cacheDir从MainAbility.ts中获取。
let path = globalThis.cacheDir;
path += '/WebState'; path += '/WebState';
let fd = fileio.openSync(path, 0o002, 0o666); // 以同步方法打开文件。
let stat = fileio.fstatSync(fd); let file = fs.openSync(path, fs.OpenMode.READ_WRITE);
let stat = fs.statSync(path);
let size = stat.size; let size = stat.size;
let buf = new ArrayBuffer(size); let buf = new ArrayBuffer(size);
fileio.read(fd, buf, (err, data) => { fs.read(file.fd, buf, (err, readLen) => {
if (data) { if (err) {
this.controller.restoreWebState(new Uint8Array(data.buffer)); console.info("mkdir failed with error message: " + err.message + ", error code: " + err.code);
} else {
console.info("read file data succeed");
this.controller.restoreWebState(new Uint8Array(buf.slice(0, readLen)));
fs.closeSync(file);
} }
fileio.closeSync(fd);
}); });
} catch (error) { } catch (error) {
console.error(`ErrorCode: ${error.code}, Message: ${error.message}`); console.error(`ErrorCode: ${error.code}, Message: ${error.message}`);
...@@ -3680,6 +3704,21 @@ struct WebComponent { ...@@ -3680,6 +3704,21 @@ struct WebComponent {
} }
``` ```
2.修改MainAbility.ts。
获取应用缓存文件路径。
```ts
// xxx.ts
import UIAbility from '@ohos.app.ability.UIAbility';
import web_webview from '@ohos.web.webview';
export default class MainAbility extends UIAbility {
onCreate(want, launchParam) {
// 通过在globalThis对象上绑定cacheDir,可以实现UIAbility组件与Page之间的数据同步。
globalThis.cacheDir = this.context.cacheDir;
}
}
```
### customizeSchemes ### customizeSchemes
static customizeSchemes(schemes: Array\<WebCustomScheme\>): void static customizeSchemes(schemes: Array\<WebCustomScheme\>): void
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册