集成 Cordova 或 Phonegap

Cordova

Apache Cordova provides APIs and packaging tools that create applications for Android, iOS, BlackBerry, and Windows Phone devices. Sencha Cmd 5+ provides a build system that supports multiple build profiles. This is ideal for native packaging. The Cordova Native Packager components have been updated to take full advantage of this feature.

PhoneGap

PhoneGap is built on top of Cordova and both have access to the Cordova API. PhoneGap and Cordova differ by how their packaging tools are implemented. PhoneGap provides a remote building interface at Adobe PhoneGap Build that lets you package and emulate an application for a single platform in the cloud. The PhoneGap Native Packager components have been updated to take full advantage of this feature.

Picking a Packager

When choosing between Cordova and PhoneGap, it’s important to note a few things:

  • Cordova packaging tools let you build simultaneously for multiple platforms on your machine.
  • Cordova is constantly updated by the open source community, whereas PhoneGap updates are coordinated by Adobe.
  • PhoneGap provides a remote building application that eliminates the need for local building.

Using Sencha Cmd with Cordova and PhoneGap

Every app generated with Sencha Cmd has the ability to switch on native building through these services. Sencha Cmd will take care of all the repetitive tasks, such as building your app, getting it in the proper place for Cordova or PhoneGap, and running the proper commands to build, emulate, or run your application.

For more information on getting started with native development, please see the Apache Cordova Platform Guides. These guides provide getting started information and prerequisites needed to get your system up and running.

Important Notes

  1. packager.json has been fully removed. If your project still contains this file, you may safely remove it. This file was only used by Sencha’s legacy Native Packager.

  2. For Cordova and PhoneGap, Sencha Cmd can only create DEBUG versions of applications, not a Release (for an app store) version. To create release versions, you must use a packager, such as Eclipse or IntelliJ for Android, and Xcode for iOS.

  3. You must have Java JRE 1.7 for the Sencha Cmd Cordova commands to work properly. While Sencha Cmd 6 on Windows and Mac OS X ship a current JRE in their default installers, the Cordova tools will not find and utilize Sencha Cmd’s internal JRE.

Environment Setup

Before developing a Cordova or PhoneGap application, you will need to get your environment set up with the following software:

  1. Install Java JRE

  2. Install Node.js

  3. If you want to package and emulate with Cordova, install it with the following command:

    npm install -g cordova

    If you’re on a Mac, you may need to include “sudo” in order to complete the installation.
    The command would then be:

    sudo npm install -g cordova

    Note: Whether or not you install Cordova to package and emulate your app, the Cordova API is available to your application. Cordova packaging and emulation does not affect the use of the Cordova APIs in a Cordova or PhoneGap app.

  4. If you want to package and emulate with PhoneGap, install it with the following command:

    npm install -g phonegap

    If you’re on a Mac, you may need to include “sudo” in order to complete the installation.
    The command would then be:

    sudo npm install -g phonegap

    Note: You must register with the free Adobe PhoneGap Build site and obtain a username and password to access the PhoneGap remote building site.

  5. Download and install Sencha Cmd.
    For more information about this process, please check out the Cmd Intro Guide.

  6. Check and satisfy target device platform-specific requirements:

    Android: Download and install the Android SDK Manager.

    Blackberry: Review the BlackBerry Native SDK and register to sign your apps with the BlackBerry Keys Order Form.

    iOS: Complete all of the steps for iOS provisioning on the Apple iOS provisioning profiles portal (requires an Apple ID, password, and a purchased developer license). Use this site to obtain a certificate, identify devices, and get an AppID.

    In addition, download and install the free Xcode software. You can use the Xcode simulator to debug your iOS app before installing it on a device. Xcode only works on a Mac with Lion, Mountain Lion, or Mavericks OS X.

config.xml

When you create a PhoneGap or Cordova app, they will both will create a config.xml file for your project.

Cordova stores config.xml in your “app_root/cordova” folder, while PhoneGap stores the config.xml file in your “app_root/cordova/www” folder. This is a problem because the www folder is the destination for the compiled build. It is very likely that this www folder will get deleted and wipe out the config.xml file.

To remedy this in Sencha Cmd, we copy the config.xml file to the root of the PhoneGap project when a PhoneGap application is created. Every time you do a build, we then copy it back into the www folder along with the compiled application. This means that users can expect the config.xml file to be in the root of the Cordova or PhoneGap folder regardless of which framework you use.

For more information about configuring PhoneGap and Cordova, please review the following resources:

Developing a Cordova App

Create your application using the Cmd generate command as follows:

sencha -sdk /path/to/Framework generate app MyApp /path/to/MyApp

When developing an Ext 6+ universal application the generated application will already contain a builds block in app.json. When your application already contains a builds block the sencha phonegap/cordova init command will not be able to add modify your app.json. To add Cordova support please modify your applications app.json builds block as follows:

"builds": {
    "classic": {
        "toolkit": "classic",
        "theme": "theme-triton"
    },

    "modern": {
        "toolkit": "modern",
        "theme": "theme-cupertino",
        "packager": "cordova",
        "cordova": {
            "config": {
               "platforms": "ios",
               "id": "com.mydomain.MyApp"
            }
        }
    }
}

Navigate to your newly generated project folder and run one of the following commands (APP_ID and APP_NAME arguments optional).

sencha phonegap init com.mycompany.MyApp MyApp

