赞
踩
Step — UIWebView
1、UIWebView常用方法
//1、声明
@property(nonatomic,strong) UIWebView*webView;
//1、代理
UIWebViewDelegate
/*2、ATS配置:
*info.plist添加字段:App Transport Security Settings - 字典
*Allow Arbitrary Loads - Boolean 设置为:YES
*/
/*3、几种方法区别
- (void)loadRequest:(NSURLRequest *)request;一般加载指定地址的网页,也可以加载本地文件(html、world、PDF、txt等)
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL;//直接用来加载html代码
- (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL;//加载data
*/
/*4 加载本地文件
1.加载网络地址
NSString *urlStr = @"https://www.jianshu.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、创建
self.webView= [[UIWebViewalloc] initWithFrame:self.view.bounds];
self.webView.backgroundColor= [UIColorwhiteColor];
self.webView.delegate= self;
[self.webViewloadRequest:request];
[self.viewaddSubview:self.webView];
//7、属性设置
//网页是否可以缩放
self.webView.scalesPageToFit= YES;
// 设置某些数据变为链接形式,这个枚举可以设置如电话号,地址,邮箱等转化为链接
self.webView.dataDetectorTypes= UIDataDetectorTypeAll;
//设置是否使用内嵌播放器播放视频
self.webView.allowsInlineMediaPlayback= YES;
//设置视频/音频是否自动播放
self.webView.mediaPlaybackRequiresUserAction= YES;
//设置音频播放是否支持 ari play 功能
self.webView.mediaPlaybackAllowsAirPlay= YES;
//设置是否将数据加载内存后渲染界面
self.webView.suppressesIncrementalRendering= YES;
//设置是否打开keyboard交互
self.webView.keyboardDisplayRequiresUserAction= YES;
//设置分页模式(网页大小超出self.view)
self.webView.paginationMode= UIWebPaginationModeUnpaginated;
//设置每页的长度
self.webView.pageLength= self.view.frame.size.height;
//设置每页的间距
self.webView.gapBetweenPages= self.view.frame.size.width;
/*8、常用方法
- (void)reload;//刷新
- (void)stopLoading;//暂停
- (void)goBack;//返回
- (void)goForward;//前进
*/
//9、代理
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType{
NSLog(@"是否加载 %s",__func__);
returnYES;
}
- (void)webViewDidStartLoad:(UIWebView*)webView{
NSLog(@"开始 %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打开网页中的地址
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType{
NSLog(@"是否加载 %s",__func__);
//在网页中打开safari
if(navigationType == UIWebViewNavigationTypeLinkClicked) {
//参数1;打开APP的URL
//参数2:可选字典参数。传入一个空字典可以达到openURL:一样的行为。
//参数3:执行成功后completionhandler在主队列中回调。
[[UIApplicationsharedApplication] openURL:[request URL] options:@{}completionHandler:nil];
returnNO;
}
3.获取web内容
- (void)webViewDidFinishLoad:(UIWebView*)webView {
/*
NSString *context=[webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];//获取当前页面的title
NSString *html = @"document.documentElement.innerHTML";//获取当前网页的html
NSString *html2 = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('creditNum').innerText"];//获取id为creditNum标签的值
NSString *html3 = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('zmScore').value"];//获取标签为id为zmScore的value
NSString *html4 = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('p')[0].innerText"];//获取标签第一个p的值
NSString *html5 = [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerText"];//获取body里面的全部内容
NSLog(@"%@",NSStringFromSelector(_cmd));
*/
}
3.和JS交互
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。