IOS開發(fā)網(wǎng)絡(luò)基礎(chǔ)02UIWebView

這篇文章主要是 講一些 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í)......

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,711評(píng)論 19 139
  • iOS開發(fā)系列--網(wǎng)絡(luò)開發(fā) 概覽 大部分應(yīng)用程序都或多或少會(huì)牽扯到網(wǎng)絡(luò)開發(fā),例如說新浪微博、微信等,這些應(yīng)用本身可...
    lichengjin閱讀 4,060評(píng)論 2 7
  • 夢(mèng)鄉(xiāng)不借一枝棲,永夜?jié)渤钣?jì)總非。 竹影上窗書個(gè)字,月華向瓦吐交輝。 人間悶聽官蛙唱,天上誰銜玉露杯? 百念從心聽漸...
    李野航閱讀 308評(píng)論 0 0
  • 作為一個(gè)Android開發(fā)者,肯定是用過Handler的。而Handler最常見的使用場(chǎng)景,就是在子線程里進(jìn)行更新...
    陳添閱讀 616評(píng)論 2 7
  • 初見佳人如倚夢(mèng),一路走來盡是空。 黃金萬兩容易得,知己一個(gè)卻難求。
    多情公子無情劍閱讀 322評(píng)論 0 2

友情鏈接更多精彩內(nèi)容