Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIWebView: Can You Disable Javascript?

You can disable Javascript in both mobile Safari and Cocoa's WebView but I can see no means of doing so in UIWebView.

Am I correct?

I ask in relation to this question regarding obtaining the title of page displayed in an UIWebView using Javascript. I had worried that it would fail if Javascript was disabled but it appears the API does not allow the disabling of Javascript.

If Javascript cannot be deactivated UIWebView,that renders my previous question moot.

like image 344
TechZen Avatar asked Feb 20 '10 17:02

TechZen


People also ask

Can I debug a UIWebView with Web Inspector?

If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled. You can debug the HTML, CSS, and JavaScript contained inside a UIWebView with Web Inspector. Read Debugging Web Content on iOS to learn how to configure Web Inspector for iOS.

What is the use of uiwebviewdelegate?

The UIWebViewDelegate protocol defines methods that a delegate of a UIWebView object can optionally implement to intervene when web content is loaded. Sets the main page contents, MIME type, content encoding, and base URL. func loadHTMLString(String, baseURL: URL?)

What happened to UIWebView in iOS 13?

As you probably know UIWebView has been deprecated in iOS 13. I've use it in my projects for years and it was really convenient to display HTML pages inside an app but even to interact with Javascript, for example to perform operations on JSON files sharing the same login with Android and even with the Web.

Can I embed UITableView in UIScrollView?

You shouldn’t embed UIWebView or UITableView objects in UIScrollView objects. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled. You can debug the HTML, CSS, and JavaScript contained inside a UIWebView with Web Inspector.


2 Answers

There is a way! Using the Content Security Policy which is partially supported in iOS 5.1 and up, and a custom header:

X-WebKit-CSP: script-src none;

You can tell the UIWebKit to not allow javascript on the page entirely. (or selectively only allow script from a specific domain, more information in the spec.

To do this from a server you control, you'll have to modify the response headers for the page to include the X-WebKit-CSP header... To do it from pages that are local (plain text or HTML data on device), you'll have to define and register a custom NSURLProtocol for loading your page, and send the header in your crafted NSHTTPURLResponse:

NSDictionary *headers = [NSDictionary dictionaryWithObjectsAndKeys:
                         @"script-src none",@"X-WebKit-CSP",
                         @"text/html",@"Content-type",
                         encoding,@"Content-encoding",
                         nil];
NSHTTPURLResponse *urlResponse = [[NSHTTPURLResponse alloc] initWithURL:self.request.URL
                                                         statusCode:200
                                                        HTTPVersion:@"1.1"
                                                       headerFields:headers];
[self.client URLProtocol:self didReceiveResponse:urlResponse cacheStoragePolicy:NSURLCacheStorageAllowedInMemoryOnly];
like image 90
BadPirate Avatar answered Oct 04 '22 00:10

BadPirate


There is no public API to disable Javascript. So it is fairly safe to assume that it won't be disabled.

like image 23
Stefan Arentz Avatar answered Oct 04 '22 00:10

Stefan Arentz