通过Apache Cordova将Web应用移动化

来源:互联网 发布:怎么搜索淘宝店铺号 编辑:程序博客网 时间:2024/04/30 03:09

今天做个更加有趣的小练习,现在我再通过Apache Cordova来将其封装为iOS上的移动应用。

我们可以看到基本上不需要做任何修改就可以迁移到iOS平台上了。

Cordova就是以前的PhoneGap。

主页地址:https://cordova.apache.org/


1.安装Cordova

我这里安装的cordova 3.0.6


2.创建一个项目Apache Cordova 项目,名称叫employee

cordova -d create ~/Documents/CordovaProjects/employee com.sample employee


增加对iOS的支持:

cordova -d platform add ios


3.下载sapui5的包,解压缩后将resource文件夹拷贝到employee/www下


4.修改一下index.html

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <!DOCTYPE html>  
  2. <!--  
  3.     Licensed to the Apache Software Foundation (ASF) under one  
  4.     or more contributor license agreements.  See the NOTICE file  
  5.     distributed with this work for additional information  
  6.     regarding copyright ownership.  The ASF licenses this file  
  7.     to you under the Apache License, Version 2.0 (the  
  8.     "License"); you may not use this file except in compliance  
  9.     with the License.  You may obtain a copy of the License at  
  10.   
  11.     http://www.apache.org/licenses/LICENSE-2.0  
  12.   
  13.     Unless required by applicable law or agreed to in writing,  
  14.     software distributed under the License is distributed on an  
  15.     "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY  
  16.      KIND, either express or implied.  See the License for the  
  17.     specific language governing permissions and limitations  
  18.     under the License.  
  19. -->  
  20. <html>  
  21.     <head>  
  22.         <meta charset="utf-8" />  
  23.   
  24.         <script src="resources/sap-ui-core.js" id="sap-ui-bootstrap"  
  25.             data-sap-ui-libs="sap.ui.commons, sap.ui.table"  
  26.             data-sap-ui-theme="sap_bluecrystal">  
  27.               
  28.             </script>  
  29.           
  30.         <script>  
  31.             // create the DataTable control  
  32.             var oTable = new sap.ui.table.Table({  
  33.                                                 editable : true  
  34.                                                 });  
  35.               
  36.             // define the Table columns  
  37.             var oControl = new sap.ui.commons.TextView({  
  38.                                                        text : "{Id}"  
  39.                                                        }); // short binding notation  
  40.             oTable.addColumn(new sap.ui.table.Column({  
  41.                                                      label : new sap.ui.commons.Label({  
  42.                                                                                       text : "ID"  
  43.                                                                                       }),  
  44.                                                      template : oControl,  
  45.                                                      sortProperty : "id",  
  46.                                                      filterProperty : "id",  
  47.                                                      width : "100px"  
  48.                                                      }));  
  49.               
  50.             // define the Table columns  
  51.             var oControl = new sap.ui.commons.TextView({  
  52.                                                        text : "{FirstName}"  
  53.                                                        }); // short binding notation  
  54.             oTable.addColumn(new sap.ui.table.Column({  
  55.                                                      label : new sap.ui.commons.Label({  
  56.                                                                                       text : "First Name"  
  57.                                                                                       }),  
  58.                                                      template : oControl,  
  59.                                                      sortProperty : "firstName",  
  60.                                                      filterProperty : "firstName",  
  61.                                                      width : "100px"  
  62.                                                      }));  
  63.               
  64.             // define the Table columns  
  65.             var oControl = new sap.ui.commons.TextView({  
  66.                                                        text : "{LastName}"  
  67.                                                        }); // short binding notation  
  68.             oTable.addColumn(new sap.ui.table.Column({  
  69.                                                      label : new sap.ui.commons.Label({  
  70.                                                                                       text : "Last Name"  
  71.                                                                                       }),  
  72.                                                      template : oControl,  
  73.                                                      sortProperty : "lastName",  
  74.                                                      filterProperty : "lastName",  
  75.                                                      width : "100px"  
  76.                                                      }));  
  77.               
  78.             var oModel = new sap.ui.model.odata.ODataModel(  
  79.                                                            "http://localhost:8080/jpa2/Employee.svc/");  
  80.               
  81.             //var oModel = new sap.ui.model.odata.ODataModel(serviceUrl);  
  82.               
  83.             oTable.setModel(oModel);  
  84.             oTable.bindRows("/Employees");  
  85.               
  86.             // finally place the Table into the UI  
  87.             oTable.placeAt("content");  
  88.             </script>  
  89.           
  90.         <script type="text/javascript" src="cordova.js"></script>  
  91.           
  92.         <title>Hello World</title>  
  93.     </head>  
  94.       
  95.     <body class="sapUiBody" role="application">  
  96.         <div id="content"></div>  
  97.     </body>  
  98.   
  99. </html>  


5.拷贝文件到项目中去:

cordova -d prepare ios


6.用XCode打开employee项目,在iOS模拟器上运行:


小结:这就是使用Html5来开发的好处了,我们可以非常方便地通过Cordova将web应用迁移到各个不同的手机平台上。

0 0