在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、運行查看結果