Step —?UIWebView
1、UIWebView常用方法
//1寇壳、聲明
@property(nonatomic,strong)?UIWebView*webView;
//1讥蔽、代理
UIWebViewDelegate
?/*2奇徒、ATS配置:
???? *info.plist添加字段:App Transport Security Settings - 字典
???? *Allow Arbitrary Loads - Boolean 設(shè)置為:YES
???? */
?/*3、幾種方法區(qū)別
??? - (void)loadRequest:(NSURLRequest *)request;一般加載指定地址的網(wǎng)頁(yè)巍膘,也可以加載本地文件(html、world芋簿、PDF峡懈、txt等)
??? - (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL;//直接用來(lái)加載html代碼
??? - (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL;//加載data
??? */
?/*4 加載本地文件
???? 1.加載網(wǎng)絡(luò)地址
??? NSString *urlStr = @"http://www.reibang.com";
??? NSURL *url = [NSURL URLWithString:urlStr];
??? */
?/*2.加載本地 html
??? NSString *path = [[NSBundle mainBundle] pathForResource:@"file_1" ofType:@"html"];
??? NSURL* url = [NSURL? fileURLWithPath:path];
??? */
?/*3.加載本地 world
???? NSString *path = [[NSBundle mainBundle] pathForResource:@"world" ofType:@"docx"];
???? NSURL *url = [NSURL fileURLWithPath:path];
???? */
?/*4.加載本地 pdf
??? NSString *path = [[NSBundle mainBundle] pathForResource:@"pdf" ofType:@"pdf"];
???? NSURL *url = [NSURL fileURLWithPath:path];
???? */
?/*4.加載本地 圖片
???? NSString *path = [[NSBundle mainBundle] pathForResource:@"photo" ofType:@"png"];
???? NSURL *url = [NSURL fileURLWithPath:path];
???? */
?//5.加載本地 txt
?NSString*path = [[NSBundlemainBundle]?pathForResource:@"txt"ofType:@"txt"];
?NSURL*url = [NSURLfileURLWithPath:path];
?NSURLRequest*request = [NSURLRequestrequestWithURL:url];
?//6、創(chuàng)建
?self.webView= [[UIWebViewalloc]?initWithFrame:self.view.bounds];
?self.webView.backgroundColor= [UIColorwhiteColor];
?self.webView.delegate=?self;
??? [self.webViewloadRequest:request];
??? [self.viewaddSubview:self.webView];
?//7与斤、屬性設(shè)置
?//網(wǎng)頁(yè)是否可以縮放
?self.webView.scalesPageToFit=?YES;
?// 設(shè)置某些數(shù)據(jù)變?yōu)殒溄有问椒究担@個(gè)枚舉可以設(shè)置如電話號(hào),地址撩穿,郵箱等轉(zhuǎn)化為鏈接
?self.webView.dataDetectorTypes=?UIDataDetectorTypeAll;
?//設(shè)置是否使用內(nèi)嵌播放器播放視頻
?self.webView.allowsInlineMediaPlayback=?YES;
?//設(shè)置視頻/音頻是否自動(dòng)播放
?self.webView.mediaPlaybackRequiresUserAction=?YES;
?//設(shè)置音頻播放是否支持 ari play 功能
?self.webView.mediaPlaybackAllowsAirPlay=?YES;
?//設(shè)置是否將數(shù)據(jù)加載內(nèi)存后渲染界面
?self.webView.suppressesIncrementalRendering=?YES;
?//設(shè)置是否打開(kāi)keyboard交互
?self.webView.keyboardDisplayRequiresUserAction=?YES;
?//設(shè)置分頁(yè)模式(網(wǎng)頁(yè)大小超出self.view)
?self.webView.paginationMode=?UIWebPaginationModeUnpaginated;
?//設(shè)置每頁(yè)的長(zhǎng)度
?self.webView.pageLength=?self.view.frame.size.height;
?//設(shè)置每頁(yè)的間距
?self.webView.gapBetweenPages=?self.view.frame.size.width;
?/*8磷支、常用方法
???? - (void)reload;//刷新
???? - (void)stopLoading;//暫停
???? - (void)goBack;//返回
???? - (void)goForward;//前進(jìn)
???? */
//9、代理
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType{
?NSLog(@"是否加載 %s",__func__);
?returnYES;
}
- (void)webViewDidStartLoad:(UIWebView*)webView{
?NSLog(@"開(kāi)始 %s",__func__);
}
- (void)webViewDidFinishLoad:(UIWebView*)webView{
?NSLog(@"完成 %s",__func__);
}
- (void)webView:(UIWebView*)webView didFailLoadWithError:(NSError*)error{
?NSLog(@"失敗 %s",__func__);
}
2食寡、特殊用法
1.打電話
?NSString*strTel =[[NSStringalloc]?initWithFormat:@"tel:%@",@"182xxxx5502"];
?UIWebView*callWebview = [[UIWebViewalloc]?init];
[callWebview?loadRequest:[NSURLRequestrequestWithURL:[NSURLURLWithString:strTel]]];
??? [self.viewaddSubview:callWebview];
2.safari打開(kāi)網(wǎng)頁(yè)中的地址
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType{
?NSLog(@"是否加載 %s",__func__);
?//在網(wǎng)頁(yè)中打開(kāi)safari
?if(navigationType ==?UIWebViewNavigationTypeLinkClicked) {
?//參數(shù)1雾狈;打開(kāi)APP的URL
?//參數(shù)2:可選字典參數(shù)。傳入一個(gè)空字典可以達(dá)到openURL:一樣的行為抵皱。
?//參數(shù)3:執(zhí)行成功后completionhandler在主隊(duì)列中回調(diào)善榛。
??????? [[UIApplicationsharedApplication]?openURL:[request?URL]?options:@{}completionHandler:nil];
?returnNO;
??? }
3.獲取web內(nèi)容
- (void)webViewDidFinishLoad:(UIWebView*)webView {
?/*
??? NSString *context=[webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
???? NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];//獲取當(dāng)前頁(yè)面的title
???? NSString *html = @"document.documentElement.innerHTML";//獲取當(dāng)前網(wǎng)頁(yè)的html
???? NSString *html2 = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('creditNum').innerText"];//獲取id為creditNum標(biāo)簽的值
???? NSString *html3 = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('zmScore').value"];//獲取標(biāo)簽為id為zmScore的value
???? NSString *html4 = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('p')[0].innerText"];//獲取標(biāo)簽第一個(gè)p的值
???? NSString *html5 = [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerText"];//獲取body里面的全部?jī)?nèi)容
????? NSLog(@"%@",NSStringFromSelector(_cmd));
???? */
}
3.和JS交互