or

sencha cordova init com.mycompany.MyApp MyApp

You should now see a phonegap or cordova directory in the project folder after the above command completes.

You should also see a block for PhoneGap or Cordova in your project’s app.json file. app.json is located at the root of your project. This is where you can set the platform for which you are building.

A build object similar to the following should now be present in your app.json file.

"builds": {
    "native": {
        "packager": "cordova",
        "cordova" : {
            "config": {
                "platforms": "ios"
                "id": "com.mydomain.MyApp"
            }
        }
    }
}

Let’s talk a bit about the above code snippet.

Build Name

It is worth noting that the word “native” is simply your build’s name and can be anything you like. Build names must be a single string, containing only alphanumeric characters with no spaces. For example you could have a build name like “ios”, “android”, “iphone”, “ipad”, etc..

Note: While you may choose anything for a build name, the names “production”, “testing” and “development” are reserved by Sencha Cmd and should not be used in this context.

Platforms

You can then set the platforms object to be any platform, or combination of platforms. For Cordova, you can specify a space delimited list, for example “ios android”.

ID

The “id” property will be used when the Cordova application is first generated. This will be the identifier for your application. That said, you will want to make sure you have picked this correctly. If you need to change it, you will want to delete the Cordova folder from your project and let Cmd regenerate it after you have changed the ID property. This is especially important for iOS application as this must match your Bundle Identifier.

From the terminal or command line run the following command:

sencha app build {build-name}

Where {build-name} is one of the names defined in the build object of your app.json file. For example if you called your build “android” the command would be:

sencha app build android

That’s it! Sencha Cmd will now create a Cordova application and build the platform you specified via the platforms property in app.json.

Sencha Cordova Commands

There are 4 commands you are able to use with Cordova and this new build object.

Build

sencha app build {build-name}

“build” will build your Sencha application and then build a native application.

Run

sencha app run {build-name}

“run” will build your Sencha application and your native application. It will then attempt to run it on a connected device.

Emulate

sencha app emulate {build-name}

“emulate” will build your Sencha application and your native application. It will then attempt to run it in a emulator.

Prepare

sencha app prepare {build-name}

“prepare” will build your Sencha application, then prepare the built application into the native app. However, it will not build the native portion. This is good if you need to inject things into your application after sencha compile and before the native build.

Developing a PhoneGap App

PhoneGap is very similar to Cordova. In fact, the process is almost identical if you are not building via the PhoneGap cloud services.

Here is a snippet for app.json that would give you a PhoneGap project identical to the Cordova project above.

"builds": {
    "native": {
        "packager": "phonegap",
        "phonegap" : {
            "config": {
                "platform": "ios",
                "id": "com.mydomain.MyApp"
            }
        }
    }
}

There are only a few minor, but important, differences.

  • The name changes from “cordova” to “phonegap”.

  • “platform” is singular not “platforms”. PhoneGap only allows you to build one local platform at a time, whereas Cordova allows for multiple simultaneously.

Once you recognize these small differences, it’s the same commands as above. You can simply run sencha app build native or sencha app run native and you will be on the same path as Cordova.

Sencha PhoneGap Commands

There are 3 commands you may use with PhoneGap and this new build object. PhoneGap does not support the prepare command found in Cordova. sencha app prepare native will throw an error if you are using PhoneGap. The other three are the same as the Cordova commands above.

Build

sencha app build {build-name}

“build” will build your sencha application and then build a native application.

Run

sencha app run {build-name}

“run” will build your sencha application and your native application. It will then attempt to run it on a connected device.

Emulate

sencha app emulate {build-name}

“emulate” will build your sencha application and your native application. It will then attempt to run it in a emulator.

Developing a Remote PhoneGap App

Building your PhoneGap application in the cloud has numerous advantages. You do not need to go through the normal hassle of downloading all the SDKs and tools for your machine. You can simply send your web app to the PhoneGap servers and Adobe will generate a native application for you.

Before you get started with Sencha Cmd and remote building, you will want to visit http://build.phonegap.com and sign up for a free account.

You can test the build process by simply uploading a zip that contains a index.html file. PhoneGap Remote Build will then package it for you.

If you are building for iOS, you will need to follow the instructions on the site to add the proper credentials to build your application.

Once you feel confident that your account is setup and you have all the correct files uploaded to Adobe, you can quickly add remote building to your Sencha workflow.

Let’s take a look at a build object for remote deployment.

"builds": {
    "native": {
        "packager": "phonegap",
        "phonegap" : {
            "config": {
                "platform": "ios",
                "remote": true,
                "id": "com.mydomain.MyApp"
            }
        }
    }
}

As you can see, it is very simple to switch from local to remote building. All you need to do is add the remote flag to your app.json builds block.

The next step is to provide your username and password so that Sencha Cmd can automatically log you in to the PhoneGap remote servers and upload your project.

To do this, we recommend adding a local.properties file to your application’s root. For security purposes, this properties file should be ignored by all forms of version control.

Add the following lines to that file:

phonegap.remote.username=myname@domain.com
phonegap.remote.password=s3nch@isgr3@t

Once you have configured your username and password, sending off a build is simple. Just run sencha app build native and your application will be compiled and shipped up to the cloud for Adobe to generate your native application.

After the build is complete, you may access the final package file through the PhoneGap portal for testing on devices, or for uploading to applicable app stores.

Last updated