UIWebView詳解

UIWebView的三種加載方式和其代理
API 提供了三種方法:

1.- (void)loadRequest:(NSURLRequest *)request; //加載HTML鏈接地址
2.- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL; //加載HTML代碼

3.- (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL;

(1)其中baseURL 是指基準的url 是一個絕對的地址,程序要用到的其他資源就可以根據這個基準地址進行查找而不用再次定位到絕對地址;

爲什麼需要設置baseURL?

也就是data中有一些鏈接是圖片,css都是外部文件,然後這些文件需要到一個目錄上去找。baseURL就是這個目錄。

(2)使用loadData方法對文件進行加載,並且指定類型(mimetype)和編碼類型(textEncodingName)

一、UIWebView加載.html網頁文件(loadRequest)

以下代碼都放在viewDidLoad方法體裏:
1、加載網絡html文件:

NSURL *url = [NSURL URLWithString:@"http://localhost:8080/jmDemo/index.html"]; //@"http//:www.baidu.com"
NSURLRequest *request = [NSURLRequest requestWithURL:url];  
[_webView loadRequest:request]

2、加載本地html文件,在iphone項目裏面

NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];  
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]];  
[_webView loadRequest:request];
 

3、加載本地html文件,不在iphone項目裏面,在Documents文件夾裏

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
NSString *documentsDirectory = [paths objectAtIndex:0];  
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"index.html"];  
NSURL *url = [NSURL fileURLWithPath:path];  
NSURLRequest *request = [NSURLRequest requestWithURL:url];  
[_webView loadRequest:request];

附加 加載pdf格式

NSURL *url =[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"f5_newspaper" ofType:@"pdf"]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];

二、loadHTMLString:baseURL:(1將本地html文件內容嵌入 2.加載HTML代碼 )

(1)將本地html文件內容嵌入

NSString *resourcePath = [[NSBundle mainBundle] resourcePath]; 
NSString *filePath = [resourcePath stringByAppendingPathComponent:@"test.html"];  
NSString *htmlstring =[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];  
[self.webView loadHTMLString:htmlstring baseURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]]];

 
 (2)直接加載加載HTML代碼
NSString *htmlstring = @"

 

  • 誠邀享有豐富教學。
  • 整合全球優質教學資源。
  • 通過科學的培訓。
  • 擁有優質食品管理人員。
  • 提高食品行業對食品安全的認知與重視度。
"

[self.webView loadHTMLString:htmlstring baseURL:nil];
<span style="font-family: Consolas, 'Bitstream Vera sans Mono', 'Courier new', Courier, monospace; background-color: rgb(255, 255, 255);"> </span>

三、 loadData:MIMEType:textEncodingName:baseURL:

(1)html內容

NSString *string = [NSString stringWithFormat:@"%@",self.model.contentClob];
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
[webContentView loadData:data MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:nil];

(2)加載.html文件

   //首先把數據寫到NSData中

NSString *pathurl = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];  

NSData *data = [NSData dataWithContentsOfFile:pathurl]

   //然後設置baseURL

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0] ;   //根據自己的具體情況設置,我的html文件在document目 錄,鏈接也是在這個目錄上開始
 NSURL *baseURL = [NSURL fileURLWithPath:documentsDir];

 [self.webView loadData:data MIMEType:@"text/html" textEncodingName:@"GBK" baseURL:baseURL];

四 、其他屬性及常用代理

 webContentView.scalesPageToFit = YES;//讓web content佈局適應webView
 webContentView.delegate = self;

#pragma mark - webView代理方法

 

- (void)webViewDidStartLoad:(UIWebView *)webView

{

    // 顯示提醒框

    [MBProgressHUD showMessage:@"哥正在幫你加載中..."];

}

 

 

- (void)webViewDidFinishLoad:(UIWebView *)webView

{

    // 隱藏提醒框

    [MBProgressHUD hideHUD];

}

 

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error

{

    // 隱藏提醒框

    [MBProgressHUD hideHUD];

}

 

 

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

{

    if (寫判斷) {

        // 不加載這個請求

        return NO;

    }

    return YES;

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章