SwiftUI:WKWebView 中的自定义弹出菜单

发布于 2025-01-13 02:39:50 字数 873 浏览 0 评论 0原文

在 My Swift 项目中,我使用 wkWebView 来显示从 Web 服务检索到的 HTML 内容。一切正常。 我想更进一步,允许用户在选择部分文本时通过菜单报告错误。

我不建议“复制、剪切和粘贴”,而只想“复制”+“报告故障”。

该按钮的作用是打开邮箱并在其中插入选定的文本。

我不知道该怎么做。我只能禁用“剪切”/“粘贴”按钮,只保留“复制”。

我的代码:

struct articleWebView: UIViewRepresentable  {
    
     ...
    
    func makeUIView(context: Context) -> WKWebView {

        let WKWebView = CustomWKWebView()
        return WKWebView()
    }
    
    
    
    func updateUIView(_ uiView: WKWebView, context: Context) {
    
          ...
     }
}

class CustomWKWebView: WKWebView {

 //we deactivate everything except "copy" 
 override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        switch action {
        case #selector(copy(_:)):
            return true

        default:
            return false
        }
    }

}

提前谢谢你

In My Swift project, I use a wkWebView to display HTML content retrieved from a webservice. Everything works.
I would like to go further and allow the user to report a fault through the menu when part of the text is selected.

Instead of proposing "copy, cut and paste", I would only like "Copy" + "Report a fault".

The button would have the action of opening the mailbox and inserting the selected text there.

I don't know how to do it. I'm only able to disable the "cut"/"paste" buttons and only keep "copy".

My code :

struct articleWebView: UIViewRepresentable  {
    
     ...
    
    func makeUIView(context: Context) -> WKWebView {

        let WKWebView = CustomWKWebView()
        return WKWebView()
    }
    
    
    
    func updateUIView(_ uiView: WKWebView, context: Context) {
    
          ...
     }
}

class CustomWKWebView: WKWebView {

 //we deactivate everything except "copy" 
 override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        switch action {
        case #selector(copy(_:)):
            return true

        default:
            return false
        }
    }

}

Thank you in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泅渡 2025-01-20 02:39:50

已经过去 6 个月了,但这里是答案(在 iOS 16 之前完美运行。在 iOS 16 中,该功能已被弃用,但仍然有效)

一切都发生在 canPerformAction 中

class CustomWKWebView: WKWebView{
    


    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {

            let menu = UIMenuController.shared
            menu.shared.menuItems = []
            let newInstanceItem = UIMenuItem(title: " My Custom Action", action:#selector(myCustomAction))
            menu.menuItems = [newInstanceItem]
            menu.update()
            if action == #selector(copy(_:)) || action == #selector(selectAll(_:)) || action == #selector(myCustomAction)){

                return true
            }
            return false

    }



    @objc func myCustomAction() {
        // your code

    }

}

it's been 6 months but here is the answer (works perfectly before iOS 16. With iOS 16 the function is deprecated but still works)

everything happens in canPerformAction

class CustomWKWebView: WKWebView{
    


    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {

            let menu = UIMenuController.shared
            menu.shared.menuItems = []
            let newInstanceItem = UIMenuItem(title: " My Custom Action", action:#selector(myCustomAction))
            menu.menuItems = [newInstanceItem]
            menu.update()
            if action == #selector(copy(_:)) || action == #selector(selectAll(_:)) || action == #selector(myCustomAction)){

                return true
            }
            return false

    }



    @objc func myCustomAction() {
        // your code

    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文