ota-native-bridge 1.6.0 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/dist/cjs/native-messenger.js +3 -0
- package/dist/cjs/plugins.js +8 -0
- package/dist/cjs/runtime.js +15 -2
- package/dist/cjs/version.js +1 -1
- package/dist/native-messenger.js +3 -0
- package/dist/native.d.ts +5 -1
- package/dist/plugins.d.ts +5 -1
- package/dist/plugins.js +8 -0
- package/dist/runtime.d.ts +1 -1
- package/dist/runtime.js +15 -2
- package/dist/types.d.ts +25 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/docs/USAGE.md +160 -25
- package/ota-native-bridge.js +13 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -27,7 +27,7 @@ if (Native.isNativePlatform()) {
|
|
|
27
27
|
|
|
28
28
|
**要点:**
|
|
29
29
|
|
|
30
|
-
-
|
|
30
|
+
- 送进 WebView 的脚本不要保留 `async` / `await`(旧 WebView 解析不了该语法;构建器已转译则不受影响,见 [docs/USAGE.md](./docs/USAGE.md))。接口用 `.then()` 或 callback;也不必先 `ready()`
|
|
31
31
|
- `Native.isNativePlatform()` 判断是否在壳 App 里
|
|
32
32
|
- 壳内打开外链用 `Browser.open(url)`,不要用 `<a target="_blank">`
|
|
33
33
|
- 自行请求 Client API 时**必须**用 `Crypto.encryptRequestBody` / `postEncrypted`,公钥来自 `App.getInfo().platform.paramPublicKey`
|
|
@@ -52,6 +52,7 @@ if (Native.isNativePlatform()) {
|
|
|
52
52
|
| 读设备标识 | `Device.getIdentifiers()`(须 `collectDeviceIdsAfterConsent`) |
|
|
53
53
|
| 读壳生成的设备序列号 | `Device.getInstallDeviceId()`(UUID v4,与签名证书无关,不需要同意开关) |
|
|
54
54
|
| 读分辨率 / 方向 / 锁屏 | `Device.getScreenInfo()` / `getOrientation()` / `getLockStatus()` |
|
|
55
|
+
| 设方向 / 全屏 | `Device.setOrientation()` / `Device.setFullscreen()` |
|
|
55
56
|
| 读机型 / 系统 / CPU / Canvas / WebGL | `Device.getEnvironmentInfo()` 一次返回(含系统版本、厂商、型号);也可分别调用 `getInfo()` / `getSystemInfo()` / `getCpuInfo()` / `getCanvasRenderInfo()` / `getWebGlInfo()` |
|
|
56
57
|
| 用资源包里的图换桌面图标 | `Icon.setFromResource({ path: 'icons/app-icon.png' })`(Android 8+;iOS / Harmony 返回失败) |
|
|
57
58
|
| 加密 Client API 请求 | `Crypto.postEncrypted(url, plain, pem)` |
|
|
@@ -16,6 +16,9 @@ function getNativePostMessage() {
|
|
|
16
16
|
if (window.HarmonyBridge?.postMessage) {
|
|
17
17
|
return (message) => window.HarmonyBridge.postMessage(message);
|
|
18
18
|
}
|
|
19
|
+
if (window.DesktopBridge?.postMessage) {
|
|
20
|
+
return (message) => window.DesktopBridge.postMessage(message);
|
|
21
|
+
}
|
|
19
22
|
return null;
|
|
20
23
|
}
|
|
21
24
|
function isNativeBridgeAvailable() {
|
package/dist/cjs/plugins.js
CHANGED
|
@@ -59,6 +59,14 @@ exports.Device = {
|
|
|
59
59
|
getOrientation(callback) {
|
|
60
60
|
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('getOrientation'), callback);
|
|
61
61
|
},
|
|
62
|
+
/** 锁定屏幕方向。portrait / landscape 锁定;default 恢复打包默认。仅运行期有效,桌面壳返回 supported=false。 */
|
|
63
|
+
setOrientation(options, callback) {
|
|
64
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('setOrientation', { ...options }), callback);
|
|
65
|
+
},
|
|
66
|
+
/** 全屏:隐藏/恢复状态栏与导航栏。仅运行期有效,页面刷新后由页面自行再调用。 */
|
|
67
|
+
setFullscreen(options, callback) {
|
|
68
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('setFullscreen', { ...options }), callback);
|
|
69
|
+
},
|
|
62
70
|
getLockStatus(callback) {
|
|
63
71
|
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('getLockStatus'), callback);
|
|
64
72
|
},
|
package/dist/cjs/runtime.js
CHANGED
|
@@ -31,10 +31,18 @@ function decodeBridgePayload(payload) {
|
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
function installNativeCallback() {
|
|
34
|
-
if (typeof window === 'undefined'
|
|
34
|
+
if (typeof window === 'undefined') {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const current = window.__nativeBridgeCallback;
|
|
38
|
+
if (current && current.__sdk) {
|
|
35
39
|
return;
|
|
36
40
|
}
|
|
37
|
-
|
|
41
|
+
const previous = current;
|
|
42
|
+
const cb = (id, success, payload) => {
|
|
43
|
+
if (typeof previous === 'function') {
|
|
44
|
+
previous(id, success, payload);
|
|
45
|
+
}
|
|
38
46
|
const item = pending[id];
|
|
39
47
|
if (!item) {
|
|
40
48
|
return;
|
|
@@ -48,6 +56,8 @@ function installNativeCallback() {
|
|
|
48
56
|
item.reject(new Error(String(data)));
|
|
49
57
|
}
|
|
50
58
|
};
|
|
59
|
+
cb.__sdk = true;
|
|
60
|
+
window.__nativeBridgeCallback = cb;
|
|
51
61
|
}
|
|
52
62
|
function callNative(method, params) {
|
|
53
63
|
return new Promise((resolve, reject) => {
|
|
@@ -78,6 +88,9 @@ function getPlatform() {
|
|
|
78
88
|
if (window.HarmonyBridge?.postMessage) {
|
|
79
89
|
return 'harmony';
|
|
80
90
|
}
|
|
91
|
+
if (window.DesktopBridge?.postMessage) {
|
|
92
|
+
return window.DesktopBridge.os === 'macos' ? 'macos' : 'windows';
|
|
93
|
+
}
|
|
81
94
|
return 'web';
|
|
82
95
|
}
|
|
83
96
|
function isNativePlatform() {
|
package/dist/cjs/version.js
CHANGED
package/dist/native-messenger.js
CHANGED
|
@@ -12,6 +12,9 @@ export function getNativePostMessage() {
|
|
|
12
12
|
if (window.HarmonyBridge?.postMessage) {
|
|
13
13
|
return (message) => window.HarmonyBridge.postMessage(message);
|
|
14
14
|
}
|
|
15
|
+
if (window.DesktopBridge?.postMessage) {
|
|
16
|
+
return (message) => window.DesktopBridge.postMessage(message);
|
|
17
|
+
}
|
|
15
18
|
return null;
|
|
16
19
|
}
|
|
17
20
|
export function isNativeBridgeAvailable() {
|
package/dist/native.d.ts
CHANGED
|
@@ -57,6 +57,8 @@ export declare const Native: {
|
|
|
57
57
|
getInfo(callback?: import("./runtime").NativeCallback<import("./types").DeviceInfo>): Promise<import("./types").DeviceInfo>;
|
|
58
58
|
getScreenInfo(callback?: import("./runtime").NativeCallback<import("./types").ScreenInfo>): Promise<import("./types").ScreenInfo>;
|
|
59
59
|
getOrientation(callback?: import("./runtime").NativeCallback<import("./types").OrientationInfo>): Promise<import("./types").OrientationInfo>;
|
|
60
|
+
setOrientation(options: import("./types").SetOrientationOptions, callback?: import("./runtime").NativeCallback<import("./types").SetOrientationResult>): Promise<import("./types").SetOrientationResult>;
|
|
61
|
+
setFullscreen(options: import("./types").SetFullscreenOptions, callback?: import("./runtime").NativeCallback<import("./types").SetFullscreenResult>): Promise<import("./types").SetFullscreenResult>;
|
|
60
62
|
getLockStatus(callback?: import("./runtime").NativeCallback<import("./types").LockStatus>): Promise<import("./types").LockStatus>;
|
|
61
63
|
getSystemInfo(callback?: import("./runtime").NativeCallback<import("./types").SystemInfo>): Promise<import("./types").SystemInfo>;
|
|
62
64
|
getCpuInfo(callback?: import("./runtime").NativeCallback<import("./types").CpuInfo>): Promise<import("./types").CpuInfo>;
|
|
@@ -223,7 +225,7 @@ export declare const Native: {
|
|
|
223
225
|
readonly Push: {
|
|
224
226
|
getToken(callback?: import("./runtime").NativeCallback<import("./types").PushTokenResult>): Promise<import("./types").PushTokenResult>;
|
|
225
227
|
};
|
|
226
|
-
readonly sdkVersion: "1.
|
|
228
|
+
readonly sdkVersion: "1.7.0";
|
|
227
229
|
readonly getPlatform: typeof getPlatform;
|
|
228
230
|
readonly isNativePlatform: typeof isNativePlatform;
|
|
229
231
|
readonly isNativeBridgeAvailable: typeof isNativeBridgeAvailable;
|
|
@@ -278,6 +280,8 @@ export declare const Native: {
|
|
|
278
280
|
getInfo(callback?: import("./runtime").NativeCallback<import("./types").DeviceInfo>): Promise<import("./types").DeviceInfo>;
|
|
279
281
|
getScreenInfo(callback?: import("./runtime").NativeCallback<import("./types").ScreenInfo>): Promise<import("./types").ScreenInfo>;
|
|
280
282
|
getOrientation(callback?: import("./runtime").NativeCallback<import("./types").OrientationInfo>): Promise<import("./types").OrientationInfo>;
|
|
283
|
+
setOrientation(options: import("./types").SetOrientationOptions, callback?: import("./runtime").NativeCallback<import("./types").SetOrientationResult>): Promise<import("./types").SetOrientationResult>;
|
|
284
|
+
setFullscreen(options: import("./types").SetFullscreenOptions, callback?: import("./runtime").NativeCallback<import("./types").SetFullscreenResult>): Promise<import("./types").SetFullscreenResult>;
|
|
281
285
|
getLockStatus(callback?: import("./runtime").NativeCallback<import("./types").LockStatus>): Promise<import("./types").LockStatus>;
|
|
282
286
|
getSystemInfo(callback?: import("./runtime").NativeCallback<import("./types").SystemInfo>): Promise<import("./types").SystemInfo>;
|
|
283
287
|
getCpuInfo(callback?: import("./runtime").NativeCallback<import("./types").CpuInfo>): Promise<import("./types").CpuInfo>;
|
package/dist/plugins.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type NativeCallback } from './runtime';
|
|
2
|
-
import type { AppChannelCodes, AppIconState, AppIconsResult, AppInfo, AppInstalledResult, AppUpdateTriggerResult, ApkUpdateResult, CheckAppUpdateOptions, CheckAppsInstalledResult, DeviceIdentifiers, InstallDeviceId, DeviceInfo, CpuInfo, CanvasRenderInfo, WebGlInfo, EnvironmentInfo, EnvironmentDeviceInfo, PhoneStatePermissionResult, InstallAppPackageOptions, InstallAppPackageResult, InstallResourcePackageOptions, InstallResourcePackageResult, LaunchAppOptions, LaunchAppResult, LocationPermissionResult, LocationResult, LocationStatus, LocationWatchResult, LockStatus, NativeFeatureConfig, NotificationOptions, OpenInBrowserOptions, OpenInBrowserResult, OrientationInfo, PlayVideoOptions, PlayVideoResult, HttpRequestOptions, HttpRequestResult, ResourceUpdateResult, SafeAreaInsets, ScreenInfo, SetAppIconFromResourceOptions, SetAppIconOptions, SetAppIconResult, SetAppThemeOptions, SetNativeFeaturesOptions, SystemInfo, ThemeConfig, VideoPlayerStatus, WindowInfo, AdapterState, BiometricStatus, BrightnessState, CalendarEventOptions, CaptureResult, ClipboardResult, ContactResult, FlashlightState, MotionReading, NativeOk, PaymentOptions, PaymentResult, PedometerResult, PickerResult, PushTokenResult, RecorderResult, ReviewResult, ScanResult, ShareOptions, VolumeState } from './types';
|
|
2
|
+
import type { AppChannelCodes, AppIconState, AppIconsResult, AppInfo, AppInstalledResult, AppUpdateTriggerResult, ApkUpdateResult, CheckAppUpdateOptions, CheckAppsInstalledResult, DeviceIdentifiers, InstallDeviceId, DeviceInfo, CpuInfo, CanvasRenderInfo, WebGlInfo, EnvironmentInfo, EnvironmentDeviceInfo, PhoneStatePermissionResult, InstallAppPackageOptions, InstallAppPackageResult, InstallResourcePackageOptions, InstallResourcePackageResult, LaunchAppOptions, LaunchAppResult, LocationPermissionResult, LocationResult, LocationStatus, LocationWatchResult, LockStatus, NativeFeatureConfig, NotificationOptions, OpenInBrowserOptions, OpenInBrowserResult, OrientationInfo, SetFullscreenOptions, SetFullscreenResult, SetOrientationOptions, SetOrientationResult, PlayVideoOptions, PlayVideoResult, HttpRequestOptions, HttpRequestResult, ResourceUpdateResult, SafeAreaInsets, ScreenInfo, SetAppIconFromResourceOptions, SetAppIconOptions, SetAppIconResult, SetAppThemeOptions, SetNativeFeaturesOptions, SystemInfo, ThemeConfig, VideoPlayerStatus, WindowInfo, AdapterState, BiometricStatus, BrightnessState, CalendarEventOptions, CaptureResult, ClipboardResult, ContactResult, FlashlightState, MotionReading, NativeOk, PaymentOptions, PaymentResult, PedometerResult, PickerResult, PushTokenResult, RecorderResult, ReviewResult, ScanResult, ShareOptions, VolumeState } from './types';
|
|
3
3
|
export declare const App: {
|
|
4
4
|
ping(callback?: NativeCallback<{
|
|
5
5
|
pong: boolean;
|
|
@@ -19,6 +19,10 @@ export declare const Device: {
|
|
|
19
19
|
getInfo(callback?: NativeCallback<DeviceInfo>): Promise<DeviceInfo>;
|
|
20
20
|
getScreenInfo(callback?: NativeCallback<ScreenInfo>): Promise<ScreenInfo>;
|
|
21
21
|
getOrientation(callback?: NativeCallback<OrientationInfo>): Promise<OrientationInfo>;
|
|
22
|
+
/** 锁定屏幕方向。portrait / landscape 锁定;default 恢复打包默认。仅运行期有效,桌面壳返回 supported=false。 */
|
|
23
|
+
setOrientation(options: SetOrientationOptions, callback?: NativeCallback<SetOrientationResult>): Promise<SetOrientationResult>;
|
|
24
|
+
/** 全屏:隐藏/恢复状态栏与导航栏。仅运行期有效,页面刷新后由页面自行再调用。 */
|
|
25
|
+
setFullscreen(options: SetFullscreenOptions, callback?: NativeCallback<SetFullscreenResult>): Promise<SetFullscreenResult>;
|
|
22
26
|
getLockStatus(callback?: NativeCallback<LockStatus>): Promise<LockStatus>;
|
|
23
27
|
getSystemInfo(callback?: NativeCallback<SystemInfo>): Promise<SystemInfo>;
|
|
24
28
|
/** 逻辑核心数。Android / iOS 读系统值;Harmony 与浏览器预览读 WebView 的 hardwareConcurrency。 */
|
package/dist/plugins.js
CHANGED
|
@@ -56,6 +56,14 @@ export const Device = {
|
|
|
56
56
|
getOrientation(callback) {
|
|
57
57
|
return withCallback(invokeNative('getOrientation'), callback);
|
|
58
58
|
},
|
|
59
|
+
/** 锁定屏幕方向。portrait / landscape 锁定;default 恢复打包默认。仅运行期有效,桌面壳返回 supported=false。 */
|
|
60
|
+
setOrientation(options, callback) {
|
|
61
|
+
return withCallback(invokeNative('setOrientation', { ...options }), callback);
|
|
62
|
+
},
|
|
63
|
+
/** 全屏:隐藏/恢复状态栏与导航栏。仅运行期有效,页面刷新后由页面自行再调用。 */
|
|
64
|
+
setFullscreen(options, callback) {
|
|
65
|
+
return withCallback(invokeNative('setFullscreen', { ...options }), callback);
|
|
66
|
+
},
|
|
59
67
|
getLockStatus(callback) {
|
|
60
68
|
return withCallback(invokeNative('getLockStatus'), callback);
|
|
61
69
|
},
|
package/dist/runtime.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isNativeBridgeAvailable } from './native-messenger';
|
|
2
|
-
export type NativePlatform = 'android' | 'ios' | 'harmony' | 'web';
|
|
2
|
+
export type NativePlatform = 'android' | 'ios' | 'harmony' | 'windows' | 'macos' | 'web';
|
|
3
3
|
export type NativeCallback<T> = (result: T, error?: Error) => void;
|
|
4
4
|
/** 空串 / `"undefined"` 不能 JSON.parse;失败时回传原字符串,避免同步 Uncaught SyntaxError */
|
|
5
5
|
export declare function decodeBridgePayload(payload: string | object | null | undefined): unknown;
|
package/dist/runtime.js
CHANGED
|
@@ -20,10 +20,18 @@ export function decodeBridgePayload(payload) {
|
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
function installNativeCallback() {
|
|
23
|
-
if (typeof window === 'undefined'
|
|
23
|
+
if (typeof window === 'undefined') {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const current = window.__nativeBridgeCallback;
|
|
27
|
+
if (current && current.__sdk) {
|
|
24
28
|
return;
|
|
25
29
|
}
|
|
26
|
-
|
|
30
|
+
const previous = current;
|
|
31
|
+
const cb = (id, success, payload) => {
|
|
32
|
+
if (typeof previous === 'function') {
|
|
33
|
+
previous(id, success, payload);
|
|
34
|
+
}
|
|
27
35
|
const item = pending[id];
|
|
28
36
|
if (!item) {
|
|
29
37
|
return;
|
|
@@ -37,6 +45,8 @@ function installNativeCallback() {
|
|
|
37
45
|
item.reject(new Error(String(data)));
|
|
38
46
|
}
|
|
39
47
|
};
|
|
48
|
+
cb.__sdk = true;
|
|
49
|
+
window.__nativeBridgeCallback = cb;
|
|
40
50
|
}
|
|
41
51
|
export function callNative(method, params) {
|
|
42
52
|
return new Promise((resolve, reject) => {
|
|
@@ -67,6 +77,9 @@ export function getPlatform() {
|
|
|
67
77
|
if (window.HarmonyBridge?.postMessage) {
|
|
68
78
|
return 'harmony';
|
|
69
79
|
}
|
|
80
|
+
if (window.DesktopBridge?.postMessage) {
|
|
81
|
+
return window.DesktopBridge.os === 'macos' ? 'macos' : 'windows';
|
|
82
|
+
}
|
|
70
83
|
return 'web';
|
|
71
84
|
}
|
|
72
85
|
export function isNativePlatform() {
|
package/dist/types.d.ts
CHANGED
|
@@ -324,6 +324,27 @@ export interface OrientationInfo {
|
|
|
324
324
|
portrait: boolean;
|
|
325
325
|
landscape: boolean;
|
|
326
326
|
}
|
|
327
|
+
/** portrait 锁竖屏;landscape 锁横屏;default 恢复打包时的默认方向 */
|
|
328
|
+
export type ScreenOrientationMode = 'portrait' | 'landscape' | 'default';
|
|
329
|
+
export interface SetOrientationOptions {
|
|
330
|
+
orientation: ScreenOrientationMode;
|
|
331
|
+
}
|
|
332
|
+
export interface SetOrientationResult {
|
|
333
|
+
success: boolean;
|
|
334
|
+
/** 桌面壳为 false */
|
|
335
|
+
supported?: boolean;
|
|
336
|
+
orientation?: ScreenOrientationMode;
|
|
337
|
+
message?: string;
|
|
338
|
+
}
|
|
339
|
+
export interface SetFullscreenOptions {
|
|
340
|
+
/** true 隐藏状态栏与导航栏;false 恢复显示 */
|
|
341
|
+
enabled: boolean;
|
|
342
|
+
}
|
|
343
|
+
export interface SetFullscreenResult {
|
|
344
|
+
success: boolean;
|
|
345
|
+
fullscreen?: boolean;
|
|
346
|
+
message?: string;
|
|
347
|
+
}
|
|
327
348
|
export interface LockStatus {
|
|
328
349
|
/** 当前是否锁屏(或钥匙串/保护数据不可用) */
|
|
329
350
|
locked: boolean;
|
|
@@ -660,6 +681,10 @@ declare global {
|
|
|
660
681
|
HarmonyBridge?: {
|
|
661
682
|
postMessage: (msg: string) => void;
|
|
662
683
|
};
|
|
684
|
+
DesktopBridge?: {
|
|
685
|
+
os?: 'windows' | 'macos';
|
|
686
|
+
postMessage: (msg: string) => void;
|
|
687
|
+
};
|
|
663
688
|
webkit?: {
|
|
664
689
|
messageHandlers?: {
|
|
665
690
|
IOSBridge?: {
|
package/dist/version.d.ts
CHANGED
package/dist/version.js
CHANGED
package/docs/USAGE.md
CHANGED
|
@@ -36,9 +36,33 @@ if (!Native.isNativePlatform()) {
|
|
|
36
36
|
|
|
37
37
|
1. **`Native.isNativePlatform()`** — 是否在壳 App 里
|
|
38
38
|
2. **直接调用** — 不用先 `ready()`,SDK 会自动等待壳就绪
|
|
39
|
-
3.
|
|
39
|
+
3. **送进 WebView 的脚本不要保留 `async` / `await` 语法** — 用 `.then()` 或 callback。原因见下节
|
|
40
40
|
4. **外链用 `Browser.open(url)`** — 不要用 `<a target="_blank">`(会在 WebView 内打开)
|
|
41
41
|
|
|
42
|
+
#### 为什么不要把 async/await 送进 WebView
|
|
43
|
+
|
|
44
|
+
调用过程是:
|
|
45
|
+
|
|
46
|
+
1. SDK 执行 `new Promise`,生成 `cb_N` 这种 id,放进待回调表。
|
|
47
|
+
2. 把 `{ id, method, params }` JSON 交给 `AndroidBridge.postMessage`、`webkit.messageHandlers.IOSBridge` 或 `HarmonyBridge.postMessage`。这段脚本和 `ota-native-bridge.js` 本身只用 ES5,没有 `async`/`await` 关键字。
|
|
48
|
+
3. 原生处理完后调用 `window.__nativeBridgeCallback(id, success, payload)`。`success` 为真则 resolve,否则 reject。
|
|
49
|
+
|
|
50
|
+
这条回调不检查调用方是 `.then()` 还是 `await`。引擎只要能解析 `async`/`await`,`await App.getInfo()` 会停在同一个 Promise 上,直到同一次 `__nativeBridgeCallback`。Crypto、定位、播放器、权限弹窗都走这个 id,没有第二套「只能用 callback、await 会丢结果」的通道。
|
|
51
|
+
|
|
52
|
+
文档要求源码不要把 `async`/`await` 送进 WebView,是因为 **旧引擎解析不了这个语法**,不是因为桥的回调模型有限制:
|
|
53
|
+
|
|
54
|
+
| 环境 | `async`/`await` |
|
|
55
|
+
|------|-----------------|
|
|
56
|
+
| Android System WebView,Chromium 55 之前(大约 Android 7 以前的系统 WebView) | 解析 `async function` 直接 `SyntaxError`,后面的桥调用不会执行 |
|
|
57
|
+
| 更老的 WKWebView(iOS 10.3 / Safari 10.1 之前) | 同样在解析阶段失败 |
|
|
58
|
+
| 当前三端壳的系统 WebView | 语法本身可用 |
|
|
59
|
+
|
|
60
|
+
失败发生在脚本加载时,和这次调用是否返回 Promise 无关。页面里只要还有未转译的 `async function`,旧 WebView 整段脚本都起不来。
|
|
61
|
+
|
|
62
|
+
构建器(Vue / Vite 等,目标包含这些旧 WebView)已经把 `async`/`await` 收成 `Promise` 和 `.then` 时,进包的代码里没有这个关键字,可以调用本 SDK。业务仓库里源码写 `async` 没有问题,只要产物里不保留该语法。文档后面的示例有时写成 `await`,是为了短,不要把那段原样放进未转译的 HTML。
|
|
63
|
+
|
|
64
|
+
壳注入的运行时脚本(安全区、视口单位)同样保持 ES5,不使用 `async`/`await`,也不使用 `new Event()`、`{ once: true }` 这类旧 WebView 没有的写法。这和 H5 业务包是否转译是两件事。
|
|
65
|
+
|
|
42
66
|
### 常用 API 一览
|
|
43
67
|
|
|
44
68
|
| 做什么 | 写法 |
|
|
@@ -299,19 +323,47 @@ export function useNativeApp() {
|
|
|
299
323
|
|
|
300
324
|
**2. 安全区:头部沉浸式 + 底部原生收缩**
|
|
301
325
|
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
326
|
+
默认 `app_config.json` 里 `safeArea.enableTop` 与 `enableBottom` 都是 `true`。改开关用 `Config.setNativeFeatures`,改完壳会重算布局并再注入 CSS 变量。只重读当前窗口、不改开关时用 `Viewport.refreshSafeArea()`。
|
|
327
|
+
|
|
328
|
+
三端都把状态栏留在画面上(透明或盖在内容上),都把 `--status-bar-height` 写成真实高度。底栏是 H5 自己画的,壳不占 `--window-bottom`。差异在「谁来留白、CSS inset 什么时候变成 0」。
|
|
329
|
+
|
|
330
|
+
**头部**
|
|
331
|
+
|
|
332
|
+
| 端 | `enableTop=true`(默认) | `enableTop=false` |
|
|
333
|
+
|----|--------------------------|-------------------|
|
|
334
|
+
| Android | WebView 顶边沉浸到状态栏下。未适配的整段 navbar 由壳加 `padding-top`(已有顶距的不重复垫)。头部被判定为贴顶近全宽的真实视频时,本页取消沉浸,改为原生顶 margin + 色条 | 该方向不再自动垫,由页面用 `env(safe-area-inset-*)` 或注入变量自己留 |
|
|
335
|
+
| iOS | WebView 顶边贴在控制器顶边,画到状态栏下面。顶部原生色条固定隐藏。壳**不会**给 navbar 加 padding,注入时还会清掉 `data-open-top-ctrl`。顶栏必须自己写 `padding-top: var(--status-bar-height)` | 原生顶边布局不变,WebView 仍然贴顶。`--status-bar-height` 和顶部 inset **仍然是真实值**,不会变成 0。`window.__SAFE_AREA__.nativeApplyTop` 只反映开关,不表示顶部已经被原生吃掉 |
|
|
336
|
+
| HarmonyOS | 顶部色条高度固定为 0(`topInsetVp = 0`),Web 顶边沉浸。壳不自动改 navbar。顶栏自己 `padding-top: var(--status-bar-height)` | 顶部色条高度仍是 0,布局与开启时相同。顶部 CSS 变量仍是真实状态栏高度 |
|
|
337
|
+
|
|
338
|
+
iOS 的逐步约束、`__SAFE_AREA__` 字段和窗口底部填充见 [OpenIOSPackage README — 安全区](../../OpenIOSPackage/README.md#安全区)。HarmonyOS 对照 [OpenHarmonyPackage README — 安全区](../../OpenHarmonyPackage/README.md#安全区)。Android 的 OEM 沉浸式和视频头取消沉浸见 [OpenAndroidPackage README — 沉浸式与安全区](../../OpenAndroidPackage/README.md#沉浸式与安全区)。
|
|
339
|
+
|
|
340
|
+
**底部**
|
|
305
341
|
|
|
306
|
-
|
|
342
|
+
`enableBottom=true` 时,iOS 与 HarmonyOS 缩短 Web 区域再铺一条底色,注入的 `--safe-area-inset-bottom` 为 `0`。Android 不缩短 WebView:导航栏透明,注入真实底部 inset,并把页面里的 `env(safe-area-inset-*)` 改写成这个变量,让 TabBar 自己画进小白条,和描述签一样。
|
|
307
343
|
|
|
308
|
-
|
|
344
|
+
| 端 | 缩短多少 | 左右 inset | 底色未配置时 |
|
|
345
|
+
|----|----------|------------|----------------|
|
|
346
|
+
| Android | 不缩短。WebView 铺到屏幕底,导航栏透明。`--safe-area-inset-bottom` 为真实系统 inset,页面 `env(safe-area-inset-bottom)` 会被改写成这个变量 | 同样上报真实左右 inset | 不另铺色条。窗口兜底色留空为 `#010114` |
|
|
347
|
+
| iOS | **整段** Home 指示条高度(`safeAreaInsets.bottom`)。另外在窗口最底层铺一块同样高度的填充,避免圆角或转场露出窗口底色 | `enableBottom=true` 时左右约束收到 `safeAreaLayoutGuide`,`--safe-area-inset-left/right` 也是 `0` | 固定 `#010114`,不用主题背景色。同时给 `html`、`body`、`#app`、`.van-tabbar` 写这个背景,只改背景,不改 TabBar 的 `bottom` |
|
|
348
|
+
| HarmonyOS | 系统底部 inset(`px2vp(insets.bottom)`),Web 在色条之上 | `enableBottom=true` 时左右 CSS inset 置 `0` | `theme.backgroundColor`,主题也空则 `#FFFFFF` |
|
|
349
|
+
|
|
350
|
+
iOS、HarmonyOS 默认开关下底栏写 `bottom: 0` 就已经停在系统底栏上沿。再写 `bottom: 10px`,或再加 `env(safe-area-inset-bottom)`,会在壳已经让出的区域上面又空一截。Android 不让出这块区域,底栏要和描述签一样自己包含 `safe-area-inset-bottom`,小白条才会叠在 TabBar 背景上。
|
|
351
|
+
|
|
352
|
+
`enableBottom=false` 时不放底部色条,Web 铺到屏幕底边,底部(以及 iOS / Harmony 的左右)CSS 变量改为真实 inset,由页面自己留白。
|
|
353
|
+
|
|
354
|
+
顶栏与底栏示例(默认两个开关都开着):
|
|
309
355
|
|
|
310
356
|
```css
|
|
311
357
|
.header {
|
|
312
358
|
padding-top: var(--status-bar-height);
|
|
313
359
|
background: #6b2d8b;
|
|
314
360
|
}
|
|
361
|
+
.tabbar {
|
|
362
|
+
position: fixed;
|
|
363
|
+
left: 0;
|
|
364
|
+
right: 0;
|
|
365
|
+
bottom: 0;
|
|
366
|
+
}
|
|
315
367
|
```
|
|
316
368
|
|
|
317
369
|
**3. 安全区背景色(前端可配)**
|
|
@@ -327,14 +379,16 @@ export function useNativeApp() {
|
|
|
327
379
|
}
|
|
328
380
|
```
|
|
329
381
|
|
|
330
|
-
- `topBackgroundColor`:头部安全区 /
|
|
331
|
-
- `bottomBackgroundColor
|
|
332
|
-
-
|
|
382
|
+
- `topBackgroundColor`:头部安全区 / 状态栏色条。Android 留空回退主题 `backgroundColor`。iOS 默认不画顶部色条,这个字段不改变「顶边贴屏幕顶」的布局。HarmonyOS 顶部色条高度为 0,颜色主要作用于状态栏内容区背景注入(`statusBarBackground`)。
|
|
383
|
+
- `bottomBackgroundColor`:底部色条。Android 不铺这条色条,留空时只作窗口兜底色 `#010114`。HarmonyOS 回退主题 `backgroundColor`(再空则 `#FFFFFF`);iOS 回退 `#010114`。
|
|
384
|
+
- 状态栏图标明暗由 `theme.statusBarStyle`(`light` / `dark`)控制,和色条开关无关。三端都没有「隐藏状态栏」的字段。
|
|
333
385
|
|
|
334
|
-
|
|
386
|
+
运行时(见 §7.2 `Config.setNativeFeatures`):
|
|
335
387
|
|
|
336
388
|
```typescript
|
|
337
389
|
Config.setNativeFeatures({
|
|
390
|
+
safeAreaEnableTop: true,
|
|
391
|
+
safeAreaEnableBottom: true,
|
|
338
392
|
safeAreaTopBackgroundColor: '#2196F3',
|
|
339
393
|
safeAreaBottomBackgroundColor: '#FFFFFF',
|
|
340
394
|
});
|
|
@@ -356,19 +410,23 @@ Config.setNativeFeatures({
|
|
|
356
410
|
|
|
357
411
|
**5. 关闭壳内安全区(纯前端自管时)**
|
|
358
412
|
|
|
359
|
-
关闭对应方向后,该方向不再自动适配(头部不再 `nativeApplyTop`,底部不再 margin 收缩),由前端自行用 `env(safe-area-inset-*)` / CSS 变量适配:
|
|
360
|
-
|
|
361
413
|
```json
|
|
362
414
|
"safeArea": { "enableTop": false, "enableBottom": false }
|
|
363
415
|
```
|
|
364
416
|
|
|
417
|
+
- `enableBottom: false`:三端都不再缩短 Web 区域,底部 inset 注入真实值,页面用这些变量或 `env(safe-area-inset-bottom)` 留白。
|
|
418
|
+
- `enableTop: false`:Android 停止自动垫顶栏,顶部交给页面。iOS、HarmonyOS 的顶边布局本来就不随这个开关缩进,关掉之后顶栏仍然要写 `padding-top: var(--status-bar-height)`,顶部变量不会变成 0。
|
|
419
|
+
|
|
365
420
|
**6. 读取安全区(脚本)**
|
|
366
421
|
|
|
367
422
|
```typescript
|
|
368
423
|
Viewport.getSafeAreaInsets().then((insets) => console.log(insets));
|
|
369
|
-
// 或 window.__SAFE_AREA__(壳注入;enableTop 时 top/statusBarHeight 为真实值)
|
|
370
424
|
```
|
|
371
425
|
|
|
426
|
+
也可以直接读壳注入的 `window.__SAFE_AREA__`。默认 `enableBottom=true` 时 `bottom` 为 `0`(原生已经缩短 Web 区域)。`top` 和 `statusBarHeight` 在三端都是真实顶部高度。iOS 另有 `nativeApplyTop`、`nativeApplyBottom`:前者为 `true` 只表示配置开着,顶部 inset 并没有被写成 0。
|
|
427
|
+
|
|
428
|
+
`resize` 和 `orientationchange` 会用上一帧度量重写 `--vh` 等视口单位。旋转之后的顶部/底部 inset 要等原生再次注入(布局变化,或页面调用 `Viewport.refreshSafeArea()`)才会更新。从后台回到前台不会自动重算。
|
|
429
|
+
|
|
372
430
|
**7. 调试**
|
|
373
431
|
|
|
374
432
|
Android:`chrome://inspect` 连接 WebView,对比变量与 Chrome DevTools 手机模拟器。
|
|
@@ -377,16 +435,19 @@ Android:`chrome://inspect` 连接 WebView,对比变量与 Chrome DevTools
|
|
|
377
435
|
|
|
378
436
|
壳在 **文档最早阶段**(`WebViewCompat.addDocumentStartJavaScript`)注入 CSS 变量,前端可直接用于安全区与视口适配:
|
|
379
437
|
|
|
380
|
-
>
|
|
438
|
+
> `enableBottom=true` 时底部 inset 为 `0`(Web 区域已被原生缩短)。三端在这个开关下左右 inset 也是 `0`(`sideNative` 跟 `enableBottom` 走)。顶部 inset 和 `--status-bar-height` 在三端都是真实状态栏高度,iOS / HarmonyOS 不因 `enableTop` 把它们写成 0。
|
|
381
439
|
|
|
382
440
|
| CSS 变量 | 含义(App 全屏 WebView) |
|
|
383
441
|
|----------|-------------------------|
|
|
384
|
-
| `--status-bar-height` |
|
|
442
|
+
| `--status-bar-height` | 状态栏高度,三端均为真实值 |
|
|
385
443
|
| `--window-top` / `--window-bottom` | 原生导航/TabBar 占位(本壳为 0,TabBar 由 H5 自绘) |
|
|
386
|
-
| `--safe-area-inset-top
|
|
387
|
-
| `--safe-area-
|
|
388
|
-
| `--
|
|
389
|
-
| `--
|
|
444
|
+
| `--safe-area-inset-top` | 顶部 inset,真实值 |
|
|
445
|
+
| `--safe-area-inset-bottom` | `enableBottom=true` 时为 `0`,否则为真实底部 inset |
|
|
446
|
+
| `--safe-area-inset-left` / `--safe-area-inset-right` | `enableBottom=true` 时三端都为 `0`,否则为真实左右 inset |
|
|
447
|
+
| `--safe-area-top` / `--safe-area-bottom` | 与对应 inset 同值 |
|
|
448
|
+
| `--vh` / `--dvh` / `--svh` / `--lvh` | 视口高度的 1%,四者同值;`100vh/100dvh` 在旧引擎由壳改写为 `calc(var(--unit)*100)` |
|
|
449
|
+
| `--vw` / `--dvw` / `--svw` / `--lvw` | 视口宽度的 1%,四者同值 |
|
|
450
|
+
| `--webview-inner-height` / `--webview-inner-width` | 当前 Web 可视宽高(像素) |
|
|
390
451
|
|
|
391
452
|
```css
|
|
392
453
|
/* 顶部状态栏占位 */
|
|
@@ -441,13 +502,13 @@ import { SDK_VERSION, Native } from 'ota-native-bridge';
|
|
|
441
502
|
// Native.sdkVersion 或 SDK_VERSION
|
|
442
503
|
```
|
|
443
504
|
|
|
444
|
-
|
|
505
|
+
`SDK_VERSION` 与 `Native.sdkVersion` 返回的是 npm 包版本,即 `package.json` 的 `version`。OTA 请求里上报的 `sdkVersion` 取自三端壳的 `OtaConstants`,是协议版本,与 npm 包版本号不同,不要混用。
|
|
445
506
|
|
|
446
507
|
---
|
|
447
508
|
|
|
448
509
|
## 5. OTA 更新(核心)
|
|
449
510
|
|
|
450
|
-
OTA 行为分两层:`platform.otaEnabled`(`platformId` + `apiKey` + `paramPublicKey` 已齐,Client API 可用)与 **`app_config.json` → `update.useCustomUpdate`**(谁负责更新 UI)。前端通过 `App.getInfo()`
|
|
511
|
+
OTA 行为分两层:`platform.otaEnabled`(`platformId` + `apiKey` + `paramPublicKey` 已齐,Client API 可用)与 **`app_config.json` → `update.useCustomUpdate`**(谁负责更新 UI)。前端通过 `App.getInfo()` 读取;普通原生更新 = `otaEnabled && !useCustomUpdate`。管理后台「壳更新推送」(`shellNative=1`)无视 `useCustomUpdate`,由壳用原生弹窗完成更新。
|
|
451
512
|
|
|
452
513
|
### 5.1 原生更新模式(默认)
|
|
453
514
|
|
|
@@ -792,7 +853,7 @@ iOS 对应字段:`apiBaseUrl`、`platformId`、`apiKey`、`autoCheckUpdateOnLa
|
|
|
792
853
|
| `platform.paramPublicKey` | 创建平台时生成的参数加密公钥(Client API / Stat 加密必填) |
|
|
793
854
|
| `update.useCustomUpdate` | 是否由前端完全接管更新流程 |
|
|
794
855
|
| `safeArea.enableTop` / `enableBottom` | 头部沉浸式适配 / 底部原生收缩(默认 `true`) |
|
|
795
|
-
| `safeArea.topBackgroundColor` / `bottomBackgroundColor` |
|
|
856
|
+
| `safeArea.topBackgroundColor` / `bottomBackgroundColor` | 头部色条留空回退 `theme.backgroundColor`。Android 底部不另铺色条,留空时窗口兜底为 `#010114`,小白条区域显示页面 TabBar |
|
|
796
857
|
| `theme` / `guide` 等 | 界面与功能开关(**开屏广告不在此配置**,由管理后台 OTA 下发,见 [OTA_PROTOCOL.md](../../docs/OTA_PROTOCOL.md)) |
|
|
797
858
|
| `desktopIcon` | 资源包内桌面图标相对路径(如 `icons/app-icon.png`)。Android 安装资源包后自动套用;Android 11+ 冷启动也会套用。留空则不自动换图标 |
|
|
798
859
|
|
|
@@ -815,7 +876,7 @@ iOS 对应字段:`apiBaseUrl`、`platformId`、`apiKey`、`autoCheckUpdateOnLa
|
|
|
815
876
|
|
|
816
877
|
以下按 **插件** 分组;均可通过 `Native.Plugins.*` 或直接 `import { App, Ota, Crypto, ... }` 调用。每个用法附**返回示例**。
|
|
817
878
|
|
|
818
|
-
>
|
|
879
|
+
> **约定:** API 返回 Promise,用 `.then()` / `.catch()` 或 callback。不要把 `async` / `await` 语法送进 WebView,原因见文首「为什么不要把 async/await 送进 WebView」。构建器已经转译过的代码可以调用这些 Promise。
|
|
819
880
|
|
|
820
881
|
### 7.1 连接与信息
|
|
821
882
|
|
|
@@ -1020,7 +1081,41 @@ Device.getOrientation().then(console.log);
|
|
|
1020
1081
|
}
|
|
1021
1082
|
```
|
|
1022
1083
|
|
|
1023
|
-
`orientation` 为 `portrait` / `landscape` / `unknown`;`rotation` 为 `0` / `90` / `180` / `270
|
|
1084
|
+
`orientation` 为 `portrait` / `landscape` / `unknown`;`rotation` 为 `0` / `90` / `180` / `270`。这是只读,改方向请用 `Device.setOrientation()`。页内横屏仍可用 CSS。`Media.playVideo` 打开的是另一层原生播放页,不会把当前 WebView 锁成横屏,关闭后也不会把 WebView 的方向改回去。
|
|
1085
|
+
|
|
1086
|
+
#### `Device.setOrientation(options)`(包版本 **1.7.0+**)
|
|
1087
|
+
|
|
1088
|
+
```typescript
|
|
1089
|
+
await Device.setOrientation({ orientation: 'landscape' });
|
|
1090
|
+
await Device.setOrientation({ orientation: 'default' });
|
|
1091
|
+
```
|
|
1092
|
+
|
|
1093
|
+
`orientation` 可取:
|
|
1094
|
+
|
|
1095
|
+
- `portrait`:锁定竖屏。
|
|
1096
|
+
- `landscape`:锁定横屏。
|
|
1097
|
+
- `default`:恢复安装包允许的默认方向。Android 手机包为竖屏,平板包随传感器,电视包为横屏;iOS 为 `Info.plist` 允许的方向。
|
|
1098
|
+
|
|
1099
|
+
回调结果为 `{ success, orientation }`。设置只在本次运行期生效,应用重启后回到默认。桌面壳返回 `{ success: false, supported: false }`。
|
|
1100
|
+
|
|
1101
|
+
#### `Device.setFullscreen(options)`(包版本 **1.7.0+**)
|
|
1102
|
+
|
|
1103
|
+
```typescript
|
|
1104
|
+
await Device.setFullscreen({ enabled: true });
|
|
1105
|
+
await Device.setFullscreen({ enabled: false });
|
|
1106
|
+
```
|
|
1107
|
+
|
|
1108
|
+
- Android:隐藏状态栏和导航栏,用户从边缘滑动时临时呼出。
|
|
1109
|
+
- iOS:隐藏状态栏和 Home 指示条。刘海或灵动岛区域的顶部安全区仍由系统给出。
|
|
1110
|
+
- Harmony:隐藏状态栏和导航栏。
|
|
1111
|
+
- 桌面:窗口进入系统全屏。
|
|
1112
|
+
|
|
1113
|
+
回调结果为 `{ success, fullscreen }`。状态在应用运行期间保持,页面刷新不会改变它,应用重启后回到非全屏。网页自己的全屏(见下)结束后,系统栏和方向回到最后一次 `Device.setFullscreen` / `Device.setOrientation` 的设置。
|
|
1114
|
+
|
|
1115
|
+
页面也可以继续用浏览器接口,不必改成 `Device.*`:
|
|
1116
|
+
|
|
1117
|
+
- `element.requestFullscreen()` 和 `<video>.webkitEnterFullscreen()`:Android 把 WebView 交来的画面铺满 Activity,并隐藏状态栏和导航栏;返回键先退出这次全屏。iOS 打开元素全屏。Harmony 在进入网页全屏时隐藏系统栏。桌面窗口跟着进入系统全屏。
|
|
1118
|
+
- `screen.orientation.lock("portrait" | "landscape" | …)` 和 `screen.orientation.unlock()`:Android、iOS、Harmony 转到壳的 `setOrientation`。手机包清单里的竖屏不会再拦住这个调用。桌面没有屏幕方向,调用会失败。
|
|
1024
1119
|
|
|
1025
1120
|
#### `Device.getLockStatus()`
|
|
1026
1121
|
|
|
@@ -1517,7 +1612,35 @@ Http.request({
|
|
|
1517
1612
|
|
|
1518
1613
|
### 7.11 设备能力(剪贴板 / 分享 / 相机 / 扫码 / 生物识别等)
|
|
1519
1614
|
|
|
1520
|
-
|
|
1615
|
+
调用方式见文首:用 `.then()` 或 callback;构建器已转译的 `async` 可以。系统权限弹窗由壳处理。包版本 **1.2.0+**。
|
|
1616
|
+
|
|
1617
|
+
下面这些**不是**本表里的插件,壳也没有对应方法或 `window` 事件。播放页、弹窗、底栏不要向壳注册拦截。各端返回键的具体走法见壳 README「壳没有交给 H5 的能力」([Android](../../OpenAndroidPackage/README.md#壳没有交给-h5-的能力)、[iOS](../../OpenIOSPackage/README.md#壳没有交给-h5-的能力)、[HarmonyOS](../../OpenHarmonyPackage/README.md#壳没有交给-h5-的能力))。
|
|
1618
|
+
|
|
1619
|
+
**返回键 / 侧滑**
|
|
1620
|
+
|
|
1621
|
+
- Android:`MainActivity.onBackPressed` 里 WebView 还能后退就 `goBack()`,否则结束 Activity。返回键在原生层消费,页面收不到「即将返回」,不能先关弹窗再决定要不要后退。页内 `history.pushState` 的 `popstate` 只由前端历史触发,物理返回键不会先变成 `popstate`。
|
|
1622
|
+
- iOS:未打开 `allowsBackForwardNavigationGestures`。边缘侧滑不会触发 WebView 历史后退,也不会先通知页面。
|
|
1623
|
+
- HarmonyOS:`onBackPress` 里还能后退就 `backward()` 并消费事件,否则交给系统退出。页面同样没有机会先关弹窗。
|
|
1624
|
+
|
|
1625
|
+
**前后台**
|
|
1626
|
+
|
|
1627
|
+
`onResume` / `onPause`(Android)、`applicationDidBecomeActive`(iOS)、`onPageShow`(HarmonyOS)只服务 OTA 或通知外链,不向 JS 派发进入前台或进入后台。没有 `appShow` / `appHide`。页面只能用 WebView 自己的 `visibilitychange`、`pageshow`、`pagehide`。部分系统 WebView 在切到最近任务或分屏时不派发这些事件,壳没有补发。回到前台也不会自动 `refreshSafeArea`。
|
|
1628
|
+
|
|
1629
|
+
**锁定屏幕方向**
|
|
1630
|
+
|
|
1631
|
+
`Device.getOrientation()` 读取当前方向,返回见 §7.1。`Device.setOrientation({ orientation: 'portrait' | 'landscape' | 'default' })` 锁定竖屏、横屏或恢复默认;`Device.setFullscreen({ enabled })` 切换全屏,见 §7.1 之后的 `Device` 小节。
|
|
1632
|
+
|
|
1633
|
+
**键盘**
|
|
1634
|
+
|
|
1635
|
+
没有键盘高度,没有弹出/收起事件,没有 `--keyboard-height`。壳不会在键盘弹出时改 Web 底边约束,Home 条色条也不会跟着键盘上移。底部输入框用页面里的 `visualViewport`(`height`、`offsetTop`)或 `focusin` / `focusout`。
|
|
1636
|
+
|
|
1637
|
+
**隐藏状态栏**
|
|
1638
|
+
|
|
1639
|
+
`Theme.set` / `Config.setNativeFeatures` 的 `statusBarStyle` 只切换状态栏内容明暗:`light` 为浅色图标,`dark` 为深色图标。Android 把状态栏设为透明但仍占位可见;iOS 刷新 `preferredStatusBarStyle`,状态栏保持显示;HarmonyOS 把状态栏和导航栏设为透明(`#00000000`)并改内容颜色。三端都没有 `hideStatusBar` / `showStatusBar`。
|
|
1640
|
+
|
|
1641
|
+
Web 内的沉浸式播放,状态栏仍盖在画面顶部。需要系统级全屏时调用 `Media.playVideo`:那是独立的原生播放页(Android 播放页 / iOS `AVPlayerViewController` / HarmonyOS `VideoPlayerPage`),不是把当前 WebView 的状态栏隐藏掉。关闭播放页后,WebView 状态栏恢复为进入之前的明暗样式,仍然显示。
|
|
1642
|
+
|
|
1643
|
+
苹果描述签(Web Clip)不经过这三端壳,安全区变量和上面这些事件都不作用在描述签上。描述签的全屏与状态栏只认描述文件里的 `FullScreen`、`StatusBarStyle`,见 [苹果描述签开放接口](../../docs/APPLE_MOBILECONFIG_OPEN_API.md)。
|
|
1521
1644
|
|
|
1522
1645
|
```typescript
|
|
1523
1646
|
import {
|
|
@@ -1717,6 +1840,18 @@ A:原生模式下由壳处理下载,不会把 URL 交给 JS;自定义模
|
|
|
1717
1840
|
**Q:接口被 OTA 后台关闭返回 403?**
|
|
1718
1841
|
A:管理后台「公共接口」可单独关闭端点;与 Bridge 无关,检查服务端 `client_api_endpoint` 配置。
|
|
1719
1842
|
|
|
1843
|
+
**Q:业务代码里写了 async,会不会破坏桥回调?**
|
|
1844
|
+
A:不会。桥是 `new Promise`,结果由 `window.__nativeBridgeCallback(id, success, payload)` resolve。引擎能解析 `await` 时,`await App.getInfo()` 等的就是同一次回调。限制的是把未转译的 `async function` 送进旧 WebView:大约 Chrome 55 之前的 Android System WebView、iOS 10.3 之前的 WKWebView 会在加载时 `SyntaxError`,后面的桥调用都不会执行。构建器已经收成 `.then` 的产物可以照常调用。见文首「为什么不要把 async/await 送进 WebView」。
|
|
1845
|
+
|
|
1846
|
+
**Q:iOS 底栏离底还有一条缝,是不是壳留了 10px?**
|
|
1847
|
+
A:默认 `enableBottom=true` 时,iOS 壳让出的是整段 Home 指示条,注入的 `--safe-area-inset-bottom` 为 0,没有 10px 常量。底栏应 `bottom: 0`。再写 `10px` 或再加 `env(safe-area-inset-bottom)`,会在已经让出的区域上再空一截。iOS 顶栏也不会被壳自动加 padding,需要 `padding-top: var(--status-bar-height)`。
|
|
1848
|
+
|
|
1849
|
+
**Q:描述签全屏页贴底,删掉 apple-mobile-web-app-status-bar-style 有用吗?**
|
|
1850
|
+
A:没用。描述签是系统 Web Clip,不走 iOS 壳的安全区。已安装描述文件的视口只认那次打包写入的 `FullScreen` 和 `StatusBarStyle`。`IgnoreManifestScope` 只取消 manifest 的 `scope`,不改 `display`,也不读取页面上的状态栏 meta。要换视口,改 `fullScreen` 重新打包并让用户安装新描述文件。
|
|
1851
|
+
|
|
1852
|
+
**Q:弹窗想先自己关掉,再响应返回键 / 侧滑,怎么注册?**
|
|
1853
|
+
A:没有这个接口。Android 和 HarmonyOS 的返回键在原生层决定后退或退出;iOS 未打开边缘返回手势。页面只能处理自己 `pushState` 产生的 `popstate`。同样没有前后台回调、锁定方向、键盘高度、隐藏状态栏。见 §7.11 开头。
|
|
1854
|
+
|
|
1720
1855
|
**Q:TypeScript 提示找不到类型?**
|
|
1721
1856
|
A:执行 `npm run build` 生成 `dist/*.d.ts`,或确认依赖版本已包含类型文件。
|
|
1722
1857
|
|
package/ota-native-bridge.js
CHANGED
|
@@ -18,6 +18,9 @@
|
|
|
18
18
|
if (global.HarmonyBridge && global.HarmonyBridge.postMessage) {
|
|
19
19
|
return function (msg) { global.HarmonyBridge.postMessage(msg); };
|
|
20
20
|
}
|
|
21
|
+
if (global.DesktopBridge && global.DesktopBridge.postMessage) {
|
|
22
|
+
return function (msg) { global.DesktopBridge.postMessage(msg); };
|
|
23
|
+
}
|
|
21
24
|
return null;
|
|
22
25
|
}
|
|
23
26
|
|
|
@@ -34,7 +37,11 @@
|
|
|
34
37
|
});
|
|
35
38
|
}
|
|
36
39
|
|
|
40
|
+
var previousCallback = global.__nativeBridgeCallback;
|
|
37
41
|
global.__nativeBridgeCallback = function (id, success, payload) {
|
|
42
|
+
if (typeof previousCallback === 'function') {
|
|
43
|
+
previousCallback(id, success, payload);
|
|
44
|
+
}
|
|
38
45
|
var item = pending[id];
|
|
39
46
|
if (!item) return;
|
|
40
47
|
delete pending[id];
|
|
@@ -101,6 +108,9 @@
|
|
|
101
108
|
if (global.AndroidBridge && global.AndroidBridge.postMessage) return 'android';
|
|
102
109
|
if (global.webkit && global.webkit.messageHandlers && global.webkit.messageHandlers.IOSBridge) return 'ios';
|
|
103
110
|
if (global.HarmonyBridge && global.HarmonyBridge.postMessage) return 'harmony';
|
|
111
|
+
if (global.DesktopBridge && global.DesktopBridge.postMessage) {
|
|
112
|
+
return global.DesktopBridge.os === 'macos' ? 'macos' : 'windows';
|
|
113
|
+
}
|
|
104
114
|
return 'web';
|
|
105
115
|
}
|
|
106
116
|
|
|
@@ -121,6 +131,8 @@
|
|
|
121
131
|
getInfo: function (cb) { return withCallback(invokeNative('getDeviceInfo'), cb); },
|
|
122
132
|
getScreenInfo: function (cb) { return withCallback(invokeNative('getScreenInfo'), cb); },
|
|
123
133
|
getOrientation: function (cb) { return withCallback(invokeNative('getOrientation'), cb); },
|
|
134
|
+
setOrientation: function (opts, cb) { return withCallback(invokeNative('setOrientation', { orientation: opts && opts.orientation }), cb); },
|
|
135
|
+
setFullscreen: function (opts, cb) { return withCallback(invokeNative('setFullscreen', { enabled: !!(opts && opts.enabled) }), cb); },
|
|
124
136
|
getLockStatus: function (cb) { return withCallback(invokeNative('getLockStatus'), cb); },
|
|
125
137
|
getSystemInfo: function (cb) { return withCallback(invokeNative('getSystemInfo'), cb); },
|
|
126
138
|
getAndroidId: function (cb) { return withCallback(invokeNative('getAndroidId'), cb); },
|
|
@@ -372,7 +384,7 @@
|
|
|
372
384
|
};
|
|
373
385
|
|
|
374
386
|
global.Native = {
|
|
375
|
-
sdkVersion: '1.
|
|
387
|
+
sdkVersion: '1.7.0',
|
|
376
388
|
getPlatform: getPlatform,
|
|
377
389
|
isNativePlatform: isNativePlatform,
|
|
378
390
|
isNativeBridgeAvailable: function () { return !!getNativeMessenger(); },
|