码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2015-09-11 20:44:27      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

1.HTML页面

 1 <html>
 2 
 3     <head>
 4 
 5        <title>HTML中用JS调用OC方法</title>
 6 
 7         <meta http-equiv="Content-Type"content="text/html; charset=UTF-8">
 8 
 9        <script>
10 
11            function test()
12 
13             {
14 
15                 alert("test alert...");
16 
17                return "abcd";
18 
19             }
20 
21         </script>
22 
23     <body>
24 
25         
26 
27        <br/>
28 
29        <br/>
30 
31        <br/>
32 
33         <a href=‘ios://openMyAlbum‘>打开相机</a><br><br/>
34 
35             
36 
37         <a href = ‘ios://openMyCamera‘>打开相册</a>
38 
39                 
40 
41     </body>
42 
43     
44 
45 </html>

 

 

2.oc页面代码

  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     } 

 

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

标签:

原文地址:http://www.cnblogs.com/liaods/p/4801980.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!