51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1711|回复: 0
打印 上一主题 下一主题

与webView进行交互,webView小记

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2019-3-6 15:51:48 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
一、与webView进行交互,调用web页面中的需要传参的函数时,参数需要带单引号,或者双引号(双引号需要进行转义在转义字符前加\),在传递json字符串时不需要加单引号或双引号。
  1. -(void)webViewDidFinishLoad:(UIWebView *)webView
  2. {
  3.     NSString *sendJsStr=[NSString stringWithFormat:@"openFile(\"%@\")",jsDocPathStr];
  4.   [webView stringByEvaluatingJavaScriptFromString:sendJsStr];
  5. }
复制代码
2、在该代理方法中判断与webView的交互,可通过html里定义的协议实现
  1. - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
复制代码
3、只有在webView加载完毕之后在能够调用对应页面中的js方法。(对应方法如第一条)

4、为webView添加背景图片

  1. approvalWebView.backgroundColor=[UIColor clearColor];
  2. approvalWebView.opaque=NO;//这句话很重要,webView是否是不透明的,no为透明
  3. //在webView下添加个imageView展示图片就可以了
复制代码

5、获取webView页面内容信息

  1. NSString *docStr=[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.textContent"];//获取web页面内容信息,此处获取的是个json字符串

  2. SBJsonParser *parserJson=[[[SBJsonParser alloc]init]autorelease];

  3. NSDictionary *contentDic=[parserJson objectWithString:docStr];//将json字符串转化为字典
复制代码

6、加载本地文件的方法

  1. 第一种方法:
  2. NSString* path = [[NSBundle mainBundle] pathForResource:name ofType:@"html" inDirectory:@"mobile"];//mobile是根目录,name是文件名称,html是文件类型
  3. [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]]; //加载本地文件
  4. 第二种方法:
  5. NSString *resourcePath = [[NSBundle mainBundle] resourcePath];  
  6. NSString *filePath = [resourcePath stringByAppendingPathComponent:@"mobile.html"];  
  7. NSString *htmlstring=[[NSString alloc] initWithContentsOfFile:filePath  encoding:NSUTF8StringEncoding error:nil];  
  8. [uiwebview loadHTMLString:htmlstring baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
复制代码

7、将文件下载到本地址然后再用webView打开

  1. NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];
  2. self.filePath = [resourceDocPath stringByAppendingPathComponent:[NSString stringWithFormat:@"maydoc%@",docType]];
  3. NSData *attachmentData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:theUrl]];
  4. [attachmentData writeToFile:filePath atomically:YES];
  5. NSURL *url = [NSURL fileURLWithPath:filePath];
  6. NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
  7. [attachmentWebView loadRequest:requestObj];
  8. //删除指定目录下的文件

  9. NSFileManager *magngerDoc=[NSFileManager defaultManager];
  10. [magngerDoc removeItemAtPath:filePath error:nil];
复制代码

8、处理webView展示txt文档乱码问题

  1. if ([theType isEqualToString:@".txt"])
  2. {
  3. //txt分带编码和不带编码两种,带编码的如UTF-8格式txt,不带编码的如ANSI格式txt
  4. //不带的,可以依次尝试GBK和GB18030编码
  5. NSString* aStr = [[NSString alloc] initWithData:attachmentData encoding:NSUTF8StringEncoding];
  6. if (!aStr)
  7. {
  8. //用GBK进行编码
  9. aStr=[[NSString alloc] initWithData:attachmentData encoding:0x80000632];
  10. }
  11. if (!aStr)
  12. {
  13. //用GBK编码不行,再用GB18030编码
  14. aStr=[[NSString alloc] initWithData:attachmentData encoding:0x80000631];
  15. }
  16. //通过html语言进行排版
  17. NSString* responseStr = [NSString stringWithFormat:
  18.                                  @"<HTML>"
  19.                                  "<head>"
  20.                                  "<title>Text View</title>"
  21.                                  "</head>"
  22.                                  "<BODY>"
  23.                                  "<pre>"
  24.                                  "%@"
  25.                                  "/pre>"
  26.                                  "</BODY>"
  27.                                  "</HTML>",
  28.                                  aStr];
  29. [attachmentWebView loadHTMLString:responseStr baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
  30.         return;
  31. }
复制代码

9 、使用webView加载本地或网络文件整个流程

  1. 1、 Loading a local PDF file into the web view

  2. - (void)viewDidLoad {
  3.     [super viewDidLoad];
  4. //从本地加载
  5.     NSString *thePath = [[NSBundle mainBundle] pathForResource:@"iPhone_User_Guide" ofType:@"pdf"];
  6.     if (thePath) {
  7.         NSData *pdfData = [NSData dataWithContentsOfFile:thePath];
  8.         [(UIWebView *)self.view loadData:pdfData MIMEType:@"application/pdf"
  9.             textEncodingName:@"utf-8" baseURL:nil];
  10.     }
  11. //从网络加载
  12. [self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]]];
  13. }
  14. 2、The web-view delegate managing network loading

  15. - (void)webViewDidStartLoad:(UIWebView *)webView
  16. {
  17.     // starting the load, show the activity indicator in the status bar
  18.     [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
  19. }

  20. - (void)webViewDidFinishLoad:(UIWebView *)webView
  21. {
  22.     // finished loading, hide the activity indicator in the status bar
  23.     [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
  24. }

  25. - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
  26. {
  27.     // load error, hide the activity indicator in the status bar
  28.     [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

  29.     // report the error inside the webview
  30.     NSString* errorString = [NSString stringWithFormat:
  31.                              @"<html><center><font size=+5 color='red'>An error occurred:<br>%@</font></center></html>",
  32.                              error.localizedDescription];
  33.     [self.myWebView loadHTMLString:errorString baseURL:nil];
  34. }
  35. 3、Stopping a load request when the web view is to disappear

  36. - (void)viewWillDisappear:(BOOL)animated
  37. {
  38.     if ( [self.myWebView loading] ) {
  39.         [self.myWebView stopLoading];
  40.     }
  41.     self.myWebView.delegate = nil;    // disconnect the delegate as the webview is hidden
  42.     [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
  43. }
  44. /************/
  45. 引用自苹果官方文档(displaying web content)
复制代码

10、查找webView中的scrollview

  1. - (void) addScrollViewListener
  2. {
  3.     UIScrollView* currentScrollView;
  4.     for (UIView* subView in self.webView.subviews) {
  5.         if ([subView isKindOfClass:[UIScrollView class]]) {
  6.             currentScrollView = (UIScrollView*)subView;
  7.             currentScrollView.delegate = self;
  8.         }
  9.     }
  10. }
复制代码

11、去掉webView的阴影,做成类似scrollView

  1. - (void)clearBackgroundWithColor:(UIColor*)color
  2. {
  3.   // 去掉webview的阴影
  4.   self.backgroundColor = color;
  5.   for (UIView* subView in [self subviews])
  6.   {
  7.     if ([subView isKindOfClass:[UIScrollView class]]) {
  8.       for (UIView* shadowView in [subView subviews])
  9.       {
  10.         if ([shadowView isKindOfClass:[UIImageView class]]) {
  11.           [shadowView setHidden:YES];
  12.         }
  13.       }
  14.     }
  15.   }

  16. }
复制代码

12、取消长按webView上的链接弹出actionSheet的问题

  1. -(void)webViewDidFinishLoad:(UIWebView *)webView
  2. {
  3. [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout = 'none';"];
  4. }
复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-5-10 23:26 , Processed in 0.064845 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表