????? 現(xiàn)在對(duì)于 混合式 移動(dòng)端開(kāi)發(fā)越來(lái)越流行构韵,因?yàn)殚_(kāi)發(fā)成本上虫啥、速度上都比傳統(tǒng)的APP開(kāi)發(fā)要好显拜,混合式開(kāi)發(fā)是傳統(tǒng)模式與PC網(wǎng)頁(yè)端相結(jié)合的模式。那么提到了APP的混合模式開(kāi)發(fā)瓤介,在Android開(kāi)發(fā)中有WebView作為混合模式開(kāi)發(fā)的橋梁吕喘,當(dāng)然在IOS中也同樣有一個(gè) UIWebView組件來(lái)作為混合模式開(kāi)發(fā)的橋梁,那么下面就對(duì)UIWebView的一些基本知識(shí)詳解一下刑桑。
一氯质、UIWebView的基礎(chǔ)使用
1、創(chuàng)建UIWebView:
CGRect bouds = [[UIScreen manScreen]applicationFrame];
UIWebView* webView = [[UIWebView alloc]initWithFrame:bounds];
2祠斧、設(shè)置屬性:
webView.scalespageToFit = YES;//自動(dòng)對(duì)頁(yè)面進(jìn)行縮放以適應(yīng)屏幕
webView.detectsPhoneNumbers = YES;//自動(dòng)檢測(cè)網(wǎng)頁(yè)上的電話號(hào)碼病梢,單擊可以撥打
3、顯示網(wǎng)頁(yè)視圖UIWebView:
[self.view addSubview:webView];
4梁肿、加載內(nèi)容
NSURL* url = [NSURL URLWithString:@"http://www.baidu.com"];//創(chuàng)建URL
NSURLRequest* request = [NSURLRequest requestWithURL:url];//創(chuàng)建NSURLRequest
[webView loadRequest:request];//加載
也可以加載一個(gè)本地資源:
NSURL* url = [NSURL fileURLWithPath:filePath];//創(chuàng)建URL
NSURLRequest* request = [NSURLRequest requestWithURL:url];//創(chuàng)建NSURLRequest
[webView loadRequest:request];//加載
UIWebView 還支持將一個(gè)NSString對(duì)象作為源來(lái)加載蜓陌。你可以為其提供一個(gè)基礎(chǔ)URL,來(lái)指導(dǎo)UIWebView對(duì)象如何跟隨鏈接和加載遠(yuǎn)程資源:
[webView loadHTMLString:myHTML baseURL:[NSURL URLWithString:@"http://baidu.com"]];
5吩蔑、導(dǎo)航
UIWebView類內(nèi)部會(huì)管理瀏覽器的導(dǎo)航動(dòng)作钮热,通過(guò)goForward和goBack方法你可以控制前進(jìn)與后退動(dòng)作:
[webView goBack];
[webView goForward];
[webView reload];//重載
[webView stopLoading];//取消載入內(nèi)容
6、UIWebViewDelegate委托代理
UIWebView支持一組委托方法烛芬,這些方法將在特定時(shí)間得到通知隧期。要使用這些方法,必須先設(shè)定webView的委托:
webView.delegate = self;
下面每個(gè)委托方法的第一個(gè)參數(shù)都是指向一個(gè)UIwebview的指針赘娄,因此你可以將一個(gè)委托用于多個(gè)網(wǎng)頁(yè)視圖仆潮。
-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*) reuqest navigationType: (UIWebViewNavigationType)navigationType;//當(dāng)網(wǎng)頁(yè)視圖被指示載入內(nèi)容而得到通知。應(yīng)當(dāng)返回YES遣臼,這樣會(huì)進(jìn)行加載性置。通過(guò)導(dǎo)航類型參數(shù)可以得到請(qǐng)求發(fā)起的原因,可以是以下任意值:
UIWebViewNavigationTypeLinkClicked
UIWebViewNavigationTypeFormSubmitted
UIWebViewNavigationTypeBackForward
UIWebViewNavigationTypeReload
UIWebViewNavigationTypeFormResubmitted
UIWebViewNavigationTypeOther
UIWebView控件加載網(wǎng)頁(yè)的監(jiān)聽(tīng)函數(shù)方法:
-(void)webViewDidStartLoad:(UIWebView*)webView ;//當(dāng)網(wǎng)頁(yè)視圖已經(jīng)開(kāi)始加載一個(gè)請(qǐng)求后揍堰,得到通知鹏浅。
-(void)webViewDidFinishLoad:(UIWebView*)webView ;//當(dāng)網(wǎng)頁(yè)視圖結(jié)束加載一個(gè)請(qǐng)求之后嗅义,得到通知。
-(void)webView:(UIWebView*)webView DidFailLoadWithError:(NSError*)error;//當(dāng)在請(qǐng)求加載中發(fā)生錯(cuò)誤時(shí)隐砸,得到通知之碗。會(huì)提供一個(gè)NSSError對(duì)象,以標(biāo)識(shí)所發(fā)生錯(cuò)誤類型季希。
以上是IOS中UIWebView的基礎(chǔ)使用要點(diǎn)詳解褪那,接下來(lái)一些UIWebView的常用注意點(diǎn)。
二式塌、IOS中UIWebView常用注意點(diǎn):
1博敬、與UIWebView進(jìn)行交互,調(diào)用web頁(yè)面中的需要傳參的函數(shù)時(shí)珊搀,參數(shù)需要帶單引號(hào)冶忱,或者雙引號(hào)(雙引號(hào)需要進(jìn)行轉(zhuǎn)義在轉(zhuǎn)義字符前加\),在傳遞json字符串時(shí)不需要加單引號(hào)或雙引號(hào):
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *sendJsStr=[NSString stringWithFormat:@"openFile(\"%@\")",jsDocPathStr];
[webView stringByEvaluatingJavaScriptFromString:sendJsStr];
}
2、在該代理方法中判斷與webView的交互境析,可通過(guò)html里定義的協(xié)議實(shí)現(xiàn):
- (BOOL)webView:(UIWebView*)webView
shouldStartLoadWithRequest:(NSURLRequest*)request
navigationType:(UIWebViewNavigationType)navigationType
3囚枪、只有在webView加載完畢之后在能夠調(diào)用對(duì)應(yīng)頁(yè)面中的js方法。(對(duì)應(yīng)方法如第1條).
4劳淆、為webView添加背景圖片:
approvalWebView.backgroundColor=[UIColor clearColor];
approvalWebView.opaque=NO;//這句話很重要链沼,webView是否是不透明的,no為透明 在webView下添加個(gè)imageView展示圖片就可以了
5沛鸵、獲取webView頁(yè)面內(nèi)容信息:
NSString *docStr=[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.textContent"];//獲取web頁(yè)面內(nèi)容信息括勺,此處獲取的是個(gè)json字符串
SBJsonParser *parserJson=[[[SBJsonParser alloc]init]autorelease];
NSDictionary *contentDic=[parserJson objectWithString:docStr];//將json字符串轉(zhuǎn)化為字典
6、 加載本地文件的方法:
//第一種方法:
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]]];
7、將文件下載到本地址然后再用webView打開(kāi):
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];
8栏妖、處理webView展示txt文檔亂碼問(wèn)題:
if ([theType isEqualToString:@".txt"])
{
//txt分帶編碼和不帶編碼兩種乱豆,帶編碼的如UTF-8格式txt,不帶編碼的如ANSI格式txt
//不帶的吊趾,可以依次嘗試GBK和GB18030編碼
NSString* aStr = [[NSString alloc] initWithData:attachmentData encoding:NSUTF8StringEncoding];
if (!aStr)
{
//用GBK進(jìn)行編碼
aStr=[[NSString alloc] initWithData:attachmentData encoding:0x80000632];
}
if (!aStr)
{
//用GBK編碼不行,再用GB18030編碼
aStr=[[NSString alloc] initWithData:attachmentData encoding:0x80000631];
}
//通過(guò)html語(yǔ)言進(jìn)行排版
NSString* responseStr = [NSString stringWithFormat:
@""
""
""
""
""
"