-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBridge.swift
36 lines (28 loc) · 967 Bytes
/
Bridge.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
//
// Bridge.swift
// WKWebviewBridge
//
// Created by lu sitong on 2018/10/27.
// Copyright © 2018 lu sitong. All rights reserved.
//
import Foundation
import WebKit
public class Bridge: NSObject, WKScriptMessageHandler {
public typealias Callback = (WKScriptMessage) -> Void
var webView: WKWebView!
var callback: Callback!
public init(webView: WKWebView, callback: @escaping Callback) {
super.init()
self.webView = webView
self.callback = callback
}
public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
callback(message)
}
public func addHandler(_ name: String) {
webView.configuration.userContentController.add(self, name: name)
}
public func removeHandler(_ name: String) {
webView.configuration.userContentController.removeScriptMessageHandler(forName: name)
}
}