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

在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、运行查看结果


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值