·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> 网页js

网页js

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

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.                 <p id="name"></p>  
    30.             </div>  
    31.             <div data-role="collapsible">  
    32.                 <h3 onclick="getInfo('systemVersion')">systemVersion</h3>  
    33.                 <p id="systemVersion"></p>  
    34.             </div>  
    35.         </div>  
    36.     </div>  
    37. </div>  
    38. </body>  
    39. </html>