Skip to content

Commit

Permalink
Fixed iOS userContentController didReceive WKScriptMessage event when…
Browse files Browse the repository at this point in the history
… using a WebView created with a windowId
  • Loading branch information
pichillilorenzo committed Jul 3, 2020
1 parent b8ec14d commit f7af313
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 29 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
## 4.0.0+3
## 4.0.0+4

- Reverted calling `handler.post` on Android when a WebView is created
- Fixed iOS extra bottom padding when opening the keyboard
- Fixed "Build for web not working – The integer literal 9223372036854775807 can't be represented exactly in JavaScript" [#429](https://github.com/pichillilorenzo/flutter_inappwebview/issues/429)
- Fixed iOS userContentController didReceive WKScriptMessage event when using a WebView created with a `windowId`

## 4.0.0

Expand Down
102 changes: 75 additions & 27 deletions ios/Classes/InAppWebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ if (window.Promise == null) {
let javaScriptBridgeJS = """
window.\(JAVASCRIPT_BRIDGE_NAME) = {};
window.\(JAVASCRIPT_BRIDGE_NAME).callHandler = function() {
var _windowId = window._flutter_inappwebview_windowId;
var _callHandlerID = setTimeout(function(){});
window.webkit.messageHandlers['callHandler'].postMessage( {'handlerName': arguments[0], '_callHandlerID': _callHandlerID, 'args': JSON.stringify(Array.prototype.slice.call(arguments, 1))} );
window.webkit.messageHandlers['callHandler'].postMessage( {'handlerName': arguments[0], '_callHandlerID': _callHandlerID, 'args': JSON.stringify(Array.prototype.slice.call(arguments, 1)), '_windowId': _windowId} );
return new Promise(function(resolve, reject) {
window.\(JAVASCRIPT_BRIDGE_NAME)[_callHandlerID] = resolve;
});
Expand All @@ -63,6 +64,7 @@ window.\(JAVASCRIPT_BRIDGE_NAME).callHandler = function() {
// the message needs to be concatenated with '' in order to have the same behavior like on Android
let consoleLogJS = """
(function(console) {
var oldLogs = {
'consoleLog': console.log,
'consoleDebug': console.debug,
Expand All @@ -83,7 +85,9 @@ let consoleLogJS = """
message += ' ' + arguments[i];
}
}
window.webkit.messageHandlers[oldLog].postMessage(message);
var _windowId = window._flutter_inappwebview_windowId;
window.webkit.messageHandlers[oldLog].postMessage({'message': message, '_windowId': _windowId});
oldLogs[oldLog].apply(null, arguments);
}
})(k);
Expand Down Expand Up @@ -141,12 +145,17 @@ function wkwebview_FindAllAsyncForElement(element, keyword) {
value.substr(idx + keyword.length)
);
var _windowId = window._flutter_inappwebview_windowId;
window.webkit.messageHandlers["onFindResultReceived"].postMessage(
JSON.stringify({
activeMatchOrdinal: wkwebview_CurrentHighlight,
numberOfMatches: wkwebview_SearchResultCount,
isDoneCounting: wkwebview_IsDoneCounting
})
{
'findResult': {
'activeMatchOrdinal': wkwebview_CurrentHighlight,
'numberOfMatches': wkwebview_SearchResultCount,
'isDoneCounting': wkwebview_IsDoneCounting
},
'_windowId': _windowId
}
);
}
} else if (element.nodeType == 1) {
Expand All @@ -171,12 +180,18 @@ function wkwebview_FindAllAsync(keyword) {
wkwebview_ClearMatches();
wkwebview_FindAllAsyncForElement(document.body, keyword.toLowerCase());
wkwebview_IsDoneCounting = true;
var _windowId = window._flutter_inappwebview_windowId;
window.webkit.messageHandlers["onFindResultReceived"].postMessage(
JSON.stringify({
activeMatchOrdinal: wkwebview_CurrentHighlight,
numberOfMatches: wkwebview_SearchResultCount,
isDoneCounting: wkwebview_IsDoneCounting
})
{
'findResult': {
'activeMatchOrdinal': wkwebview_CurrentHighlight,
'numberOfMatches': wkwebview_SearchResultCount,
'isDoneCounting': wkwebview_IsDoneCounting
},
'_windowId': _windowId
}
);
}
Expand Down Expand Up @@ -238,12 +253,17 @@ function wkwebview_FindNext(forward) {
block: "center"
});
var _windowId = window._flutter_inappwebview_windowId;
window.webkit.messageHandlers["onFindResultReceived"].postMessage(
JSON.stringify({
activeMatchOrdinal: wkwebview_CurrentHighlight,
numberOfMatches: wkwebview_SearchResultCount,
isDoneCounting: wkwebview_IsDoneCounting
})
{
'findResult': {
'activeMatchOrdinal': wkwebview_CurrentHighlight,
'numberOfMatches': wkwebview_SearchResultCount,
'isDoneCounting': wkwebview_IsDoneCounting
},
'_windowId': _windowId
}
);
}
}
Expand Down Expand Up @@ -1141,6 +1161,9 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
}
}

let userScript = WKUserScript(source: "window._flutter_inappwebview_windowId = \(windowId == nil ? "null" : String(windowId!));" , injectionTime: .atDocumentStart, forMainFrameOnly: false)
configuration.userContentController.addUserScript(userScript)

if windowId != nil {
// the new created window webview has the same WKWebViewConfiguration variable reference
return
Expand Down Expand Up @@ -2812,7 +2835,13 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
if let r = result {
json = r as! String
}
self.evaluateJavaScript("if(window.\(JAVASCRIPT_BRIDGE_NAME)[\(_callHandlerID)] != null) {window.\(JAVASCRIPT_BRIDGE_NAME)[\(_callHandlerID)](\(json)); delete window.\(JAVASCRIPT_BRIDGE_NAME)[\(_callHandlerID)];}", completionHandler: nil)

self.evaluateJavaScript("""
if(window.\(JAVASCRIPT_BRIDGE_NAME)[\(_callHandlerID)] != null) {
window.\(JAVASCRIPT_BRIDGE_NAME)[\(_callHandlerID)](\(json));
delete window.\(JAVASCRIPT_BRIDGE_NAME)[\(_callHandlerID)];
}
""", completionHandler: nil)
}
})
}
Expand Down Expand Up @@ -2905,24 +2934,43 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
messageLevel = 1
break;
}
onConsoleMessage(message: message.body as! String, messageLevel: messageLevel)
let body = message.body as! [String: Any?]
let consoleMessage = body["message"] as! String

let _windowId = body["_windowId"] as? Int64
var webView = self
if let wId = _windowId, let webViewTransport = InAppWebView.windowWebViews[wId] {
webView = webViewTransport.webView
}
webView.onConsoleMessage(message: consoleMessage, messageLevel: messageLevel)
} else if message.name == "callHandler" {
let body = message.body as! [String: Any]
let body = message.body as! [String: Any?]
let handlerName = body["handlerName"] as! String
if handlerName == "onPrint" {
printCurrentPage(printCompletionHandler: nil)
}
let _callHandlerID = body["_callHandlerID"] as! Int64
let args = body["args"] as! String
onCallJsHandler(handlerName: handlerName, _callHandlerID: _callHandlerID, args: args)

let _windowId = body["_windowId"] as? Int64
var webView = self
if let wId = _windowId, let webViewTransport = InAppWebView.windowWebViews[wId] {
webView = webViewTransport.webView
}
webView.onCallJsHandler(handlerName: handlerName, _callHandlerID: _callHandlerID, args: args)
} else if message.name == "onFindResultReceived" {
if let resource = convertToDictionary(text: message.body as! String) {
let activeMatchOrdinal = resource["activeMatchOrdinal"] as! Int
let numberOfMatches = resource["numberOfMatches"] as! Int
let isDoneCounting = resource["isDoneCounting"] as! Bool

self.onFindResultReceived(activeMatchOrdinal: activeMatchOrdinal, numberOfMatches: numberOfMatches, isDoneCounting: isDoneCounting)
let body = message.body as! [String: Any?]
let findResult = body["findResult"] as! [String: Any]
let activeMatchOrdinal = findResult["activeMatchOrdinal"] as! Int
let numberOfMatches = findResult["numberOfMatches"] as! Int
let isDoneCounting = findResult["isDoneCounting"] as! Bool

let _windowId = body["_windowId"] as? Int64
var webView = self
if let wId = _windowId, let webViewTransport = InAppWebView.windowWebViews[wId] {
webView = webViewTransport.webView
}
webView.onFindResultReceived(activeMatchOrdinal: activeMatchOrdinal, numberOfMatches: numberOfMatches, isDoneCounting: isDoneCounting)
}
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_inappwebview
description: A Flutter plugin that allows you to add an inline webview, to use an headless webview, and to open an in-app browser window.
version: 4.0.0+3
version: 4.0.0+4
homepage: https://github.com/pichillilorenzo/flutter_inappwebview

environment:
Expand Down

0 comments on commit f7af313

Please sign in to comment.