• 网页 js


    JS与iOS之间的通信,主要运用两个方法:(PhoneGap框架也是基于此原理)

    1、UIWebView的  stringByEvaluatingJavaScriptFromString方法

    2、UIWebViewDelegate的

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType方法

     

    示例:

    上部分是一个UIWebView,实现UIWebViewDelegate

     

     

    1. - (void)viewDidLoad  
    2. {  
    3.     [super viewDidLoad];  
    4.     NSString *path = [[NSBundle mainBundle] pathForResource:@"jm/info" ofType:@"html"];  
    5.     NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]];  
    6.     [self.webView loadRequest:request];  
    7. }  

    1. #pragma mark - UIWebViewDelegate  
    2.   
    3. - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType  
    4. {  
    5.     if([request.mainDocumentURL.relativePath isEqualToString:@"/getInfo/name"])  
    6.     {  
    7.         NSString *info = [[UIDevice currentDevice] name];  
    8.         NSString *js = [NSString stringWithFormat:@"showInfo("name","%@")",info];  
    9.         [self.webView stringByEvaluatingJavaScriptFromString:js];  
    10.         return false;  
    11.     }  
    12.     if([request.mainDocumentURL.relativePath isEqualToString:@"/getInfo/systemVersion"])  
    13.     {  
    14.         NSString *info = [[UIDevice currentDevice] systemVersion];  
    15.         NSString *js = [NSString stringWithFormat:@"showInfo("systemVersion","%@")",info];  
    16.         [self.webView stringByEvaluatingJavaScriptFromString:js];  
    17.         return false;  
    18.     }  
    19.     return true;  
    20. }  


    JS代码:

      1. <!DOCTYPE html>  
      2. <html>  
      3. <head>  
      4. <title>city</title>  
      5. <meta charset="utf-8">  
      6. <meta name="viewport" content="width=device-width, initial-scale=1">  
      7. <link rel="stylesheet" href="jquery.mobile-1.0.css"/>  
      8. <script type="text/javascript" src="jquery.js"></script>  
      9. <script type="text/javascript" src="jquery.mobile-1.0.js"></script>  
      10. <script>  
      11. function getInfo(name)  
      12. {  
      13.     window.location = "/getInfo/"+name;  
      14. }  
      15.   
      16. function showInfo(id,info)  
      17. {  
      18.     $("p#"+id).html(info);  
      19. }  
      20. </script>  
      21. </head>  
      22. <body>  
      23. <div data-role="page">  
      24.     <div data-role="content">  
      25.         <h2>Divice Info</h2>  
      26.         <div data-role="collapsible-set" data-theme="c" data-content-theme="d">  
      27.             <div data-role="collapsible">  
      28.                 <h3 onclick="getInfo('name')">name</h3>  
      29.                 <id="name"></p>  
      30.             </div>  
      31.             <div data-role="collapsible">  
      32.                 <h3 onclick="getInfo('systemVersion')">systemVersion</h3>  
      33.                 <id="systemVersion"></p>  
      34.             </div>  
      35.         </div>  
      36.     </div>  
      37. </div>  
      38. </body>  
      39. </html>  
  • 相关阅读:
    Lock wait timeout exceeded; try restarting transaction linux设置mysql innodb_lock_wait_timeout
    用NaviCat创建存储过程批量添加测试数据
    mysql存储过程语法及实例
    mysql中迅速插入百万条测试数据的方法
    mysql学习之通过文件创建数据库以及添加数据
    有用的网站集合
    VMware Workstation虚拟磁盘文件备份或移植
    CoreData修改了数据模型报错 The model used to open the store is incompatible with the one used to create the store
    iOS中自定义UITableViewCell的用法
    golang make()的第三个参数
  • 原文地址:https://www.cnblogs.com/lhx2015/p/4663504.html
Copyright © 2020-2023  润新知