UIWebView中Html中用JS调用OC方法及OC执行JS代码

来源:互联网 发布:学习c语言编程 编辑:程序博客网 时间:2024/05/01 20:39


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. }  
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. }  
0 0
原创粉丝点击