【问题标题】:Prevent the user from copying text of HTML in WebKit View Xcode防止用户在 WebKit View Xcode 中复制 HTML 文本
【发布时间】:2018-11-01 00:40:33
【问题描述】:

我想禁止复制显示在 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 是全新的,所以如果这很简单,我很抱歉。

谢谢!

【问题讨论】:

    标签: html ios xcode copy webkit


    【解决方案1】:

    试试这个代码

    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
    }
    

    【讨论】:

    • 当我添加该代码时,它将其更改为:func webViewDidFinishLoad(webView: UIWebView) { webView.stringByEvaluatingJavaScript(from: "document.documentElement.style.webkitUserSelect='none'")! webView.stringByEvaluatingJavaScript(from: "document.documentElement.style.webkitTouchCallout='none'")! },但随后出现警告:表示类型:未使用字符串。我不知道在这里做什么,它不会阻止复制和粘贴
    • 我忘记在上面的评论中标记你了,谢谢。 @JigarDarji
    • 谢谢。但它仍然不起作用。当我在 iPhone 上选择文本时,调试器中会显示:2018-05-23 18:20:10.926904-0400 App Name Here [3967:3696537] [MC] systemgroup.com.apple 的系统组容器。 configurationprofiles 路径是 /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles 2018-05-23 18:20:10.930144-0400 App Name Here[3967:3696537] [MC] 从公共有效用户设置中读取. 我不确定这是否有帮助。它还希望我将Selector("customMethod:") 修复为Selector(("customMethod:"))
    • 这是上述更改的警告:没有使用 Objective-C 选择器“customMethod:”声明的方法将选择器名称括在括号中以抑制此警告。我尝试了两种方法,它仍然允许复制和粘贴:(@JigarDarji
    猜你喜欢
    • 1970-01-01
    • 2015-12-24
    • 2016-01-03
    • 1970-01-01
    • 2011-03-28
    • 2019-07-09
    • 2016-03-22
    • 2015-01-16
    • 1970-01-01
    相关资源
    最近更新 更多