Merge pull request #1575 from heralight/fix-context-menu-ios13
fix #1389, fix #1315
This commit is contained in:
commit
5128b2e3e9
|
@ -21,8 +21,7 @@ class _InAppWebViewExampleScreenState extends State<InAppWebViewExampleScreen> {
|
||||||
mediaPlaybackRequiresUserGesture: false,
|
mediaPlaybackRequiresUserGesture: false,
|
||||||
allowsInlineMediaPlayback: true,
|
allowsInlineMediaPlayback: true,
|
||||||
iframeAllow: "camera; microphone",
|
iframeAllow: "camera; microphone",
|
||||||
iframeAllowFullscreen: true
|
iframeAllowFullscreen: true);
|
||||||
);
|
|
||||||
|
|
||||||
PullToRefreshController? pullToRefreshController;
|
PullToRefreshController? pullToRefreshController;
|
||||||
|
|
||||||
|
@ -63,7 +62,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(
|
||||||
|
@ -121,7 +122,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;
|
||||||
|
|
|
@ -224,6 +224,11 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate,
|
||||||
if !self.responds(to: Selector(targetMethodName)) {
|
if !self.responds(to: Selector(targetMethodName)) {
|
||||||
let customAction: () -> Void = {
|
let customAction: () -> Void = {
|
||||||
self.channelDelegate?.onContextMenuActionItemClicked(id: id, title: title)
|
self.channelDelegate?.onContextMenuActionItemClicked(id: id, title: title)
|
||||||
|
if #available(iOS 16.0, *) {
|
||||||
|
if #unavailable(iOS 16.4) {
|
||||||
|
self.onHideContextMenu()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
let castedCustomAction: AnyObject = unsafeBitCast(customAction as @convention(block) () -> Void, to: AnyObject.self)
|
let castedCustomAction: AnyObject = unsafeBitCast(customAction as @convention(block) () -> Void, to: AnyObject.self)
|
||||||
let swizzledImplementation = imp_implementationWithBlock(castedCustomAction)
|
let swizzledImplementation = imp_implementationWithBlock(castedCustomAction)
|
||||||
|
@ -239,12 +244,50 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate,
|
||||||
return super.hitTest(point, with: event)
|
return super.hitTest(point, with: event)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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 contextMenuSettings.hideDefaultSystemContextMenuItems {
|
||||||
|
builder.remove(menu: .lookup)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if #unavailable(iOS 16.4), settings?.disableContextMenu == false {
|
||||||
|
contextMenuIsShowing = false
|
||||||
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
|
||||||
|
self.onCreateContextMenu()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
super.buildMenu(with: builder)
|
||||||
|
}
|
||||||
|
|
||||||
|
@available(iOS 16.4, *)
|
||||||
|
public func webView(_ webView: WKWebView, willPresentEditMenuWithAnimator animator: UIEditMenuInteractionAnimating) {
|
||||||
|
onCreateContextMenu()
|
||||||
|
}
|
||||||
|
|
||||||
|
@available(iOS 16.4, *)
|
||||||
|
public func webView(_ webView: WKWebView, willDismissEditMenuWithAnimator animator: UIEditMenuInteractionAnimating) {
|
||||||
|
onHideContextMenu()
|
||||||
|
}
|
||||||
|
|
||||||
public override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
|
public override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
|
||||||
if let _ = sender as? UIMenuController {
|
var needCheck = sender is UIMenuController
|
||||||
if self.settings?.disableContextMenu == true {
|
if #available(iOS 13.0, *) {
|
||||||
|
needCheck = sender is UIMenuElement || sender is UIMenuController
|
||||||
|
}
|
||||||
|
|
||||||
|
if needCheck {
|
||||||
|
if settings?.disableContextMenu == true {
|
||||||
if !onCreateContextMenuEventTriggeredWhenMenuDisabled {
|
if !onCreateContextMenuEventTriggeredWhenMenuDisabled {
|
||||||
// workaround to trigger onCreateContextMenu event as the same on Android
|
// workaround to trigger onCreateContextMenu event as the same on Android
|
||||||
self.onCreateContextMenu()
|
onCreateContextMenu()
|
||||||
onCreateContextMenuEventTriggeredWhenMenuDisabled = true
|
onCreateContextMenuEventTriggeredWhenMenuDisabled = true
|
||||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
||||||
self.onCreateContextMenuEventTriggeredWhenMenuDisabled = false
|
self.onCreateContextMenuEventTriggeredWhenMenuDisabled = false
|
||||||
|
@ -265,10 +308,14 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate,
|
||||||
|
|
||||||
if contextMenuIsShowing, !action.description.starts(with: "onContextMenuActionItemClicked-") {
|
if contextMenuIsShowing, !action.description.starts(with: "onContextMenuActionItemClicked-") {
|
||||||
let id = action.description.compactMap({ $0.asciiValue?.description }).joined()
|
let id = action.description.compactMap({ $0.asciiValue?.description }).joined()
|
||||||
self.channelDelegate?.onContextMenuActionItemClicked(id: id, title: action.description)
|
channelDelegate?.onContextMenuActionItemClicked(id: id, title: action.description)
|
||||||
|
if #available(iOS 16.0, *) {
|
||||||
|
if #unavailable(iOS 16.4) {
|
||||||
|
onHideContextMenu()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return super.canPerformAction(action, withSender: sender)
|
return super.canPerformAction(action, withSender: sender)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -320,6 +367,7 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate,
|
||||||
context: nil)
|
context: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if #unavailable(iOS 16.0) {
|
||||||
NotificationCenter.default.addObserver(
|
NotificationCenter.default.addObserver(
|
||||||
self,
|
self,
|
||||||
selector: #selector(onCreateContextMenu),
|
selector: #selector(onCreateContextMenu),
|
||||||
|
@ -331,6 +379,7 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate,
|
||||||
selector: #selector(onHideContextMenu),
|
selector: #selector(onHideContextMenu),
|
||||||
name: UIMenuController.didHideMenuNotification,
|
name: UIMenuController.didHideMenuNotification,
|
||||||
object: nil)
|
object: nil)
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Still not working on iOS 16.0!
|
// TODO: Still not working on iOS 16.0!
|
||||||
// if #available(iOS 16.0, *) {
|
// if #available(iOS 16.0, *) {
|
||||||
|
|
Loading…
Reference in New Issue