简体   繁体   English

从WKWebView中的自定义UIMenuItem获取选定的文本

[英]Get selected text from custom UIMenuItem in WKWebView

I am creating an iOS app that has a custom UIMenuItem. 我正在创建一个具有自定义UIMenuItem的iOS应用。 This new custom UIMenuItem shows up when text is selected in WKWebView. 在WKWebView中选择文本时,将显示此新的自定义UIMenuItem。 How do I get the selected text. 如何获取所选文本。

I followed instructions in https://stackoverflow.com/a/49761522/6828076 to create a custom UIMenuItem. 我按照https://stackoverflow.com/a/49761522/6828076中的说明创建自定义UIMenuItem。 It works fine, but I need the selected text that was used when the custom UIMenuItem was tapped. 它工作正常,但是我需要点击自定义UIMenuItem时使用的选定文本。 There are many posts about using UIPasteboard but the custom item does not copy the selected text into the UIPasteboard, so I am unable to retrieve it. 关于使用UIPasteboard的文章很多,但是自定义项不会将所选文本复制到UIPasteboard中,因此我无法检索到它。

func setupCustomMenu() {
    let customMenuItem = UIMenuItem(title: "Foo", action:
        #selector(ViewController.transelateMenuTapped))
    UIMenuController.shared.menuItems = [customMenuItem]
    UIMenuController.shared.update()
}

@objc func transelateMenuTapped() {
    let yay = //Need to retrieve the selected text here
    let alertView = UIAlertController(title: "Yay!!", message: yay, preferredStyle: .alert)
    alertView.addAction(UIAlertAction(title: "cool", style: .default, handler: nil))
    present(alertView, animated: true, completion: nil)
}

You can use Javascript for that. 您可以为此使用Javascript。

Here's the code from the the answer you used, slightly altered to get the selected text by evaluating Javascript on the WKWebView: 这是您使用的答案中的代码,通过对WKWebView上的Javascript进行评估,对代码进行了些微改动以获取选定的文本:

import UIKit
import WebKit

class ViewController: UIViewController {
    weak var webView: CustomMenuWebView!

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        prepareWebView()
    }

    @objc func translateMenuTapped(_ test: Any) {
        webView.evaluateJavaScript("window.getSelection().toString()") { (test, error) in
                guard let test = test as? String, error == nil else { return }
                // ***** Here's the user's selected text *****
                print(test) 
        }
    }
}

private extension ViewController {
    func prepareWebView() {
        addWebViewToView()
        loadWebViewContent()
        setupCustomMenu()
    }

    func addWebViewToView() {
        let webView = CustomMenuWebView(
            frame: view.bounds, configuration: WKWebViewConfiguration())
        view.addSubview(webView)
        self.webView = webView
    }

    func loadWebViewContent() {
        let url = URL(string: "https://www.google.com")
        let request = URLRequest(url: url!)
        webView.load(request)
    }

    func setupCustomMenu() {
        let customMenuItem = UIMenuItem(
            title: "Translate", action: #selector(ViewController.translateMenuTapped))
        UIMenuController.shared.menuItems = [ customMenuItem ]
        UIMenuController.shared.update()
    }
}

class CustomMenuWebView: WKWebView {
    // Turn off all other menu items
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        return false
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM