51Testing软件测试论坛
标题: 与webView进行交互,webView小记 [打印本页]
作者: 哈士奇的罪恶 时间: 2019-3-6 15:51
标题: 与webView进行交互,webView小记
一、与webView进行交互,调用web页面中的需要传参的函数时,参数需要带单引号,或者双引号(双引号需要进行转义在转义字符前加\),在传递json字符串时不需要加单引号或双引号。
- -(void)webViewDidFinishLoad:(UIWebView *)webView
- {
- NSString *sendJsStr=[NSString stringWithFormat:@"openFile(\"%@\")",jsDocPathStr];
- [webView stringByEvaluatingJavaScriptFromString:sendJsStr];
- }
复制代码 2、在该代理方法中判断与webView的交互,可通过html里定义的协议实现
- - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
复制代码 3、只有在webView加载完毕之后在能够调用对应页面中的js方法。(对应方法如第一条) 4、为webView添加背景图片
- approvalWebView.backgroundColor=[UIColor clearColor];
- approvalWebView.opaque=NO;//这句话很重要,webView是否是不透明的,no为透明
- //在webView下添加个imageView展示图片就可以了
复制代码 5、获取webView页面内容信息
- NSString *docStr=[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.textContent"];//获取web页面内容信息,此处获取的是个json字符串
- SBJsonParser *parserJson=[[[SBJsonParser alloc]init]autorelease];
- NSDictionary *contentDic=[parserJson objectWithString:docStr];//将json字符串转化为字典
复制代码 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打开
- 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文档乱码问题
- if ([theType isEqualToString:@".txt"])
- {
- //txt分带编码和不带编码两种,带编码的如UTF-8格式txt,不带编码的如ANSI格式txt
- //不带的,可以依次尝试GBK和GB18030编码
- NSString* aStr = [[NSString alloc] initWithData:attachmentData encoding:NSUTF8StringEncoding];
- if (!aStr)
- {
- //用GBK进行编码
- aStr=[[NSString alloc] initWithData:attachmentData encoding:0x80000632];
- }
- if (!aStr)
- {
- //用GBK编码不行,再用GB18030编码
- aStr=[[NSString alloc] initWithData:attachmentData encoding:0x80000631];
- }
- //通过html语言进行排版
- NSString* responseStr = [NSString stringWithFormat:
- @"<HTML>"
- "<head>"
- "<title>Text View</title>"
- "</head>"
- "<BODY>"
- "<pre>"
- "%@"
- "/pre>"
- "</BODY>"
- "</HTML>",
- aStr];
- [attachmentWebView loadHTMLString:responseStr baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
- return;
- }
复制代码 9 、使用webView加载本地或网络文件整个流程
- 1、 Loading a local PDF file into the web view
- - (void)viewDidLoad {
- [super viewDidLoad];
- //从本地加载
- NSString *thePath = [[NSBundle mainBundle] pathForResource:@"iPhone_User_Guide" ofType:@"pdf"];
- if (thePath) {
- NSData *pdfData = [NSData dataWithContentsOfFile:thePath];
- [(UIWebView *)self.view loadData:pdfData MIMEType:@"application/pdf"
- textEncodingName:@"utf-8" baseURL:nil];
- }
- //从网络加载
- [self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]]];
- }
- 2、The web-view delegate managing network loading
- - (void)webViewDidStartLoad:(UIWebView *)webView
- {
- // starting the load, show the activity indicator in the status bar
- [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
- }
-
- - (void)webViewDidFinishLoad:(UIWebView *)webView
- {
- // finished loading, hide the activity indicator in the status bar
- [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
- }
-
- - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
- {
- // load error, hide the activity indicator in the status bar
- [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
-
- // report the error inside the webview
- NSString* errorString = [NSString stringWithFormat:
- @"<html><center><font size=+5 color='red'>An error occurred:<br>%@</font></center></html>",
- error.localizedDescription];
- [self.myWebView loadHTMLString:errorString baseURL:nil];
- }
- 3、Stopping a load request when the web view is to disappear
- - (void)viewWillDisappear:(BOOL)animated
- {
- if ( [self.myWebView loading] ) {
- [self.myWebView stopLoading];
- }
- self.myWebView.delegate = nil; // disconnect the delegate as the webview is hidden
- [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
- }
- /************/
- 引用自苹果官方文档(displaying web content)
复制代码10、查找webView中的scrollview
- - (void) addScrollViewListener
- {
- UIScrollView* currentScrollView;
- for (UIView* subView in self.webView.subviews) {
- if ([subView isKindOfClass:[UIScrollView class]]) {
- currentScrollView = (UIScrollView*)subView;
- currentScrollView.delegate = self;
- }
- }
- }
复制代码11、去掉webView的阴影,做成类似scrollView
- - (void)clearBackgroundWithColor:(UIColor*)color
- {
- // 去掉webview的阴影
- self.backgroundColor = color;
- for (UIView* subView in [self subviews])
- {
- if ([subView isKindOfClass:[UIScrollView class]]) {
- for (UIView* shadowView in [subView subviews])
- {
- if ([shadowView isKindOfClass:[UIImageView class]]) {
- [shadowView setHidden:YES];
- }
- }
- }
- }
- }
复制代码 12、取消长按webView上的链接弹出actionSheet的问题
- -(void)webViewDidFinishLoad:(UIWebView *)webView
- {
- [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout = 'none';"];
- }
复制代码
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) |
Powered by Discuz! X3.2 |