Remove AlertView Title in UIWebView


Background

The app uses webview to load web-page in iPad. When javascript call the function alert, iPad will call a message, e.g.

As you can see, the title of alert view is the domain name of this page, but it's ugly or should be in secrecy. So the task is to hide or remove the title of the alert view.

Solution

We can add category to UIWebView to catch up the alert and show another new alert with no title.

@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* customAlert = [[UIAlertView alloc] initWithTitle:@""
                                                          message:message
                                                         delegate:nil
                                                cancelButtonTitle:@"OK"
                                                otherButtonTitles:nil];

    [customAlert show];
    [customAlert release];

}

@end

Now we will get a new alert view with no title, but notice that the message will be in bold font.

Reference

Apple API Reference
Relevant problem on stackoverflow
Another solution on stackoverflow