在iOS app 中经常会嵌套html 代码,js运行alert时会出现这个问题,见图:
提示框的title为所在目录文件夹名字:
解决方案:
用 iOS native 的uiwebview 的扩展方法来监听 js的alert 然后自定义 alert 的title
#import <UIKit/UIKit.h>
@interface UIWebView (JavaScriptAlert)
- (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(CGRect *)frame;
@end
#import "UIWebView+JavaScriptAlert.h"
@implementation UIWebView (JavaScriptAlert)
- (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(CGRect *)frame {
// app名称
NSDictionary *dictInfo = [[NSBundle mainBundle] infoDictionary];
NSString *app_Name = [dictInfo objectForKey:@"CFBundleDisplayName"];
UIAlertView* customAlert = [[UIAlertView alloc] initWithTitle:app_Name message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[customAlert show];
[customAlert autorelease];
}
@end