UIWebview JS 交互

mark! UIwebview 通过jquery 和用户交互信息:

  1. NSstring *html=@"<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html xmlns='http://www.w3.org/1999/xhtml'><head runat='server'><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'><title>阿速度发送地方</title><script src='http://d5mt.xj169.com/Miaosha/images/jquery-1.7.2.min.js' type='text/javascript'></script><meta http-equiv='Cache-Control' content='must-revalidate,no-cache'><meta id='viewport'   name='viewport' content='width=device-width, maximum-scale=2,minimum-scale=1,initial-scale=1.000000'><meta name='MobileOptimized' content='240'></head><body> <script type='text/javascript'>$(document).ready(function() {shows(); $('#imgurl').click(function (){shows();});}); function shows(){alert('123456789');}</script> <img style='height:20px; width:80px;' id='imgurl' src='https://www.google.com.hk/images/srpr/logo11w.png'></img><p>1.asdfasdfasdfasdfasdfasdf</p><p>2.asdfasdfasdfasdfasdfasdf</p><p>3.asdfasdfasdfasdfasdfasdf</p><p>4.asdfasdfasdfasdfasdfasdf</p><p>5.asdfasdfasdfasdfasdfasdf</p><p>1.asdfasdfasdfasdfasdfasdf</p><p>2.asdfasdfasdfasdfasdfasdf</p><p>3.asdfasdfasdfasdfasdfasdf</p><p>4.asdfasdfasdfasdfasdfasdf</p><p>5.asdfasdfasdfasdfasdfasdf</p><p>1.asdfasdfasdfasdfasdfasdf</p><p>2.asdfasdfasdfasdfasdfasdf</p><p>3.asdfasdfasdfasdfasdfasdf</p><p>4.asdfasdfasdfasdfasdfasdf</p><p>5.asdfasdfasdfasdfasdfasdf</p><p>1.asdfasdfasdfasdfasdfasdf</p><p>2.asdfasdfasdfasdfasdfasdf</p><p>3.asdfasdfasdfasdfasdfasdf</p><p>4.asdfasdfasdfasdfasdfasdf</p><p>5.asdfasdfasdfasdfasdfasdf</p><p>1.asdfasdfasdfasdfasdfasdf</p><p>2.asdfasdfasdfasdfasdfasdf</p><p>3.asdfasdfasdfasdfasdfasdf</p><p>4.asdfasdfasdfasdfasdfasdf</p><p>5.asdfasdfasdfasdfasdfasdf</p></body></html>";  
  2.      [web loadHTMLString:html baseURL:nil];  


这样 内容加载完后 自动执行弹窗 还有点击图片也可以弹窗, 这时候弹出提示框回属于系统自带的内容不是我们需要的,那么可以通过

 

- (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame;  这个方法来处理;具体如下:

 

  1. - (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {    
  2.         
  3.         
  4.     UIAlertView* customAlert = [[UIAlertView alloc] initWithTitle:@"提示信息" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];    
  5.         
  6.     [customAlert show];    
  7.         
  8.     [customAlert autorelease];    
  9.         
  10. }    

 如此,我们还可以通过jquery 调用程序事件,前提是 你的触发事件要输出这个串:{window.location='image';} 通过下面的方法 调用

 

    1. -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType  
    2. {  
    3.       
    4.     if([request.mainDocumentURL.relativePath isEqualToString:@"/image"])  
    5.     {  
    6. NSLog(@"点击图片");  
    7.     }  
    8. ]
posted on 2015-09-11 16:45  廖利君  阅读(165)  评论(0编辑  收藏  举报