ios开发中WebView,去除(自定义)JS中Alert显示的网址

来源:互联网 发布:淘宝旺铺智能版要钱吗 编辑:程序博客网 时间:2024/05/26 15:58

在ios开发中,经常会需要加载一些WebView 页面,html 、js 、php等。以加载JS为例,我们常常会遇到一些问题,web页面弹出Alert会显示出网址。

如图:


这种看起来很别扭,如何去掉这个网址或者自定义Alert显示内容呢?


1、建立UIWebView类别,添加监听JS页面的方法

(1)建类别方法如下:




2、在新的类别内添加如下方法:

.h中添加

#import <UIKit/UIKit.h>@interface UIWebView (hr_ent)- (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(id)frame;@end


.m中添加

#import "UIWebView+hr_ent.h"@implementation UIWebView (hr_ent)- (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(id)frame {            UIAlertView* customAlert = [[UIAlertView alloc] initWithTitle:@""                                                          message:message                                                         delegate:nil                                                cancelButtonTitle:@"确定"                                                otherButtonTitles:nil];        [customAlert show];    [customAlert release];    }@end


3、在加载UIWebView的页面进行导入

.m中添加

#import "ENTViewController.h"#import "UIWebView+hr_ent.h"@interface ENTViewController ()@property (nonatomic, strong)UIWebView  *webView;@end@implementation ENTViewController- (void)viewDidLoad {    [super viewDidLoad];        self.view.backgroundColor = [UIColor whiteColor];        UIWebView *webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];    self.webView = webView;    [self.view addSubview:webView];            [self loadHTML];        // Do any additional setup after loading the view.}


4、运行查看结果


0 0
原创粉丝点击