update web view with default call handler
This commit is contained in:
parent
1681048279
commit
bfe706908f
@ -10,7 +10,9 @@ import UIKit
|
|||||||
import WebKit
|
import WebKit
|
||||||
|
|
||||||
@objcMembers open class WebView: View, MVMCoreUIViewConstrainingProtocol {
|
@objcMembers open class WebView: View, MVMCoreUIViewConstrainingProtocol {
|
||||||
|
|
||||||
|
let mvmWebViewMessageHandler = "mvmWebViewMessageHandler"
|
||||||
|
|
||||||
public let loadingSpinner = MFLoadingSpinner(frame: .zero)
|
public let loadingSpinner = MFLoadingSpinner(frame: .zero)
|
||||||
|
|
||||||
var delegateObject: MVMCoreUIDelegateObject?
|
var delegateObject: MVMCoreUIDelegateObject?
|
||||||
@ -25,7 +27,7 @@ import WebKit
|
|||||||
|
|
||||||
override open func setupView() {
|
override open func setupView() {
|
||||||
super.setupView()
|
super.setupView()
|
||||||
let webView = createWebView(messageHandler: nil, jsScript: nil)
|
let webView = createWebView(messageHandler: mvmWebViewMessageHandler, jsScript: nil)
|
||||||
addSubview(webView)
|
addSubview(webView)
|
||||||
NSLayoutConstraint.constraintPinSubview(toSuperview: webView)
|
NSLayoutConstraint.constraintPinSubview(toSuperview: webView)
|
||||||
self.webView = webView
|
self.webView = webView
|
||||||
@ -112,24 +114,16 @@ extension WebView : WKUIDelegate {
|
|||||||
if !dynamicHeight {
|
if !dynamicHeight {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if let _ = webviewModel?.jsScript {
|
|
||||||
/* evaluateJavaScript only works when webview contains userscrpt.
|
/* was using "document.readyState" to check the state, while evaluateJavaScript "document.readyState",only works when webview contains userscrpt.otherwise, it would return WKErrorDomain Code=4 "A JavaScript exception occurred".
|
||||||
otherwise, it would return WKErrorDomain Code=4 "A JavaScript exception occurred"
|
so webView.isLoading to check load finished state
|
||||||
*/
|
*/
|
||||||
webView.evaluateJavaScript("document.readyState", completionHandler: { [weak self] (result, error) in
|
if !webView.isLoading {
|
||||||
if result == nil || error != nil {
|
webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { [weak self] (result, error) in
|
||||||
return
|
guard let self = self, let height = result as? CGFloat else { return }
|
||||||
}
|
self.webViewHeight?.constant = height
|
||||||
webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { (result, error) in
|
self.delegateObject?.moleculeDelegate?.moleculeLayoutUpdated(self)
|
||||||
guard let height = result as? CGFloat else { return }
|
|
||||||
self?.webViewHeight?.constant = height
|
|
||||||
self?.delegateObject?.moleculeDelegate?.moleculeLayoutUpdated(self!)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
} else {
|
|
||||||
//get webview's content viewheight when no javescript setup
|
|
||||||
self.webViewHeight?.constant = webView.scrollView.contentSize.height
|
|
||||||
self.delegateObject?.moleculeDelegate?.moleculeLayoutUpdated(self)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -163,21 +157,14 @@ extension WebView : WKNavigationDelegate {
|
|||||||
// MARK: - WKScriptMessageHandler
|
// MARK: - WKScriptMessageHandler
|
||||||
extension WebView: WKScriptMessageHandler {
|
extension WebView: WKScriptMessageHandler {
|
||||||
public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
|
public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
|
||||||
if message.name == callHandlerName, let text = message.body as? String {
|
if message.name == mvmWebViewMessageHandler, let actionMap = message.body as? [AnyHashable: Any] {
|
||||||
/*
|
/*
|
||||||
receiving JavaScript func webkit.messageHandlers.{callHandler}.postMessage(body);
|
receiving JavaScript func webkit.messageHandlers.{callHandler}.postMessage(body);
|
||||||
if body is dictionary string
|
if body is dictionary
|
||||||
MVMCoreActionHanlder handleAction
|
MVMCoreActionHanlder handleAction
|
||||||
*/
|
*/
|
||||||
if let data = text.data(using: .utf8) {
|
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: nil, delegateObject: self.delegateObject)
|
||||||
do {
|
|
||||||
let actionMap = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
|
|
||||||
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: nil, delegateObject: self.delegateObject)
|
|
||||||
} catch {
|
|
||||||
// if post message is not dictionary string
|
|
||||||
print(error.localizedDescription)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user