Can I handle alert inside UIWebViewDelegate?

A better solution to this problem is to create a Category for UIWebView for the method

webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:

So that you can handle the alert event in any way that you’d like. I did this because I don’t like the default behavior of UIWebView when it puts the filename of the source in the UIAlertView title. The Category looks something like this,

@interface UIWebView (JavaScriptAlert) 

- (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame;

@end

@implementation UIWebView (JavaScriptAlert)

- (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
    UIAlertView* dialogue = [[UIAlertView alloc] initWithTitle:nil message:message delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
    [dialogue show];
    [dialogue autorelease];
}

@end

Leave a Comment