0

I want to disable copying text of my html file that is displayed in a WebKit View. Here is my WebKit View code:

@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)

}

I'm trying to find a solution and here's the closest one, but I'm not sure how to implement this if it is even the correct answer for me: Prevent user from copying text on browsers

I've been using Swift and Xcode for about 6 months, but I'm brand new to HTML and WebKit View, so I apologize if this is something simple.

Thanks!

Jared L.
  • 207
  • 2
  • 13

1 Answers1

0

try this code

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
}
Jigar
  • 1,801
  • 1
  • 14
  • 29
  • When I added that code it changed it to this: `func webViewDidFinishLoad(webView: UIWebView) { webView.stringByEvaluatingJavaScript(from: "document.documentElement.style.webkitUserSelect='none'")! webView.stringByEvaluatingJavaScript(from: "document.documentElement.style.webkitTouchCallout='none'")! }` but then there's a warning: Expressing of type: String unused. I'm not sure what to do here, it doesn't prevent copy and paste – Jared L. May 22 '18 at 22:35
  • I forgot to tag you for the comment above, thanks. @JigarDarji – Jared L. May 23 '18 at 09:46
  • Thanks. It still doesn't work though. When I select text on the iPhone this shows up in the debugger: _2018-05-23 18:20:10.926904-0400 App Name Here [3967:3696537] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles 2018-05-23 18:20:10.930144-0400 App Name Here[3967:3696537] [MC] Reading from public effective user settings._ I'm not sure if this is helpful. Also it wanted me to fix `Selector("customMethod:")` to `Selector(("customMethod:"))` – Jared L. May 23 '18 at 22:24
  • This is the warning for the change noted above: _No method declared with Objective-C selector 'customMethod:' Wrap the selector name in parentheses to suppress this warning_. I tried it both ways and it still allows copy and pasting :( @JigarDarji – Jared L. May 23 '18 at 22:26