1 webView的使用
- 优点:能在当前app中打开网址,适配 iOS7
- 缺点:没有功能,如果需要功能,只能自己实现
1.1 webView加载网页
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 设置内边距
self.webView.scrollView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0);
//内容是否自适应
self.webView.scalesPageToFit = YES;
[self.webView loadRequest:request];
1.2 webView加载本地文件
NSURL *url = [NSURL fileURLWithPath:@"/Users/Desktop/123.mp4"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
1.3 webView加载本地html
NSURL *url = [[NSBundle mainBundle] URLForResource:@"text.html" withExtension:nil];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 检测特殊字符的样式
self.webView.dataDetectorTypes = UIDataDetectorTypeAll;
[self.webView loadRequest:request];
1.4 webView的前进/后退/刷新
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
// 先要遵守UIWebViewDelegate代理
self.webView.delegate = self;
}
- (IBAction)goBackBtnClick:(id)sender {
[self.webView goBack];
}
- (IBAction)goForwardBtnClick:(id)sender {
[self.webView goForward];
}
- (IBAction)reloadBtnClick:(id)sender {
[self.webView reload];
}
#pragma mark UIWebViewDelegate
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
self.goBack.enabled = self.webView.canGoBack;
self.goForward.enabled = self.webView.canGoForward;
}
2 SFSafariViewController的使用
- 优点:把Safari封装成了一个控制器
- 缺点:iOS9以后才能使用
- 使用步骤:
1.导入#import <SafariServices/SafariServices.h>
2.推荐使用Modal,但也可以使用 push
NSURL *url = [NSURL URLWithString:item.url];
SFSafariViewController *safariVc = [[SFSafariViewController alloc] initWithURL:url];
1.push的方式
// SFSafariViewController顶部条 会与 导航条冲突,显示顶部条,一定要记得隐藏导航条
self.navigationController.navigationBar.hidden = YES;
safariVc.delegate = self; // 遵守SFSafariViewControllerDelegate代理
[self.navigationController pushViewController:safariVc animated:YES];
// 默认不能返回,需要在代理方法中作返回处理
-(void)safariViewControllerDidFinish:(SFSafariViewController *)controller {
[self.navigationController popViewControllerAnimated:YES];
}
2.Model 的方式
[self presentViewController:safariVc animated:YES completion:nil];
备注:具备返回的功能,不需要额外处理(推荐)
3 WKWebView控件 (UIWebView 的升级版)
-优点:可以监听进度条,比UIWebView功能更加多
-缺点: iOS 8以后
-使用:1.#import <WebKit/WebKit.h> 2.需要手动把WebKit框架编译
- (void)viewDidLoad {
[super viewDidLoad];
WKWebView *webView = [[WKWebView alloc] init];
[_contentView addSubview:webView];
// 添加底部额外滚动区域
webView.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 44, 0);
// 展示网页
NSURLRequest *request = [NSURLRequest requestWithURL:_url];
[webView loadRequest:request];
// 前进,后退,刷新,进度条
// KVO监听属性
// Observer:观察者
// KeyPath:监听哪个属性
// options:NSKeyValueObservingOptionNew,监听新值改变
[webView addObserver:self forKeyPath:@"canGoBack" options:NSKeyValueObservingOptionNew context:nil];
[webView addObserver:self forKeyPath:@"canGoForward" options:NSKeyValueObservingOptionNew context:nil];
[webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
[webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];
}
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
WKWebView *webView = _contentView.subviews[0];
webView.frame = _contentView.bounds;
}
// 监听属性有新值就会调用
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)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;
}
// 对象即将销毁,移除观察者
- (void)dealloc
{
[_webView removeObserver:self forKeyPath:@"canGoBack"];
[_webView removeObserver:self forKeyPath:@"canGoForward"];
[_webView removeObserver:self forKeyPath:@"estimatedProgress"];
[_webView removeObserver:self forKeyPath:@"title"];
}
// 后退
- (IBAction)back:(id)sender {
[_webView goBack];
}
// 前进
- (IBAction)forward:(id)sender {
[_webView goForward];
}
// 刷新
- (IBAction)refresh:(id)sender {
[_webView reload];
}