写一个按钮触发时间跳转webView控制器
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:@"跳转网页" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor redColor]];
[btn setTintColor:[UIColor whiteColor]];
[btn setFrame:CGRectMake(30, 44*3+20, self.view.frame.size.width-60, 50)];
[btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
btn.layer.cornerRadius = 5;
[self.view addSubview:btn];
}
- (void)click{
[WPWebViewController showWithController:self withUrlStr:@"http://www.baidu.com" withTitle:@"百度"];
}
新建一个控制器WPWebViewController 在里边定义一个类方法
@property (nonatomic, strong) NSURL *url;
+(void)showWithController:(UIViewController *)controller withUrlStr:(NSString *)urlStr withTitle:(NSString *)title;
然后实现
+(void)showWithController:(UIViewController *)controller withUrlStr:(NSString *)urlStr withTitle:(NSString *)title{
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
WPWebViewController *webContro = [WPWebViewController new];
webContro.url = [NSURL URLWithString:urlStr];
webContro.title = title;
[controller.navigationController pushViewController:webContro animated:YES];
}
设置进度条
- (void)setUI{
//设置进度条
UIProgressView *progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0.1, self.view.frame.size.width, 3)];
progressView.tintColor = [UIColor blueColor];
progressView.trackTintColor = [UIColor whiteColor];
[self.view addSubview:progressView];
self.progressView = progressView;
//加载网页
if (IOS8x) {
WKWebView *wkWebView = [[WKWebView alloc] initWithFrame:self.view.bounds];
wkWebView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
wkWebView.navigationDelegate = self;
wkWebView.backgroundColor = [UIColor whiteColor];
[self.view insertSubview:wkWebView belowSubview:progressView];
[wkWebView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
[wkWebView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];
[wkWebView addObserver:self forKeyPath:@"loading" options:NSKeyValueObservingOptionNew context:nil];
NSURLRequest *request = [NSURLRequest requestWithURL:_url];
[wkWebView loadRequest:request];
self.wkWebView = wkWebView;
}else {
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
webView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
webView.scalesPageToFit = YES;
webView.backgroundColor = [UIColor whiteColor];
webView.delegate = self;
[self.view insertSubview:webView belowSubview:progressView];
NSURLRequest *request = [NSURLRequest requestWithURL:_url];
[webView loadRequest:request];
self.webView = webView;
}
}
// 计算wkWebView进度条
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (object == self.wkWebView && [keyPath isEqualToString:@"title"]){
self.title = self.wkWebView.title;
NSLog(@"捕捉title333:%@",self.title);
}
if (object == self.wkWebView && [keyPath isEqualToString:@"loading"]){
NSLog(@"加载完成");
}
if (object == self.wkWebView && [keyPath isEqualToString:@"estimatedProgress"]) {
CGFloat newprogress = [[change objectForKey:NSKeyValueChangeNewKey] doubleValue];
if (newprogress == 1) {
self.progressView.hidden = YES;
[self.progressView setProgress:0 animated:NO];
}else {
self.progressView.hidden = NO;
[self.progressView setProgress:newprogress animated:YES];
}
}
}
// 记得取消监听
- (void)dealloc {
if (IOS8x) {
[self.wkWebView removeObserver:self forKeyPath:@"estimatedProgress"];
[self.wkWebView removeObserver:self forKeyPath:@"title"];
[self.wkWebView removeObserver:self forKeyPath:@"loading"];
}
}
#pragma mark - webView代理
// 计算webView进度条
- (void)setLoadCount:(NSUInteger)loadCount {
_loadCount = loadCount;
if (loadCount == 0) {
self.progressView.hidden = YES;
[self.progressView setProgress:0 animated:NO];
}else {
self.progressView.hidden = NO;
CGFloat oldP = self.progressView.progress;
CGFloat newP = (1.0 - oldP) / (loadCount + 1) + oldP;
if (newP > 0.95) {
newP = 0.95;
}
[self.progressView setProgress:newP animated:YES];
}
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
self.loadCount ++;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
self.loadCount --;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
self.loadCount --;
}