0

I'm writing a simple RSS reader for iOS using Xcode 8 and building for iOS 10.0.

I allow the user to save webpages for offline reading; however, when there is no internet connection, local HTML files take a long time to load as the UIWebView I'm loading them with tries to resolve external assets like images etc (or at least, that's what I think it's doing). I figured disabling JS when an internet connection is not present would solve this problem, but I don't know how to do that and I haven't found much information on the matter.

tl,dr: is there a way to disable javascript in a UIWebView?

Thank you

1
  • Update: by using a WKWebView as LGP recommended the speed problems have vanished, so I don't need to disable js after all. Commented Feb 17, 2018 at 11:49

1 Answer 1

4

If you are building for iOS 10.0 you should use WKWebView instead. It has support for disabling javascript, and is also the recommended component to use.

First create the preferences object. You need to set this up before the web view is created.

WKPreferences *preferences = [[WKPreferences alloc] init];
preferences.javaScriptEnabled = NO; // Here its set

// Other things you might want to disable
preferences.javaScriptCanOpenWindowsAutomatically = NO;

Then create the configuration object which holds the preferences.

WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
configuration.preferences = preferences;

Then create your web view.

WKWebView *webView = [[WKWebView alloc] initWithFrame:frame configuration:configuration];

Read more about WKWebView here

Sign up to request clarification or add additional context in comments.

5 Comments

Oh I see, thanks. Just out of curiosity, was there any way to do it with the old UIWebView?
No, not that I am aware of.
Actually the WKWebView doesn't recognize the javaScriptEnabled attribute for some reason, am I doing it wrong? I followed this guide to set it up: iostpoint.wordpress.com/2016/08/05/implement-wkwebview-in-ios
Well either way the problem I had is solved (see the comment on my op) so it doesn't matter too much other than for the sake of learning. Thank you very much!
Sorry about that! I was too quick. The property is available, but on a preference object you set on the configuration. Note that you should use the initializer that takes the configuration parameter, or it won't work. I have updated the answer.

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.