UIWebView的基本用法

1、UIWebView的基礎使用html

一、建立UIWebView:web

CGRect bouds = [[UIScreen manScreen]applicationFrame]; 
UIWebView* webView = [[UIWebView alloc]initWithFrame:bounds];json

二、設置屬性:瀏覽器

webView.scalespageToFit = YES;//自動對頁面進行縮放以適應屏幕 
webView.detectsPhoneNumbers = YES;//自動檢測網頁上的電話號碼,單擊能夠撥打app

三、顯示網頁視圖UIWebView:函數

[self.view addSubview:webView];ui

四、加載內容編碼

NSURL* url = [NSURL URLWithString:@"http://www.baidu.com"];//建立URL 
NSURLRequest* request = [NSURLRequest requestWithURL:url];//建立NSURLRequest 
[webView loadRequest:request];//加載atom

也能夠加載一個本地資源:lua

NSURL* url = [NSURL fileURLWithPath:filePath];//建立URL 
NSURLRequest* request = [NSURLRequest requestWithURL:url];//建立NSURLRequest 
[webView loadRequest:request];//加載

UIWebView 還支持將一個NSString對象做爲源來加載。你能夠爲其提供一個基礎URL,來指導UIWebView對象如何跟隨連接和加載遠程資源:

[webView loadHTMLString:myHTML baseURL:[NSURL URLWithString:@"http://baidu.com"]];

 

五、導航

UIWebView類內部會管理瀏覽器的導航動做,經過goForward和goBack方法你能夠控制前進與後退動做:

[webView goBack]; 
[webView goForward]; 
[webView reload];//重載 
[webView stopLoading];//取消載入內容

 

六、UIWebViewDelegate委託代理

UIWebView支持一組委託方法,這些方法將在特定時間獲得通知。要使用這些方法,必須先設定webView的委託:

webView.delegate = self; 

下面每一個委託方法的第一個參數都是指向一個UIwebview的指針,所以你能夠將一個委託用於多個網頁視圖。

-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*) reuqest navigationType: (UIWebViewNavigationType)navigationType;//當網頁視圖被指示載入內容而獲得通知。應當返回YES,這樣會進行加載。經過導航類型參數能夠獲得請求發起的緣由,能夠是如下任意值: 
UIWebViewNavigationTypeLinkClicked 
UIWebViewNavigationTypeFormSubmitted 
UIWebViewNavigationTypeBackForward 
UIWebViewNavigationTypeReload 
UIWebViewNavigationTypeFormResubmitted 
UIWebViewNavigationTypeOther

UIWebView控件加載網頁的監聽函數方法:

-(void)webViewDidStartLoad:(UIWebView*)webView ;//當網頁視圖已經開始加載一個請求後,獲得通知。 
-(void)webViewDidFinishLoad:(UIWebView*)webView ;//當網頁視圖結束加載一個請求以後,獲得通知。 
-(void)webView:(UIWebView*)webView DidFailLoadWithError:(NSError*)error;//當在請求加載中發生錯誤時,獲得通知。會提供一個NSSError對象,以標識所發生錯誤類型。

以上是IOS中UIWebView的基礎使用要點詳解,接下來一些UIWebView的經常使用注意點。

 

2、IOS中UIWebView經常使用注意點:

一、與UIWebView進行交互,調用web頁面中的須要傳參的函數時,參數須要帶單引號,或者雙引號(雙引號須要進行轉義在轉義字符前加\),在傳遞json字符串時不須要加單引號或雙引號:

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *sendJsStr=[NSString stringWithFormat:@"openFile(\"%@\")",jsDocPathStr];
[webView stringByEvaluatingJavaScriptFromString:sendJsStr];
}

二、在該代理方法中判斷與webView的交互,可經過html裏定義的協議實現:

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

三、只有在webView加載完畢以後在可以調用對應頁面中的js方法。(對應方法如第1條).

四、爲webView添加背景圖片:

approvalWebView.backgroundColor=[UIColor clearColor];
approvalWebView.opaque=NO;//這句話很重要,webView是不是不透明的,no爲透明 在webView下添加個imageView展現圖片就能夠了

五、獲取webView頁面內容信息:

NSString *docStr=[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.textContent"];//獲取web頁面內容信息,此處獲取的是個json字符串
SBJsonParser *parserJson=[[[SBJsonParser alloc]init]autorelease];
NSDictionary *contentDic=[parserJson objectWithString:docStr];//將json字符串轉化爲字典

六、 加載本地文件的方法:

//第一種方法:
NSString* path = [[NSBundle mainBundle] pathForResource:name ofType:@"html" inDirectory:@"mobile"];//mobile是根目錄,name是文件名稱,html是文件類型
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]]; //加載本地文件
//第二種方法:
NSString *resourcePath = [[NSBundle mainBundle] resourcePath]; 
NSString *filePath = [resourcePath stringByAppendingPathComponent:@"mobile.html"]; 
NSString *htmlstring=[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; 
[uiwebview loadHTMLString:htmlstring baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];

七、將文件下載到本地址而後再用webView打開:

NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];
self.filePath = [resourceDocPath stringByAppendingPathComponent:[NSString stringWithFormat:@"maydoc%@",docType]];
NSData *attachmentData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:theUrl]];
[attachmentData writeToFile:filePath atomically:YES];
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[attachmentWebView loadRequest:requestObj];
//刪除指定目錄下的文件
NSFileManager *magngerDoc=[NSFileManager defaultManager];
[magngerDoc removeItemAtPath:filePath error:nil];

八、處理webView展現txt文檔亂碼問題:

if ([theType isEqualToString:@".txt"]) {//txt分帶編碼和不帶編碼兩種,帶編碼的如UTF-8格式txt,不帶編碼的如ANSI格式txt//不帶的,能夠依次嘗試GBK和GB18030編碼NSString* aStr = [[NSString alloc] initWithData:attachmentData encoding:NSUTF8StringEncoding];if (!aStr) {//用GBK進行編碼aStr=[[NSString alloc] initWithData:attachmentData encoding:0x80000632];}if (!aStr) {//用GBK編碼不行,再用GB18030編碼aStr=[[NSString alloc] initWithData:attachmentData encoding:0x80000631];}

相關文章
相關標籤/搜索