简体   繁体   English

我可以在UIWebView中禁用JS吗? (目标C)

[英]Can I disable JS in a UIWebView? (Objective C)

I'm writing a simple RSS reader for iOS using Xcode 8 and building for iOS 10.0. 我正在使用Xcode 8为iOS编写一个简单的RSS阅读器,并为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). 但是,当没有Internet连接时,本地HTML文件需要花很长时间才能加载,因为我正在用UIWebView加载它们,以尝试解析图像等外部资产(至少,我认为这就是这样做的)。 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. 我认为在不存在Internet连接的情况下禁用JS可以解决此问题,但是我不知道该怎么做,而且我也没有找到很多有关此问题的信息。

tl,dr: is there a way to disable javascript in a UIWebView? tl,dr:有没有办法禁用UIWebView中的javascript?

Thank you 谢谢

If you are building for iOS 10.0 you should use WKWebView instead. 如果要针对iOS 10.0进行构建,则应改用WKWebView It has support for disabling javascript, and is also the recommended component to use. 它支持禁用javascript,也是推荐使用的组件。

First create the preferences object. 首先创建首选项对象。 You need to set this up before the web view is created. 您需要在创建Web视图之前进行设置。

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. 然后创建您的Web视图。

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

Read more about WKWebView here 在此处阅读有关WKWebView的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM