這篇文章主要是 講一些 UIWebView的簡(jiǎn)單知識(shí),實(shí)現(xiàn)一個(gè)瀏覽器的效果和加載文件(txt,html,pdf等)
關(guān)于MIMIE Type
就是服務(wù)器告訴瀏覽器發(fā)送的多媒體數(shù)據(jù)的類型。(更多請(qǐng)自行Google)
MIME類型能包括視頻、圖像、文本、音頻、應(yīng)用程序等。
獲取指定URL的MIME類型
#pragma mark - 獲取指定URL的MIME type 類型
- (NSString *)mimeType:(NSURL *)url
{
//1.NSURLRequest
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//2.NSURLConnection
//從NSURLResponse 可以獲取服務(wù)器返回的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
一個(gè)瀏覽器的例子
實(shí)現(xiàn)前進(jìn)、后退、刷新
頂部的文本框、前進(jìn)、后退、刷新 UI
UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 20, 320, 44)];
[self.view addSubview:toolbar];
//1>.1個(gè)textField (textField 以自定義視圖的形式加入到toolbar)
UITextField *textfield = [[UITextField alloc]initWithFrame:CGRectMake(10, 26, 200, 32)];
//設(shè)置邊框、設(shè)置對(duì)其、設(shè)置清楚按鈕
[textfield setBorderStyle:UITextBorderStyleRoundedRect];
[textfield setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[textfield setClearButtonMode:UITextFieldViewModeWhileEditing];
[textfield setDelegate:self];
UIBarButtonItem *addressItem = [[UIBarButtonItem alloc]initWithCustomView:textfield];
//2>.三個(gè)按鈕
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];
文本框回車時(shí)關(guān)閉鍵盤 開始請(qǐng)求
首先遵守協(xié)議:<UITextFieldDelegate>
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//1.關(guān)閉鍵盤
[textField resignFirstResponder];
//2.讓webview加載地址欄中的內(nèi)容,如果沒有內(nèi)容不加載
//關(guān)于字符串的比較 是屬于消耗性能的 在判斷是否有內(nèi)容時(shí),可以使用長度是否為0
if(textField.text.length > 0){
[self loadContentWithURLString:textField.text];
}
return YES;
}
根據(jù)類型 分流請(qǐng)求
- (void)loadContentWithURLString:(NSString *)urlString
{
BOOL hasError = NO;
//針對(duì)urlStrig進(jìn)行判斷
//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:@"地址錯(cuò)誤" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
[alertView show];
}
}
發(fā)起http請(qǐng)求
- (void)loadURL:(NSURL *)url
{
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}
前進(jìn)后退刷新
#pragma mark 后對(duì)
- (void)goBack
{
[self.webView goBack];
NSLog(@"后退");
}
#pragma mark 前進(jìn)
- (void)goForward
{
[self.webView goForward];
NSLog(@"前進(jìn)");
}
#pragma mark 刷新
- (void)refresh
{
[self.webView reload];
NSLog(@"刷新");
}

瀏覽器.png
做個(gè)筆記,一步一步,一起學(xué)習(xí)......