Ionic App Upgrade Plugin development

Source: Internet
Author: User

Finally came to the writing plugin of this place, the process of plug-ins:

1. Install the Plugman plugin to manage our programs

NPM install-g Plugman

2. Create the plug-in project APPUPGRADE,CD to your target directory and execute the following command:

Plugman Create--name appupgrade--plugin_id  com.caseStudy.plugin  0.0.  1

Where Com.caseStudy.plugin is the package name of the Java class and will be referenced later.

After execution, the directory will see the new Appupgrade directory, its downstream plug.xml files, src and www directories

3. Add Android Platform: first CD to Appupgrade directory, then execute the following command

Plugman Platform Add--platform_name Android

A Appupgrade.java file is automatically created in the SRC directory

4. Modify our Appupgrade.js file

Cordova.define ("Cordova/plugins/appupgrade",function(Require, exports, module) {//1. Load the required components    varexec = require ("Cordova/exec"); //2. Defining the JS Class object    varAppupgrade =function() {}; //3. Add JS class object method, URL for API version check addressAppUpgrade.prototype.downLoad =function(Apiurl,successcallback, errorcallback) {if(typeofErrorcallback! = "function") {Console.log ("Error"); return        }        if(typeofSuccesscallback! = "function") {Console.log ("Error"); return        }        //4. Call the download method of the Appupgrade.java class with a parameter of ApiurlEXEC (Successcallback, errorcallback, ' Appupgrade ', ' downLoad ', [{url:apiurl}]);    }; //5. Set the plugin to exports    varAppupgrade =NewAppupgrade (); Module.exports=Appupgrade;});if(!window.plugins) {Window.plugins= {};}if(!Window.plugins.appUpgrade) {Window.plugins.appUpgrade= Cordova.require ("Cordova/plugins/appupgrade");}

5.appupgrade.java class function Rewrite, here using the Open source code Com.shelwee.update.UpdateHelper package, the previous article has mentioned how to add to our project .... There is no need to add, the package name can be introduced

 PackageCom.caseStudy.plugin;ImportOrg.apache.cordova.CordovaPlugin;ImportOrg.apache.cordova.CallbackContext;ImportOrg.json.JSONArray;Importorg.json.JSONException;ImportOrg.json.JSONObject;//Add new import JarImportCom.shelwee.update.UpdateHelper;Importandroid.app.Activity;ImportAndroid.content.Context; Public classAppupgradeextendscordovaplugin{@Override Public BooleanExecute (String action, Jsonarray args, Callbackcontext callbackcontext)throwsjsonexception {if(Action.equals ("DownLoad"))        {            FinalJsonobject options = args.getjsonobject (0); FinalString url = options.getstring ("url");  This. DownLoad (URL, callbackcontext); return true; }        Else{callbackcontext.success ("There is no DownLoad method in Appupgrade.java class"); return false; }    }    Private voiddownLoad (String URL, callbackcontext callbackcontext) {callbackcontext.success ("Invoke DownLoad method Success,start to check ..."); Updatehelper Updatehelper=NewUpdatehelper.builder (Cordova.getactivity ()). Checkurl (URL). Isautoinstall (true). build ();    Updatehelper.check (); }}

To the completion of this plug-in development.

6. To deploy the plugin to APP,CD to our own Ionic app project directory, execute the following command:

Cordova Plugin Add your file path/appupgrade

This will load into our project and we can see our plugin name in the plugin directory Appupgrade

7.app program calls the plug-in method, do not need to indicate any dependencies, the following code can be placed in the button event,

Window.plugins.appUpgrade.downLoad (appcheckversionurl,function (data) {Console.log (data);  },function (Error) { Console.log (Error); alert (error); });

Plugin has been deployed to complete, it seems that in the browser can not see the effect of plug-ins, need to pack into APK installed on the phone

8. Execute command: Ionic build Android

If the program is correct, the successful generation of the APK, the error, you need to check our Java code, remove the plugin re-join, and so on, this oneself met several times, the code to tidy up successfully.

9. The commands to remove plugins are:

Cordova Plugin Add Path/to/your/plugin/dir

After the exploration of the plug-in, to ionic understand and deepen one step, you can analyze the code of other plug-ins!!!

Ionic App Upgrade Plugin development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.