这篇文章主要是 讲一些 UIWebView的简单知识,实现一个浏览器的效果和加载文件(txt,html,pdf等)
关于MIMIE Type
就是服务器告诉浏览器发送的多媒体数据的类型。(更多请自行Google)
MIME类型能包括视频、图像、文本、音频、应用程序等。
获取指定URL的MIME类型
#pragma mark - 获取指定URL的MIME type 类型
- (NSString *)mimeType:(NSURL *)url
{
//1.NSURLRequest
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//2.NSURLConnection
//从NSURLResponse 可以获取服务器返回的MIMEType
//使用同步方法获取MIME
NSURLResponse *response = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
return response.MIMEType;
}
加载沙盒html文件
- (void)loadHtml
{
NSString *path = [[NSBundle mainBundle]pathForResource:@"1.html" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSLog(@"%@",[self mimeType:url]);
NSData *data = [NSData dataWithContentsOfFile:path];
[self.webView loadData:data MIMEType:@"text/html" textEncodingName:@"UFT-8" baseURL:nil];
}
加载PDF文件
- (void)loadPDF
{
NSString *path = [[NSBundle mainBundle]pathForResource:@"1.pdf" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSLog(@"%@",[self mimeType:url]);
NSData *data = [NSData dataWithContentsOfFile:path];
[self.webView loadData:data MIMEType:@"application/pdf" textEncodingName:@"UFT-8" baseURL:nil];
}
加载文本文件
- (void)loadTxt
{
NSString *path = [[NSBundle mainBundle]pathForResource:@"1.txt" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSLog(@"%@",[self mimeType:url]);
NSData *data = [NSData dataWithContentsOfFile:path];
[self.webView loadData:data MIMEType:@"text/plain" textEncodingName:@"UFT-8" baseURL:nil];
}
记载PDF.png
一个浏览器的例子
实现前进、后退、刷新
顶部的文本框、前进、后退、刷新 UI
UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 20, 320, 44)];
[self.view addSubview:toolbar];
//1>.1个textField (textField 以自定义视图的形式加入到toolbar)
UITextField *textfield = [[UITextField alloc]initWithFrame:CGRectMake(10, 26, 200, 32)];
//设置边框、设置对其、设置清楚按钮
[textfield setBorderStyle:UITextBorderStyleRoundedRect];
[textfield setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[textfield setClearButtonMode:UITextFieldViewModeWhileEditing];
[textfield setDelegate:self];
UIBarButtonItem *addressItem = [[UIBarButtonItem alloc]initWithCustomView:textfield];
//2>.三个按钮
UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:self action:@selector(goBack)];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:@selector(goForward)];
UIBarButtonItem *item3 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh)];
//将UIBarButtonItem加入boolbar
[toolbar setItems:@[addressItem,item1,item2,item3]];
//2.UIWebView
UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 64, 320, self.view.bounds.size.height - 64)];
[self.view addSubview:webView];
文本框回车时关闭键盘 开始请求
首先遵守协议:<UITextFieldDelegate>
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//1.关闭键盘
[textField resignFirstResponder];
//2.让webview加载地址栏中的内容,如果没有内容不加载
//关于字符串的比较 是属于消耗性能的 在判断是否有内容时,可以使用长度是否为0
if(textField.text.length > 0){
[self loadContentWithURLString:textField.text];
}
return YES;
}
根据类型 分流请求
- (void)loadContentWithURLString:(NSString *)urlString
{
BOOL hasError = NO;
//针对urlStrig进行判断
//1.如果是http开头说明是web地址
if([urlString hasPrefix:@"http://"]){
[self loadURL:[NSURL URLWithString:urlString]];
}else if ([urlString hasPrefix:@"file://"]){
//2.如果是file开头说明是加载沙箱的文件
}else{
hasError = YES;
}
if (hasError == YES) {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"地址错误" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alertView show];
}
}
发起http请求
- (void)loadURL:(NSURL *)url
{
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}
前进后退刷新
#pragma mark 后对
- (void)goBack
{
[self.webView goBack];
NSLog(@"后退");
}
#pragma mark 前进
- (void)goForward
{
[self.webView goForward];
NSLog(@"前进");
}
#pragma mark 刷新
- (void)refresh
{
[self.webView reload];
NSLog(@"刷新");
}
浏览器.png
做个笔记,一步一步,一起学习......
网友评论