Skip to content Skip to sidebar Skip to footer

How To Control UIWebView's Javascript

It is that UIWebview and toggle button on UIView. WebPage includes javascript. I want to control UIWebView's javascript. What I want is that whenever Button on UIView is toggled, j

Solution 1:

Don't think it's possible to disable JavaScript for a UIWebView.

But you can use the stringByEvaluatingJavaScriptFromString: method of UIWebView, in order to execute specific JS code, all a JS function, etc.

- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script;

For instance:

[ self.webView stringByEvaluatingJavaScriptFromString: @"colorChange();" ];

If you want to deactivate the onclick() you've set, you can for instance set a global variable with stringByEvaluatingJavaScriptFromString:, and check for that variable in your colorChange function. If it has some value, do something, otherwise, do nothing.

var colorChangeEnabled = true;
function colorChange()
{
    if( colorChangeEnabled == false ) return;
    ...

And so:

[ self.webView stringByEvaluatingJavaScriptFromString: @"colorChangeEnabled = false;" ];

Solution 2:

Two part question, really.

If you're doing a user-interaction in javascript, it cannot subsequently broker a call into the iOS SDKs. You can do the reverse -- cause an action to occur within the webview from an iOS element, but you can't do the reverse.

Second, there's no public API for disabling/enabling javascript within an UIWebView object.

You can turn it on/off for Safari, so there's a way to disable the engine, but it seems to be a private method, so it's use will be prohibited in any app store submission.


Post a Comment for "How To Control UIWebView's Javascript"