zoukankan      html  css  js  c++  java
  • UIWebView中Html中用JS调用OC方法及OC执行JS代码


    HTML代码:

     

    <html>

        <head>

           <title>HTML中用JS调用OC方法</title>

            <meta http-equiv="Content-Type"content="text/html; charset=UTF-8">

           <script>

               function test()

                {

                    alert("test alert...");

                   return "abcd";

                }

            </script>

        <body>

            

           <br/>

           <br/>

           <br/>

            <a href='ios://openMyAlbum'>打开相机</a><br><br/>

                

            <a href = 'ios://openMyCamera'>打开相册</a>

                    

        </body>

        

    </html>

    #OBJECT-C

    [objc] view plaincopy
     
    1. -(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType  
    2.   
    3. {  
    4.       
    5.       
    6.       
    7.     NSString  
    8.       
    9.     *urlstr = request.URL.absoluteString;  
    10.       
    11.     NSRange  
    12.       
    13.     range = [urlstr  
    14.              rangeOfString:@"ios://"];  
    15.       
    16.     if  
    17.           
    18.         (range.length!=0)  
    19.     {  
    20.           
    21.         NSString  
    22.           
    23.         *method = [urlstr  
    24.                    substringFromIndex:(range.location+range.length)];  
    25.           
    26.         SEL  
    27.           
    28.         selctor = NSSelectorFromString(method);  
    29.           
    30.         [self  
    31.            
    32.          performSelector:selctor  
    33.            
    34.          withObject:nil];  
    35.           
    36.     }  
    37.       
    38.     return  
    39.       
    40.     YES;  
    41.       
    42. }  
    43.   
    44.   
    45.   
    46. -(void)openMyAlbum  
    47.   
    48. {  
    49.       
    50.     UIImagePickerController  
    51.       
    52.     *vc = [[UIImagePickerController  
    53.               
    54.             alloc]init];  
    55.       
    56.     vc.sourceType  
    57.       
    58.     = UIImagePickerControllerSourceTypePhotoLibrary;  
    59.       
    60.     [self  
    61.        
    62.      presentViewController:vc  
    63.        
    64.      animated:YES  
    65.        
    66.      completion:nil];  
    67.       
    68. }  
    69.   
    70.   
    71.   
    72. -(void)openMyCamera  
    73.   
    74. {  
    75.     [_webView stringByEvaluatingJavaScriptFromString:@"test();"];  
    76.           
    77.     return;  
    78.       
    79.     UIImagePickerController  
    80.       
    81.     *vc = [[UIImagePickerController  
    82.               
    83.             alloc]init];  
    84.       
    85.     vc.sourceType  
    86.       
    87.     = UIImagePickerControllerSourceTypeCamera;  
    88.       
    89.     [self  
    90.        
    91.      presentViewController:vc  
    92.        
    93.      animated:YES  
    94.        
    95.      completion:nil];  
    96.       
    97. }  
    98.   
    99.   
    100. - (void)viewDidLoad {  
    101.     [super viewDidLoad];  
    102.       
    103.       
    104.     _webView = [[UIWebView alloc] initWithFrame:self.view.bounds];  
    105.     [self.view addSubview:_webView];  
    106.       
    107.     NSString *path = [[NSBundle mainBundle] pathForResource:@"test.html" ofType:nil];  
    108.       
    109.     NSURL *url = [NSURL fileURLWithPath:path];  
    110.     NSURLRequest *req = [[NSURLRequest alloc] initWithURL:url];  
    111.       
    112.     _webView.delegate   = self;  
    113.     _webView.dataDetectorTypes  = UIDataDetectorTypeAll;  
    114.       
    115.     [_webView loadRequest:req];  
    116.     // Do any additional setup after loading the view, typically from a nib.  
    117. }  


    原文出处:http://blog.csdn.net/zttjhm/article/details/43304329

  • 相关阅读:
    spring mvc 获取请求中参数方式
    23种设计模式
    Liunx-Centos下安装FFmpeg
    liunx下nginx静态服务器配置SSL证书
    JDK 1.5新特性
    搭建kubenetes集群
    centos7添加虚拟IP
    Apache+tomcat配置动静分离(一个apache一个tomcat,没有做集群)
    maven使用内嵌tomcat7
    spring集成mybatis后,打印SQL语句
  • 原文地址:https://www.cnblogs.com/gaohe/p/4555192.html
Copyright © 2011-2022 走看看