-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebViewAlertPlugin.swift
68 lines (54 loc) · 2.05 KB
/
WebViewAlertPlugin.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// WebViewAlertPlugin.swift
// HoloWebViewBridge_Example
//
// Created by 与佳期 on 2021/1/24.
// Copyright © 2021 CocoaPods. All rights reserved.
//
import Foundation
import HoloWebViewBridge
class WebViewAlertPlugin: WebViewPluginProtocol {
deinit {
print("WebViewAlertPlugin deinit")
}
// MARK: - AlertPlugin method
func alert(_ msg: String) {
let alertVC = UIAlertController(title: msg, message: nil, preferredStyle: .alert)
let action = UIAlertAction(title: "Confirm", style: .cancel, handler: nil)
alertVC.addAction(action)
UIApplication.shared.keyWindow?.rootViewController?.present(alertVC, animated: true, completion: nil)
}
func alertConfirm(_ msg: String, responseHandler: ResponseHandler?) {
let alertVC = UIAlertController(title: msg, message: nil, preferredStyle: .alert)
let action = UIAlertAction(title: "Confirm", style: .cancel) { (action) in
responseHandler?("confirm")
}
alertVC.addAction(action)
UIApplication.shared.keyWindow?.rootViewController?.present(alertVC, animated: true, completion: nil)
}
// MARK: - WebViewPluginProtocol
public var identifier: String {
return "holo.webview.bridge.example.alert"
}
public var javascript: String {
if let path = Bundle(for: WebViewAlertPlugin.self).path(forResource: "alert", ofType: "js"),
let js = try? String(contentsOfFile: path, encoding: .utf8) {
return js
}
return ""
}
func didReceiveMessage(_ fun: String, args: Any?) {
if fun == "alert()" {
if let msg = args as? String {
self.alert(msg)
}
return
}
if fun == "alert(confirm)" {
if let args = args as? [Any], let msg = args.first as? String, let handler = args.last as? ResponseHandler {
self.alertConfirm(msg, responseHandler: handler)
}
return
}
}
}