Loading...

Create Quick Tip Using and Installing Cordova

View: 683    Dowload: 0   Comment: 0   Post by: hanhga   Category: Android   Fields: Other

First install Node.js from their official packages,Homebrew (Mac), Chocolatey or Scoop (Windows). Cordova uses the package manager of Node.js, npm, for installation.

On OSX and Linux open your terminal and run:

sudo npm install -g cordova

On Windows open Command Prompt run:

C:\>npm install -g cordova

This installs the Cordova package globally and accessible in any project.

Create a Project

Create a new project with cordova create <name> e.g.

cordova create my-mobile-app
cd my-mobile-app

Add a Platform

Type cordova platform to see a list of available platforms. You’ll see different options depending on the operating system, here’s the results on a Mac.

Cordova Platform Options

Add a platform with cordova platform add <platform>, e.g.

cordova platform add ios
cordova platform add android

To build and run you’ll need SDKs for each platform that you want to target installed. We have a handy quick tip for installing Android and for iOS you will need XCode.

To check you have everything needed for the platforms added, run:

cordova requirements

Build

Build your app with cordova build <platform>, e.g.

cordova build ios
cordova build android

You can also run cordova build to build all platforms added to the project.

Run

Run the app with cordova run <platform>, e.g.

cordova run ios
cordova run android

 

Note: On a Mac cordova run ios will open the iOS Simulator. A good option for emulating android devices is with GenyMotion that lets you install and run popular android devices as a Virtual Machine (VM). Once the VM is running, cordova run android will install and run the app.

Plugins

Plugins are additions to Cordova that allow you to access hardware features from JavaScript.

To Install a plugin run cordova plugin add <plugin>. For example, to install the camera plugin.

cordova plugin add cordova-plugin-camera

Adding Your Application

The custom code for your app sits in the www directory. You can edit those files, build and run to see the changes reflected in an emulator. As an example, add a button which will take a photo from the library and show it in the view.

Replace the #deviceready div in index.html with:

<div id="deviceready">
  <p>
    <button id="get-picture">Take a photo</button>
  </p>
  <p>
    <img id="my-photo" width="300" />
  </p>
</div>

Add the following to the end of js/index.js:

document.getElementById('get-picture').addEventListener('click', getPicture, false);

function getPicture() {
  navigator.camera.getPicture(onSuccess, onFail, {
    quality: 50,
    sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
    destinationType: Camera.DestinationType.FILE_URI
  });
}

function onSuccess(imageURI) {
  var image = document.getElementById('my-photo');
  image.src = imageURI;
}

function onFail(message) {
  alert('Failed because: ' + message);
}

Build then Run and you should be able to test out your first ‘native’ feature.

Camera Example

You’re on your way to building great apps :)

Next Steps

Read the Cordova Overview for an understanding of how Cordova works and search the Plugins to see what’s available on different platforms.

 

Note: On a Mac cordova run ios will open the iOS Simulator. A good option for emulating android devices is with GenyMotion that lets you install and run popular android devices as a Virtual Machine (VM). Once the VM is running, cordova run android will install and run the app.

Plugins

Plugins are additions to Cordova that allow you to access hardware features from JavaScript.

To Install a plugin run cordova plugin add <plugin>. For example, to install the camera plugin.

Create Quick Tip Using and Installing Cordova

First install Node.js from their official packages,Homebrew (Mac), Chocolatey or Scoop (Windows). Cordova uses the package manager of Node.js, npm, for installation.

Posted on 06-07-2016 

Comment:

To comment you must be logged in members.

Files with category

 
File suggestion for you
Loading...
File top downloads
Loading...
Loading...
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd