update web view with default call handler
This commit is contained in:
parent
1681048279
commit
bfe706908f
@ -10,7 +10,9 @@ import UIKit
|
||||
import WebKit
|
||||
|
||||
@objcMembers open class WebView: View, MVMCoreUIViewConstrainingProtocol {
|
||||
|
||||
|
||||
let mvmWebViewMessageHandler = "mvmWebViewMessageHandler"
|
||||
|
||||
public let loadingSpinner = MFLoadingSpinner(frame: .zero)
|
||||
|
||||
var delegateObject: MVMCoreUIDelegateObject?
|
||||
@ -25,7 +27,7 @@ import WebKit
|
||||
|
||||
override open func setupView() {
|
||||
super.setupView()
|
||||
let webView = createWebView(messageHandler: nil, jsScript: nil)
|
||||
let webView = createWebView(messageHandler: mvmWebViewMessageHandler, jsScript: nil)
|
||||
addSubview(webView)
|
||||
NSLayoutConstraint.constraintPinSubview(toSuperview: webView)
|
||||
self.webView = webView
|
||||
@ -112,24 +114,16 @@ extension WebView : WKUIDelegate {
|
||||
if !dynamicHeight {
|
||||
return
|
||||
}
|
||||
if let _ = webviewModel?.jsScript {
|
||||
/* evaluateJavaScript only works when webview contains userscrpt.
|
||||
otherwise, it would return WKErrorDomain Code=4 "A JavaScript exception occurred"
|
||||
*/
|
||||
webView.evaluateJavaScript("document.readyState", completionHandler: { [weak self] (result, error) in
|
||||
if result == nil || error != nil {
|
||||
return
|
||||
}
|
||||
webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { (result, error) in
|
||||
guard let height = result as? CGFloat else { return }
|
||||
self?.webViewHeight?.constant = height
|
||||
self?.delegateObject?.moleculeDelegate?.moleculeLayoutUpdated(self!)
|
||||
})
|
||||
|
||||
/* 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".
|
||||
so webView.isLoading to check load finished state
|
||||
*/
|
||||
if !webView.isLoading {
|
||||
webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { [weak self] (result, error) in
|
||||
guard let self = self, 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
|
||||
extension WebView: WKScriptMessageHandler {
|
||||
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);
|
||||
if body is dictionary string
|
||||
if body is dictionary
|
||||
MVMCoreActionHanlder handleAction
|
||||
*/
|
||||
if let data = text.data(using: .utf8) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: nil, delegateObject: self.delegateObject)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user