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

View File

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