網頁 js,網頁js

來源:互聯網
上載者:User

網頁 js,網頁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.                 <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>  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.