13

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 2

17

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)
}
Sign up to request clarification or add additional context in comments.

5 Comments

How can i implement that javaScriptEnabled in my code? i declared a var alpha = WKPreferences() then alpha.javaScriptEnabled = false but javascript are always loaded
Have you added the WKNavigationDelegate?
worked. but now some links are loaded in desktop mode. There's an option to force loading in mobile mode?
Are the links available in responsive?
Links are generated by a parser
1

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];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.