@bud-fe/h5-native-bridge 1.0.4 → 1.0.5

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 CHANGED
@@ -18,7 +18,6 @@ npm install h5-native-bridge
18
18
  ```
19
19
 
20
20
  ## 打包
21
-
22
21
  ```bash
23
22
  npm run build
24
23
  ```
@@ -31,14 +30,13 @@ npm run build
31
30
  import nativeBridge from 'h5-native-bridge';
32
31
 
33
32
  // 获取位置信息
34
- nativeBridge
35
- .getLocation({
36
- enableHighAccuracy: true,
37
- })
38
- .then((location) => {
33
+ nativeBridge.getLocation({
34
+ enableHighAccuracy: true
35
+ })
36
+ .then(location => {
39
37
  console.log('✅ 位置信息:', location);
40
38
  })
41
- .catch((error) => {
39
+ .catch(error => {
42
40
  console.error('获取位置失败:', error);
43
41
  });
44
42
  ```
@@ -58,36 +56,33 @@ nativeBridge.setDebug(true);
58
56
  import nativeBridge from 'h5-native-bridge';
59
57
 
60
58
  // 获取位置信息
61
- nativeBridge
62
- .getLocation({
63
- enableHighAccuracy: true,
64
- })
65
- .then((location) => {
59
+ nativeBridge.getLocation({
60
+ enableHighAccuracy: true
61
+ })
62
+ .then(location => {
66
63
  console.log('✅ 位置信息:', location);
67
64
  })
68
- .catch((error) => {
65
+ .catch(error => {
69
66
  console.error('获取位置失败:', error);
70
67
  });
71
68
 
72
69
  // 获取WiFi列表
73
- nativeBridge
74
- .getWifiList()
75
- .then((deviceInfo) => {
70
+ nativeBridge.getWifiList()
71
+ .then(deviceInfo => {
76
72
  console.log('设备信息:', deviceInfo);
77
73
  })
78
- .catch((error) => {
74
+ .catch(error => {
79
75
  console.error('获取WiFi失败:', error);
80
76
  });
81
77
 
82
78
  // 保存图片
83
- nativeBridge
84
- .saveImage({
85
- url: 'https://p3-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/ecbfab94b77e4c73a9635f4639bf41fd~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg5Y-q5oOz6Lq65bmz5LiN5oOz5Yqq5Yqb:q75.awebp?rk3s=f64ab15b&x-expires=1747911373&x-signature=bqy63dhMBpzZuILHRSVE37RuRLM%3D',
86
- })
87
- .then((deviceInfo) => {
79
+ nativeBridge.saveImage({
80
+ url: "https://p3-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/ecbfab94b77e4c73a9635f4639bf41fd~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg5Y-q5oOz6Lq65bmz5LiN5oOz5Yqq5Yqb:q75.awebp?rk3s=f64ab15b&x-expires=1747911373&x-signature=bqy63dhMBpzZuILHRSVE37RuRLM%3D"
81
+ })
82
+ .then(deviceInfo => {
88
83
  console.log('图片信息:', deviceInfo);
89
84
  })
90
- .catch((error) => {
85
+ .catch(error => {
91
86
  console.error('图片保存失败:', error);
92
87
  });
93
88
  ```
@@ -172,37 +167,37 @@ public class MainActivity extends AppCompatActivity {
172
167
 
173
168
  ### iOS端
174
169
 
175
- ````swift
170
+ ```swift
176
171
  // ViewController.swift
177
172
  import UIKit
178
173
  import WebKit
179
174
 
180
175
  class ViewController: UIViewController, WKScriptMessageHandler, WKNavigationDelegate {
181
-
176
+
182
177
  var webView: WKWebView!
183
-
178
+
184
179
  override func viewDidLoad() {
185
180
  super.viewDidLoad()
186
-
181
+
187
182
  // 配置WKWebView
188
183
  let configuration = WKWebViewConfiguration()
189
184
  let userContentController = WKUserContentController()
190
-
185
+
191
186
  // 注册JS消息处理,名称为 "nativeBridge"
192
187
  userContentController.add(self, name: "nativeBridge")
193
188
  configuration.userContentController = userContentController
194
-
189
+
195
190
  // 创建WKWebView
196
191
  webView = WKWebView(frame: view.bounds, configuration: configuration)
197
192
  webView.navigationDelegate = self
198
193
  view.addSubview(webView)
199
-
194
+
200
195
  // 加载H5页面
201
196
  if let url = URL(string: "https://your-app-url.com") {
202
197
  webView.load(URLRequest(url: url))
203
198
  }
204
199
  }
205
-
200
+
206
201
  // 处理来自JS的消息
207
202
  func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
208
203
  if message.name == "nativeBridge", let messageBody = message.body as? String {
@@ -213,7 +208,7 @@ class ViewController: UIViewController, WKScriptMessageHandler, WKNavigationDele
213
208
  let action = jsonMessage["action"] as? String,
214
209
  let params = jsonMessage["params"] as? [String: Any],
215
210
  let callbackId = jsonMessage["callbackId"] as? String {
216
-
211
+
217
212
  // 根据action处理不同的请求
218
213
  switch action {
219
214
  case "getLocation":
@@ -244,7 +239,7 @@ class ViewController: UIViewController, WKScriptMessageHandler, WKNavigationDele
244
239
  }
245
240
  }
246
241
  }
247
-
242
+
248
243
  // 处理获取位置请求
249
244
  private func handleGetLocation(callbackId: String, params: [String: Any]) {
250
245
  // 获取定位权限并执行定位
@@ -256,7 +251,7 @@ class ViewController: UIViewController, WKScriptMessageHandler, WKNavigationDele
256
251
  "timestamp": Date().timeIntervalSince1970 * 1000,
257
252
  "address": "上海市某某路123号"
258
253
  ]
259
-
254
+
260
255
  // 调用JavaScript回调
261
256
  let script = "window.nativeBridgeCallback('\(callbackId)', 'success', \(toJSONString(location)))"
262
257
  webView.evaluateJavaScript(script) { _, error in
@@ -265,7 +260,7 @@ class ViewController: UIViewController, WKScriptMessageHandler, WKNavigationDele
265
260
  }
266
261
  }
267
262
  }
268
-
263
+
269
264
  // 处理导航请求
270
265
  private func handleNavigate(callbackId: String, params: [String: Any]) {
271
266
  // 实现导航逻辑,例如跳转到指定页面
@@ -273,7 +268,7 @@ class ViewController: UIViewController, WKScriptMessageHandler, WKNavigationDele
273
268
  let script = "window.nativeBridgeCallback('\(callbackId)', 'success', \(toJSONString(response)))"
274
269
  webView.evaluateJavaScript(script, completionHandler: nil)
275
270
  }
276
-
271
+
277
272
  // 处理保存图片请求
278
273
  private func handleSaveImage(callbackId: String, params: [String: Any]) {
279
274
  // 实现图片保存逻辑
@@ -281,7 +276,7 @@ class ViewController: UIViewController, WKScriptMessageHandler, WKNavigationDele
281
276
  let script = "window.nativeBridgeCallback('\(callbackId)', 'success', \(toJSONString(response)))"
282
277
  webView.evaluateJavaScript(script, completionHandler: nil)
283
278
  }
284
-
279
+
285
280
  // 处理获取设备信息请求
286
281
  private func handleGetDeviceInfo(callbackId: String, params: [String: Any]) {
287
282
  // 实现设备信息获取逻辑
@@ -300,7 +295,7 @@ class ViewController: UIViewController, WKScriptMessageHandler, WKNavigationDele
300
295
  let script = "window.nativeBridgeCallback('\(callbackId)', 'success', \(toJSONString(deviceInfo)))"
301
296
  webView.evaluateJavaScript(script, completionHandler: nil)
302
297
  }
303
-
298
+
304
299
  // 处理获取WiFi列表请求
305
300
  private func handleGetWifiList(callbackId: String, params: [String: Any]) {
306
301
  // 实现WiFi列表获取逻辑
@@ -316,7 +311,7 @@ class ViewController: UIViewController, WKScriptMessageHandler, WKNavigationDele
316
311
  let script = "window.nativeBridgeCallback('\(callbackId)', 'success', \(toJSONString(response)))"
317
312
  webView.evaluateJavaScript(script, completionHandler: nil)
318
313
  }
319
-
314
+
320
315
  // 辅助函数:将字典转为JSON字符串
321
316
  private func toJSONString(_ dict: [String: Any]) -> String {
322
317
  if let data = try? JSONSerialization.data(withJSONObject: dict),
@@ -341,7 +336,7 @@ npm run build
341
336
 
342
337
  # 运行测试
343
338
  npm run test
344
- ````
339
+ ```
345
340
 
346
341
  ## 许可证
347
342