一、UIWebView简介
1、是iOS内置的浏览器控件,可以浏览网页、打开文档等
2、能够加载html、pdf、docx、text等格式的文件
3、系统自带的Safari浏览器就是通过UIWebView实现的
4、当UIWebView加载了一个界面(网页),那么在操作浏览的时候和所写的代码已经没有什么关系了,和网络服务器、网页操作有关,与iOS开发无关
二、UIWebView加载内容的三种方式
1、loadRequest
可以加载本地或服务器中的资源、网页
2、loadHTMLString:baseURL
加载html代码 html学习网站 http://www.w3school.com.cn/html/index.asp
参数:baseURL,相对地址可以在指定的baseURL中查找相关文件,一般赋值为nil
3、loadData:MIMEType:textEncodingName:baseURL
可以加载本地或服务器中的文件、网页
参数MIMEType:文件类型,告诉浏览器使用什么样的插件来加载数据 文件类型对照表http://tool.oschina.net/commons
三、webView导航方法
1、goBack 回退
2、goForward 前进
3、reload 重载
4、stopLoading 取消载入内容
四、常用属性
1、自动对页面进行缩放以适应屏幕
scalespageToFit
2、设定电话号码、网址、电子邮件和日期等文字变为链接文字
dataDetectorTypes
五、代理方法
1、网页开始加载的时候调用
- (void )webViewDidStartLoad:(UIWebView *)webView
2、网页加载完成的时候调用
- (void )webViewDidFinishLoad:(UIWebView *)webView
3、网页加载错误的时候调用
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
练习:简易浏览器,模拟safair浏览器
@interface ViewController ()<UISearchBarDelegate,UIWebViewDelegate>
@property (nonatomic,strong) UISearchBar *searchBar;
@property (nonatomic,strong) UIWebView *webView;
@property (nonatomic,strong) UIToolbar *toolBar;
@property (nonatomic,strong) UIActivityIndicatorView *indicatorView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self loadUI];
}
- (void)loadUI{
//搜索框
self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 20, 414, 40)];
self.searchBar.placeholder = @"输入内容";
self.searchBar.delegate = self;
[self.view addSubview:self.searchBar];
//两个按钮之间的空隙
self.searchBar.showsScopeBar = YES;
//导航条
self.toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 736-50, 414, 50)];
[self.view addSubview:self.toolBar];
UIBarButtonItem *backBtn = [[UIBarButtonItem alloc]initWithTitle:@"后退" style:UIBarButtonItemStylePlain target:self action:@selector(clickBackBtn)];
UIBarButtonItem *forwardBtn = [[UIBarButtonItem alloc]initWithTitle:@"前进" style:UIBarButtonItemStylePlain target:self action:@selector(clickForwordBtn)];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
self.toolBar.items = @[backBtn,space,forwardBtn];
//网页视图图
self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 64, 414, 736-64-50)];
[self.view addSubview:self.webView];
//让网页内容根据屏幕自适应
self.webView.scalesPageToFit = YES;
self.webView.delegate = self;
//将手机、邮件、网页等变为链接文字
self.webView.dataDetectorTypes = UIDataDetectorTypeAll;
//风火轮视图
self.indicatorView = [[UIActivityIndicatorView alloc]init];
self.indicatorView.center = self.view.center;
[self.view addSubview:self.indicatorView];
self.indicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
}
- (void)clickBackBtn{
[self.webView goBack];
}
- (void)clickForwordBtn{
[self.webView goForward];
}
//键盘搜索按钮按下时调用该方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
[self loadRequest:searchBar.text];
}
- (void)loadRequest:(NSString *)inputStr{
NSURL *resultUrl;
/**
* 请求到的字符串类型大致有三种格式:
A.前缀是file:///
* 1.当前缀是file:///时,先获取到前缀在字符串中的位置
* 2.截取文件名
* 3.获取路径
* 4.将路径转换为url
B.前缀是http://
C.当直接输入字符串进行搜索时,用默认的百度浏览器来进行搜索
*/
if ([inputStr hasPrefix:@"file:///"]) {
// file:///test.png
//找到file:///在字符串中的位置
NSRange rang = [inputStr rangeOfString:@"file:///"];
//截取到文件名
inputStr = [inputStr substringFromIndex:rang.length];
NSString *path = [[NSBundle mainBundle] pathForResource:inputStr ofType:nil];
resultUrl = [NSURL fileURLWithPath:path];
}else if([inputStr hasPrefix:@"http://"]){
resultUrl = [NSURL URLWithString:inputStr];
}else{
NSString *tempStr = [NSString stringWithFormat:@"http://www.baidu.com/s?wd=%@",inputStr];
resultUrl = [NSURL URLWithString:tempStr];
}
NSURLRequest *request = [NSURLRequest requestWithURL:resultUrl];
[self.webView loadRequest:request];
}
//WebView开始加载
- (void)webViewDidStartLoad:(UIWebView *)webView{
//在状态栏显示网络旋转的齿轮,默认是没有
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[self.indicatorView startAnimating];
}
//WebView已经完成加载
- (void)webViewDidFinishLoad:(UIWebView *)webView{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[self.indicatorView stopAnimating];
}
//加载失败
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[self.indicatorView stopAnimating];
UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"请检查网络设置" message:nil preferredStyle:UIAlertControllerStyleAlert];
[alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}]];
[self presentViewController:alertC animated:YES completion:nil];
}
//隐藏状态栏
- (BOOL)prefersStatusBarHidden
{
return YES;
}
网友评论