I am using WKWebView to load some links in my application. I want to disable all the annoying JavaScript banners that appear on almost all web pages. Is there a simple function that can do that?
2 Answers
WKWebView has a configuration to disable JavaScript, check the Apple reference.
var javaScriptEnabled: Bool
Update
let preferences = WKPreferences()
preferences.javaScriptEnabled = false
// Create a configuration for the preferences
let configuration = WKWebViewConfiguration()
configuration.preferences = preferences
// Instantiate the web view
webView = WKWebView(frame: view.bounds, configuration: configuration)
// Load page
if let theWebView = webView{
let url = NSURL(string: "http://www.apple.com")
let urlRequest = NSURLRequest(URL: url!)
theWebView.loadRequest(urlRequest)
theWebView.navigationDelegate = self
view.addSubview(theWebView)
}
5 Comments
Swift1
How can i implement that javaScriptEnabled in my code? i declared a var alpha = WKPreferences() then alpha.javaScriptEnabled = false but javascript are always loaded
Rashwan L
Have you added the WKNavigationDelegate?
Swift1
worked. but now some links are loaded in desktop mode. There's an option to force loading in mobile mode?
Rashwan L
Are the links available in responsive?
Swift1
Links are generated by a parser
For us who still use Objective-C, here is the solution:
WKPreferences *prefs = [[WKPreferences alloc]init];
prefs.javaScriptEnabled = NO;
// Create a configuration for the preferences
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc]init];
config.preferences = prefs;
// Instantiate the web view
_webView = [[WKWebView alloc]initWithFrame:[[UIScreen mainScreen]bounds] configuration:config];