简体   繁体   中英

Get selected text from custom UIMenuItem in WKWebView

I am creating an iOS app that has a custom UIMenuItem. This new custom UIMenuItem shows up when text is selected in WKWebView. How do I get the selected text.

I followed instructions in https://stackoverflow.com/a/49761522/6828076 to create a custom UIMenuItem. It works fine, but I need the selected text that was used when the custom UIMenuItem was tapped. 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.

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.

Here's the code from the the answer you used, slightly altered to get the selected text by evaluating Javascript on the WKWebView:

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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