与Webviwe进行交互,webview小记

  • Post author:
  • Post category:其他


一、与

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]]];



版权声明:本文为Calvi_Klein原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。