SwiftUI:WKWebView 中的自定义弹出菜单
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
已经过去 6 个月了,但这里是答案(在 iOS 16 之前完美运行。在 iOS 16 中,该功能已被弃用,但仍然有效)
一切都发生在 canPerformAction 中
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