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:
Thank you so much for your responses. And sorry for my level, I am new and I am learning programming.
To clear old contents of webview With UIWebView you would use UIWebViewDelegate 's - webViewDidFinishLoad: .
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.
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.
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!
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With