Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable user selection in a WKWebView in Swift?

I have developed an app which shows a web page using WKWebView in Swift. I need to disable the user selection and the callout (because the web loads a graph) and I don't find any way to do this with WKWebView.

This is my code:

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {

    @IBOutlet weak var webView: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let url = URL(string: "https://danfg95glucose.azurewebsites.net")
        let request = URLRequest(url: url!)
        webView.navigationDelegate = self
        webView.load(request)

    }
}

I want to do something similar with this but in Swift with WKWebView:

// Disable user selection
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
// Disable callout
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];

Is it possible?

This is a image showing the problem:

enter image description here

Thank you so much for your responses. And sorry for my level, I am new and I am learning programming.

like image 413
Daniel Fernandez Avatar asked Jan 26 '18 18:01

Daniel Fernandez


People also ask

How can I clear the contents of a UIWebView WKWebView?

To clear old contents of webview With UIWebView you would use UIWebViewDelegate 's - webViewDidFinishLoad: .

How do I refresh WKWebView?

The WKWebView already contains a scrollview. All you need to do is create the refresh control, assign a target function that will get called when a user initiates a refresh, and attach the refresh control to the scrollview.

What is WKWebView in Swift?

A WKWebView object is a platform-native view that you use to incorporate web content seamlessly into your app's UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app's native views.


2 Answers

What you're looking for is adding this css to your WKWebView programmatically.

First, on your view controller, add the WKWebView:

let webViewConfiguration = WKWebViewConfiguration()
let webView = WKWebView(frame: .zero, configuration: webViewConfiguration)
view.addSubview(webView)
webView.snp.updateConstraints { make in
    make.edges.equalTo(view)
}

webView.navigationDelegate = self
webView.load(URLRequest(url: URL(string: "https://www.google.com")!))

Then, implement the navigation delegate method

optional public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)

And inside it add this:

extension ExampleViewController: WKNavigationDelegate {
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        let javascriptStyle = "var css = '*{-webkit-touch-callout:none;-webkit-user-select:none}'; var head = document.head || document.getElementsByTagName('head')[0]; var style = document.createElement('style'); style.type = 'text/css'; style.appendChild(document.createTextNode(css)); head.appendChild(style);"
        webView.evaluateJavaScript(javascriptStyle, completionHandler: nil)
    }
}

With that, you'll add a style tag programmatically with the css that disables callouts.

Hope it helps!

like image 111
Joel Márquez Avatar answered Sep 28 '22 10:09

Joel Márquez


The answer from Joel Marquez is correct, however I want to add that you can use the WKUserContentController instead of the delegate method:

    let selectionString = "var css = '*{-webkit-touch-callout:none;-webkit-user-select:none}';"
        + " var head = document.head || document.getElementsByTagName('head')[0];"
        + " var style = document.createElement('style'); style.type = 'text/css';" +
        " style.appendChild(document.createTextNode(css)); head.appendChild(style);"
    let selectionScript: WKUserScript = WKUserScript(source: selectionString, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
    webView.configuration.userContentController.addUserScript(selectionScript)
like image 30
Apfelsaft Avatar answered Sep 28 '22 12:09

Apfelsaft