iOS開發——網路編程OC篇&使用WebView構建HyBird應用

來源:互聯網
上載者:User

標籤:

使用WebView構建HyBird應用

HyBird是一種本地技術與Web相結合,能過實現跨平台的行動裝置 App開發,最常用的一個架構: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)didReceiveMemoryWarning12 {13     [super didReceiveMemoryWarning];14     // Dispose of any resources that can be recreated.15 }16 17 #pragma mark UIWebViewDelegate18 19 - (void)webViewDidFinishLoad:(UIWebView *)webView20 {21     [self.webView stringByEvaluatingJavaScriptFromString:@"helloWorld(‘從iOS對象中調用JS Ok.‘)"];22 }23 24 25 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType26 {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 = [NSJSONSerialization37                                   JSONObjectWithData:responseData38                                   options:NSJSONReadingAllowFragments39                                   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*)URLDecodedString13 {14     NSString *result = (NSString *)15     CFBridgingRelease(CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault,16                                                             (CFStringRef)self,17                                                             CFSTR(""),18                                                             kCFStringEncodingUTF8));19     return result;20 }

 

 

 

iOS開發——網路編程OC篇&使用WebView構建HyBird應用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.