防止用户在WebKit View Xcode中复制HTML文本

时间:2018-05-22 09:36:38

标签: html ios xcode copy webkit

我想禁用复制WebKit视图中显示的html文件的文本。这是我的WebKit查看代码:

@IBOutlet weak var webView: WKWebView!
@IBOutlet weak var backgroundView: UIView!

var index = 0
var fileName = ""

override func viewDidLoad() {

    super.viewDidLoad()

    loadSubject()

    // Load the appropriate file

    let htmlPath = Bundle.main.path(forResource: fileName, ofType: "htm")
    let url = URL(fileURLWithPath: htmlPath!)
    let request = URLRequest(url: url)
    webView.load(request)

}

我试图找到一个解决方案而且这是最接近的解决方案,但如果对我来说这是正确的答案,我也不确定如何实现这个:Prevent user from copying text on browsers

我已经使用Swift和Xcode大约6个月,但我是HTML和WebKit View的新手,所以如果这很简单,我会道歉。

谢谢!

1 个答案:

答案 0 :(得分:0)

试试这段代码

func webViewDidFinishLoad(_ webView: UIWebView) {
    webView.stringByEvaluatingJavaScript(from: "document.documentElement.style.webkitTouchCallout='none';")
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
    if action == Selector("customMethod:") {
        return super.canPerformAction(action, withSender: sender)
    }
    return false
}