1.webView :没有自带功能,不能监听进度条
2.WebKit:没有自带功能,但可以监听进度条, iOS8后出的
3.safari : 自带功能齐全,进度条也封装好, iOS9后出的
webView使用:
1)通过URL加载请求
[self.webViewloadRequest:[NSURLRequestrequestWithURL:[NSURLURLWithString:self.url]]];
2)成为代理,监听点击
WebKit的使用
可以通过KVO监听,进度条,前进返回按钮
1.加载网页
NSURLRequest*request = [NSURLRequestrequestWithURL:_url];
[webViewloadRequest:request];
2.使用KVO监听
[_webView addObserver:selfforKeyPath:@"canGoBack"options:NSKeyValueObservingOptionNewcontext:nil];
[_webView addObserver:selfforKeyPath:@"canGoForward"options:NSKeyValueObservingOptionNewcontext:nil];
//进度条,监听进度条
[_webView addObserver:selfforKeyPath:@"estimatedProgress"options:NSKeyValueObservingOptionNewcontext:nil];
[_webView addObserver:selfforKeyPath:@"title"options:NSKeyValueObservingOptionNewcontext:nil];
//观察的属性有新值的时候就会调用
- (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void*)context
{
_backItem.enabled=_webView.canGoBack;
_forwardItem.enabled=_webView.canGoForward;
_progressView.progress=_webView.estimatedProgress;
_progressView.hidden=_webView.estimatedProgress>=1;
self.title=_webView.title;
}
3.注销KVO
- (void)dealloc
{
[_webView removeObserver:self forKeyPath:@"title"];
[_webView removeObserver:self forKeyPath:@"canGoBack"];
[_webView removeObserver:self forKeyPath:@"canGoForward"];
[_webView removeObserver:self forKeyPath:@"estimatedProgress"];
}
Safari的使用
使用很简单,只需要modal出来即可;缺点是需要跳转到其他应用.并且是iOS9才能使用
SFSafariViewController* safriVC = [[SFSafariViewControlleralloc]initWithURL:[NSURLURLWithString:item.url]];
[self presentViewController:safriVCanimated:YEScompletion:nil];