开发者

Disable the Alert Box in UIWebView

开发者 https://www.devze.com 2023-01-05 13:28 出处:网络
I am loading an External web page in the UIWebView. When the web page loads, there is an Alert Box ( with OK and Cancel buttons) with some Suggest开发者_如何学Cion/ Info about the web page. I want to

I am loading an External web page in the UIWebView. When the web page loads, there is an Alert Box ( with OK and Cancel buttons) with some Suggest开发者_如何学Cion/ Info about the web page. I want to block this Alert Box when the web page loads in the UIWebView component in my iphone app. How can I implement in my code?


Thanks for your reply. I am not in-charge for the external web page(but i could ask the concern web page owner to do the changes with respect to following requirement). The requirement is that the Alert Box (used to tell about my iphone app) could be shown when we view the web page in iPhone Safari browswer , but not in UIWebView in which I am using the same web page) of my iphone app. I am using the same web page url both in iPhone Safari and in my iPhone app with UIWebView. So requirement is show the Alert box when we view the web page in iPHone Safari and don't show the Alert box when we view the same web page in iPhone app (within in UIWebView). Hope I have clearly explained my requirement. Please give any solution for this.


using stringByEvaluatingJavaScriptFromString: method you can change anything you want on the page with any javascript you provide, so you can overload or replace the alert function to do what you want


Are you the one 'in charge' of this external web page? If that's the case, you could do something like this:

Make the UIWebView load externalpage.html?noalert instead of externalpage.html. Then on externalpage.html you can check whether this query string variable exists, and only show the alert() if it doesn't:

if(!document.location.search || document.location.search.substring(1) != 'noalert') {
    alert('Boo!');
}


duplicate: UIWebView: Can I disable the javascript alert() inside any web page?


Since a UIWebView translates all Javascript alerts into native UIAlertViews it is fairly simple to block it on the native end. Looking into UIAlertView.h there is only one public method for showing an alert which is conveniently called: - (void)show;.

@interface UIAlertView (Blocker)
@end

#import "UIAlertView+Blocker.h"

@implementation UIAlertView (Blocker)

- (void)show {
   return;
}
@end

You can find the answer here: https://stackoverflow.com/a/21698251/2377378

0

精彩评论

暂无评论...
验证码 换一张
取 消