fix #1389 #1315 contextMenu ios 13

This commit is contained in:
Alexandre Richonnier 2023-03-10 11:03:00 +01:00
parent a42b0e4dfd
commit a7cda8d5b6
2 changed files with 50 additions and 29 deletions

View File

@ -17,11 +17,10 @@ class _InAppWebViewExampleScreenState extends State<InAppWebViewExampleScreen> {
InAppWebViewController? webViewController;
InAppWebViewSettings settings = InAppWebViewSettings(
mediaPlaybackRequiresUserGesture: false,
allowsInlineMediaPlayback: true,
iframeAllow: "camera; microphone",
iframeAllowFullscreen: true
);
mediaPlaybackRequiresUserGesture: false,
allowsInlineMediaPlayback: true,
iframeAllow: "camera; microphone",
iframeAllowFullscreen: true);
PullToRefreshController? pullToRefreshController;
@ -45,7 +44,7 @@ class _InAppWebViewExampleScreenState extends State<InAppWebViewExampleScreen> {
await webViewController?.clearFocus();
})
],
settings: ContextMenuSettings(hideDefaultSystemContextMenuItems: false),
settings: ContextMenuSettings(hideDefaultSystemContextMenuItems: true),
onCreateContextMenu: (hitTestResult) async {
print("onCreateContextMenu");
print(hitTestResult.extra);
@ -62,7 +61,9 @@ class _InAppWebViewExampleScreenState extends State<InAppWebViewExampleScreen> {
contextMenuItemClicked.title);
});
pullToRefreshController = kIsWeb || ![TargetPlatform.iOS, TargetPlatform.android].contains(defaultTargetPlatform)
pullToRefreshController = kIsWeb ||
![TargetPlatform.iOS, TargetPlatform.android]
.contains(defaultTargetPlatform)
? null
: PullToRefreshController(
settings: PullToRefreshSettings(
@ -120,7 +121,7 @@ class _InAppWebViewExampleScreenState extends State<InAppWebViewExampleScreen> {
// initialFile: "assets/index.html",
initialUserScripts: UnmodifiableListView<UserScript>([]),
initialSettings: settings,
// contextMenu: contextMenu,
contextMenu: contextMenu,
pullToRefreshController: pullToRefreshController,
onWebViewCreated: (controller) async {
webViewController = controller;

View File

@ -239,34 +239,50 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate,
return super.hitTest(point, with: event)
}
public override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if let _ = sender as? UIMenuController {
if self.settings?.disableContextMenu == true {
if !onCreateContextMenuEventTriggeredWhenMenuDisabled {
// workaround to trigger onCreateContextMenu event as the same on Android
self.onCreateContextMenu()
onCreateContextMenuEventTriggeredWhenMenuDisabled = true
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.onCreateContextMenuEventTriggeredWhenMenuDisabled = false
}
}
return false
}
@available(iOS 13.0, *)
public override func buildMenu(with builder: UIMenuBuilder) {
if #available(iOS 16.0, *) {
if let menu = contextMenu {
let contextMenuSettings = ContextMenuSettings()
if let contextMenuSettingsMap = menu["settings"] as? [String: Any?] {
let _ = contextMenuSettings.parse(settings: contextMenuSettingsMap)
if !action.description.starts(with: "onContextMenuActionItemClicked-") && contextMenuSettings.hideDefaultSystemContextMenuItems {
return false
if contextMenuSettings.hideDefaultSystemContextMenuItems {
builder.remove(menu: .lookup)
}
}
}
if contextMenuIsShowing, !action.description.starts(with: "onContextMenuActionItemClicked-") {
let id = action.description.compactMap({ $0.asciiValue?.description }).joined()
self.channelDelegate?.onContextMenuActionItemClicked(id: id, title: action.description)
}
super.buildMenu(with: builder)
}
public override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if self.settings?.disableContextMenu == true {
if !onCreateContextMenuEventTriggeredWhenMenuDisabled {
// workaround to trigger onCreateContextMenu event as the same on Android
self.onCreateContextMenu()
onCreateContextMenuEventTriggeredWhenMenuDisabled = true
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.onCreateContextMenuEventTriggeredWhenMenuDisabled = false
}
}
return false
}
if let menu = contextMenu {
let contextMenuSettings = ContextMenuSettings()
if let contextMenuSettingsMap = menu["settings"] as? [String: Any?] {
let _ = contextMenuSettings.parse(settings: contextMenuSettingsMap)
if !action.description.starts(with: "onContextMenuActionItemClicked-") && contextMenuSettings.hideDefaultSystemContextMenuItems {
return false
}
}
}
if contextMenuIsShowing, !action.description.starts(with: "onContextMenuActionItemClicked-") {
let id = action.description.compactMap({ $0.asciiValue?.description }).joined()
self.channelDelegate?.onContextMenuActionItemClicked(id: id, title: action.description)
}
return super.canPerformAction(action, withSender: sender)
@ -612,7 +628,11 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate,
contextMenuIsShowing = true
let hitTestResult = HitTestResult(type: .unknownType, extra: nil)
if let lastLongPressTouhLocation = lastLongPressTouchPoint {
if configuration.preferences.javaScriptEnabled {
self.evaluateJavaScript("window.\(JAVASCRIPT_BRIDGE_NAME)._findElementsAtPoint(\(lastLongPressTouhLocation.x),\(lastLongPressTouhLocation.y))", completionHandler: {(value, error) in