(iOS开发)webView的使用与注意事项
2016-09-20 14:17
766 查看
webView的简单使用:
1、
myWebView =[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight+64)];
myWebView.backgroundColor=[UIColor colorWithRed:248/255.0 green:244/255.0 blue:232/255.0 alpha:1.0];
myWebView.delegate=self;
myWebView.userInteractionEnabled=YES;
[myWebView setOpaque:YES];//opaque YES透明
// [myWebView setScalesPageToFit:YES];//自动缩放以适应屏幕
// myWebView.scrollView.bounces = NO;//不允许漏出空白背景
[self.view addSubview:myWebView];
//加载网页的方式
[myWebView loadHTMLString:_urlString baseURL:nil];
//其他加载网页的方式
//1.创建并加载远程网页
//NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
//[myWebView loadRequest:[NSURLRequest requestWithURL:url]];
//2.加载本地文件资源
/* NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:request];*/
//3.读入一个HTML,直接写入一个HTML代码
//NSString *htmlPath = [[[NSBundle mainBundle]bundlePath]stringByAppendingString:@"webapp/loadar.html"];
//NSString *htmlString = [NSString stringWithContentsOfURL:htmlPath encoding:NSUTF8StringEncoding error:NULL];
//[myWebView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:htmlPath]];
H5交互部分:
参考文章:http://www.jianshu.com/p/769b046edc3f
注意事项:
1、webView上面添加tap等手势的时候必须添加:(web
view手势)
2、设置显示字体大小
3、解决双击放大的问题
:
1、
myWebView =[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight+64)];
myWebView.backgroundColor=[UIColor colorWithRed:248/255.0 green:244/255.0 blue:232/255.0 alpha:1.0];
myWebView.delegate=self;
myWebView.userInteractionEnabled=YES;
[myWebView setOpaque:YES];//opaque YES透明
// [myWebView setScalesPageToFit:YES];//自动缩放以适应屏幕
// myWebView.scrollView.bounces = NO;//不允许漏出空白背景
[self.view addSubview:myWebView];
//加载网页的方式
[myWebView loadHTMLString:_urlString baseURL:nil];
//其他加载网页的方式
//1.创建并加载远程网页
//NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
//[myWebView loadRequest:[NSURLRequest requestWithURL:url]];
//2.加载本地文件资源
/* NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:request];*/
//3.读入一个HTML,直接写入一个HTML代码
//NSString *htmlPath = [[[NSBundle mainBundle]bundlePath]stringByAppendingString:@"webapp/loadar.html"];
//NSString *htmlString = [NSString stringWithContentsOfURL:htmlPath encoding:NSUTF8StringEncoding error:NULL];
//[myWebView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:htmlPath]];
H5交互部分:
//如果是H5页面里面自带的返回按钮处理如下: #pragma mark - webViewDelegate - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSString * requestString = [[request URL] absoluteString];//获取请求的绝对路径 requestString = [requestString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //获取H5页面里面按钮的操作方法,根据这个进行判断返回是内部的还是push的上一级页面 if ([requestString hasPrefix:@"goback:"]) { [self.navigationController popViewControllerAnimated:YES]; }else{ [self.webView goBack]; } return YES; } //获取当前页面的title和url - (void)webViewDidFinishLoad:(UIWebView *)webView { //隐藏HUD菊花 NSString * title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];//获取当前页面的title self.title = title; //获取当前网页的html self.currentURL = webView.request.URL.absoluteString; NSLog(@"title-%@--url-%@--",self.title,self.currentURL); NSString *lJs = @"document.documentElement.innerHTML"; self.currentHTML = [webView stringByEvaluatingJavaScriptFromString:lJs]; }
参考文章:http://www.jianshu.com/p/769b046edc3f
注意事项:
1、webView上面添加tap等手势的时候必须添加:(web
view手势)
//必须实现 否则无法触发webView的点击事件 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES; }
2、设置显示字体大小
//方法中加入js代码 设置显示字体大小是原来大小的多少 - (void)webViewDidFinishLoad:(UIWebView *)webView { NSString *str = @"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '100%'"; [myWebView stringByEvaluatingJavaScriptFromString:str]; }
3、解决双击放大的问题
_webView.multipleTouchEnabled=NO - (UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView { returnnil; }
:
相关文章推荐
- (iOS开发)UIScrollView的使用以及注意事项
- iOS开发:关于automaticallyAdjustsScrollViewInsets的使用和注意事项
- Android开发 WebView使用注意事项
- cocoa WebView使用注意事项
- 【iOS开发-34】自动释放池@autoreleasepool的使用注意事项以及ARC机制——面试必考内容
- ios开发入门- WebView使用
- iOS开发万能的transform注意事项,以及viewWithTag以及.png后缀可以省略的一些知识
- ios开发入门- WebView使用
- iOS开发之Objective-C(Swift)与JavaScript交互·WebViewJavascriptBridge使用篇
- iOS开发之AFN的使用和注意事项
- IOS 开发学习34 使用webview
- ios开发入门- WebView使用
- cocoa WebView使用注意事项
- UIScrollView的基本使用方法和注意事项 - iOS - UI基础知识总结10
- 使用webview加载,注意事项
- Android 4.4 中 WebView 使用注意事项
- iOS开发中WebView的使用
- Android 4.4 中 WebView 使用注意事项
- webview使用总结及注意事项
- Android 4.4 中 WebView 使用注意事项