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

iOS开发——网络编程OC篇&使用WebView构建HyBird应用

时间:2015-06-06 00:08:37      阅读:345      评论:0      收藏:0      [点我收藏+]

标签:

使用WebView构建HyBird应用

HyBird是一种本地技术与Web相结合,能过实现跨平台的移动应用开发,最常用的一个框架:PhoneGap

一:首先,写好html代码

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
 5         <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
 6         <script>
 7             //从iOS对象中调用
 8             function  helloWorld(msg) {
 9                 document.getElementById(message).innerHTML = msg;
10             }
11             //调用iOS对象
12             function showAndroidDialog(msg) {
13                 var myJSONObject = new Object();
14                 myJSONObject.title = HelloWorld;
15                 myJSONObject.message = msg;
16                 var JSONString = JSON.stringify(myJSONObject);
17                 var uri = gap://XXXClass.XXXmethod# + JSONString;
18                 window.location = uri;
19             }
20          </script>
21         
22     </head>
23     <body>
24         <h2>iOS本地与Web互相调用</h2>
25         <button onclick=showAndroidDialog("JS to iOS 对象")>调用iOS对象</button>
26         <br><br>
27         <div  id=message></div>
28     </body>
29 </html>

二:在iOS中调用javaScript代码:

 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4     // Do any additional setup after loading the view, typically from a nib.
 5     
 6     self.webView.delegate = self;
 7     NSString *path = [[NSBundle mainBundle] pathForResource:@"www/index" ofType:@"html"];
 8     [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath: path]]];
 9 }
10 
11 - (void)didReceiveMemoryWarning
12 {
13     [super didReceiveMemoryWarning];
14     // Dispose of any resources that can be recreated.
15 }
16 
17 #pragma mark UIWebViewDelegate
18 
19 - (void)webViewDidFinishLoad:(UIWebView *)webView
20 {
21     [self.webView stringByEvaluatingJavaScriptFromString:@"helloWorld(‘从iOS对象中调用JS Ok.‘)"];
22 }
23 
24 
25 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
26 {
27     NSString *actionType = request.URL.host;
28     NSString *scheme = request.URL.scheme;
29     NSString *fragment = [request.URL.fragment URLDecodedString];
30     NSData *responseData = [fragment dataUsingEncoding:NSUTF8StringEncoding];
31     
32     if ( [scheme isEqualToString:@"gap"] ) {
33         if ([actionType isEqualToString:@"XXXClass.XXXmethod"]) {
34             
35             NSError* error;
36             NSDictionary* json = [NSJSONSerialization
37                                   JSONObjectWithData:responseData
38                                   options:NSJSONReadingAllowFragments
39                                   error:&error];
40             
41             NSLog(@"title: %@ , message: %@",[json objectForKey:@"title"], [json objectForKey:@"message"] );
42             
43         }
44     }
45     return true;
46 }

 

 

注:这里用到了一个NSString的分类用于字符串雨URL的编码与解码

 1 - (NSString *)URLEncodedString
 2 {
 3     NSString *result = (NSString *)
 4     CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
 5                                             (CFStringRef)self,
 6                                             NULL,
 7                                             CFSTR("!*‘();:@&amp;=+$,/?%#[] "),
 8                                             kCFStringEncodingUTF8));
 9     return result;
10 }
11 
12 - (NSString*)URLDecodedString
13 {
14     NSString *result = (NSString *)
15     CFBridgingRelease(CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault,
16                                                             (CFStringRef)self,
17                                                             CFSTR(""),
18                                                             kCFStringEncodingUTF8));
19     return result;
20 }

 

 

 

iOS开发——网络编程OC篇&使用WebView构建HyBird应用

标签:

原文地址:http://www.cnblogs.com/iCocos/p/4555756.html

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