Beginning with Apache Cordova

前端 未结 3 1518
南旧
南旧 2021-02-09 10:55

I have just donwloaded Apache Cordova and it seems like there are platform specific versions. Do I have to code for a specific platform before porting it to another? Is it possi

相关标签:
3条回答
  • 2021-02-09 11:21

    As long as you don't need the API, you can code everything in your browser. However, when you want to test the Cordova API features, you must run on a platform.

    The platform specific files (e.g. the eclipse project for android) contain a www folder (assets/www for android) where you have to put all of your code. Within that folder, everything should be cross-platform, with one exception: the cordova.js file that contains the bridge to the native code for each platform.

    You can also try Ripple to run a project directly on your browser, which has the advantage of better debugging tools. It is far from being feature comple though.

    0 讨论(0)
  • 2021-02-09 11:31

    I think that there is a slight gap in your understanding of Cordova. Cordova is used to build hybrid mobile applications. Hybrid means that your application is basically a standard web site built with HTML/CSS/JavasScript, but it has access to native device functionality. Usually when you are building a regular website with JavaScript, you cannot do certain things without working directly in the native code, like taking a picture or going through the Contacts on the phone. However, Cordova allows you to access these native functions just from the JavaScript! And you never have to touch the native code! So you will build one application in HTML/JavaScript/CSS (one uniform codebase!), and after you go through the build process, will have multiple application files, one for each native platform that you "built" it for.

    Yes, one major benefit of using Cordova is that you can easily create multiplatform apps. What you do is first create your application in HTML/JavaScript - when you need to use a native API, you can just call the appropriate Cordova JavaScript API - this API is common across all of the platforms, meaning that calling say cordova.someAPI.doCoolDeviceOnlyThingThatBrowsersCantDoYet(parameter) will expect the same parameters no matter what device the user is using. Your built Cordova app will then call the equivalent native functionality, no matter which OS the app was built for.

    In order to create an app for, say, iOS and Android, you will need to set up the appropriate build tools for both platforms. Then what you will do is import your HTML/Javascript files that make up your application into these tools and "build" the native app, which will wrap your HTML/JavaScript in native code, add the device specific Cordova code (usually in the form of something like cordova.jar on Android) and create the downloadable package that you can then publish to app stores. This process of "compiling" your app to the multiple platforms can be a pain, but thankfully there is an automated service that can help, so check out PhoneGap build while it's still free.

    You should be able to get started building apps with just your favorite HTML editor, the Ripple emulator, and the Cordova API reference guides. However, you will probably want a real device to test in and build your app with, so you probably will have to set up at least one native environment (like Eclipse with the Android ADT and the appropriate cordova.js file.) (The cordova.js file is very similar between platforms, except for when there are differences between platforms, like in the bridge that handles communication between JavaScript and the native code.)

    Hopefully I have answered all of your questions - good luck!

    0 讨论(0)
  • 2021-02-09 11:34

    Answer by @MBillau perfectly describes the concept, from source code perspective there are some specific CLI-related commands:

    • cordova create com.domain.projectname - create a project.

    • cordova platform add ios or cordova platform add android - add target platform, after creating a project remember to cd com.domain.projectname, otherwise you might see something like that Current working directory is not a Cordova-based project. in your terminal.

    • cordova plugin add pluginname - add specific plugin to the project, which you will need quite often to do. In your terminal you should have something like Installing "pluginname" for ios and Adding pluginname to package.json.

    • Finally cordova build or cordova build ios or cordova build android to build this and cordova emulate android or cordova run android to run it on your device or emulate on Android. For iOS you'll need Xcode.

    More information you can find on https://cordova.apache.org/docs/en/latest/

    0 讨论(0)
提交回复
热议问题