js 中的alert title 在 iOS 中如何消失(未测试)

来源:互联网 发布:怎样看网络直播送礼物 编辑:程序博客网 时间:2024/06/13 18:27

在iOS app 中经常会嵌套html 代码, 然后  js 的alert 的title 怎么修改呢,不修改的话很丑陋,用户无法接受。如下:


 但是现在有了好的办法就是用 iOS native 的uiwebview 的扩展方法来监听 js的alert 然后自定义 alert 的title ,如下:



这样就可以自定义, 也比较好看了。

方法是 在 你加在webview 的 m 方法中加入:

[html] view plaincopy
  1. @interface UIWebView (JavaScriptAlert)  
  2.   
  3. - (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame;  
  4.   
  5. @end  
  6.   
  7. @implementation UIWebView (JavaScriptAlert)  
  8.   
  9. - (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {  
  10.       
  11.       
  12.     UIAlertView* customAlert = [[UIAlertView alloc] initWithTitle:@"我是JS Alert"  
  13.                                                           message:message  
  14.                                                          delegate:nil  
  15.                                                 cancelButtonTitle:@"确定"  
  16.                                                 otherButtonTitles:nil];  
  17.       
  18.     [customAlert show];  
  19.     [customAlert release];  
  20.       
  21. }  
  22. @end  

这样就ok了! 哈哈!

html 代码 :

[html] view plaincopy
  1. <html>  
  2. <head>  
  3.   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />   
  4.   <title>Webview document</title>  
  5.     <script type="text/javascript">  
  6.         function change_header()  
  7.         {  
  8.             alert("不错的一天");  
  9.         }  
  10.      </script>  
  11. </head>  
  12.   
  13. <body style="background-color:transparent; color:white;">  
  14.     <h4>UIWebView HTML document</h4>  
  15.     <button onclick="change_header()">按钮</button>  
  16. </body>  
  17.   
  18. </html>  
0 0
原创粉丝点击