最近开发了个ionic 的app,看了下源代码,加上一些文档,虽然没能调试出来整个过程,但是也大概知道了下,比如appversion这个navtive ,我们在使用的时候获取版本号,getVersionNumber(),

运行过程是首先 调用ionic native中appversion的getVersionNumber(),然后再去调用cordova-plugin-app-version/www/AppVersionPlugin.js中的getVersionNumber

Skip to content
Features
Business
Explore
Marketplace
Pricing

Search

Sign in or Sign up
48 321 178 whiteoctober/cordova-plugin-app-version
 Code  Issues 2  Pull requests 4  Projects 0  Insights
Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.

cordova-plugin-app-version/www/AppVersionPlugin.js
863d55b  on 3 Jun 2016
 Yannick Gagnon Add support for WhenJS
@rjmunro @matiassingers @uranus86 @pke @oliversalzburg @gprasanth
     
66 lines (58 sloc)  2.12 KB
/*jslint indent: 2 */
/*global window, jQuery, angular, cordova */
"use strict";

// Returns a jQuery or AngularJS deferred object, or pass a success and fail callbacks if you don't want to use jQuery or AngularJS
var getPromisedCordovaExec = function (command, success, fail) {
  var toReturn, deferred, injector, $q;
  if (success === undefined) {
    if (window.jQuery) {
      deferred = jQuery.Deferred();
      success = deferred.resolve;
      fail = deferred.reject;
      toReturn = deferred;
    } else if (window.angular) {
      injector = angular.injector(["ng"]);
      $q = injector.get("$q");
      deferred = $q.defer();
      success = deferred.resolve;
      fail = deferred.reject;
      toReturn = deferred.promise;
    } else if (window.when && window.when.promise) {
      deferred = when.defer();
      success = deferred.resolve;
      fail = deferred.reject;
      toReturn = deferred.promise;
    } else if (window.Promise) {
      toReturn = new Promise(function(c, e) {
        success = c;
        fail = e;
      });
    } else if (window.WinJS && window.WinJS.Promise) {
      toReturn = new WinJS.Promise(function(c, e) {
        success = c;
        fail = e;
      });
    } else {
      return console.error('AppVersion either needs a success callback, or jQuery/AngularJS/Promise/WinJS.Promise defined for using promises');
    }
  }
  // 5th param is NOT optional. must be at least empty array
  cordova.exec(success, fail, "AppVersion", command, []);
  return toReturn;
};

var getAppVersion = function (success, fail) {
  return getPromisedCordovaExec('getVersionNumber', success, fail);
};

getAppVersion.getAppName = function (success, fail) {
  return getPromisedCordovaExec('getAppName', success, fail);
};

getAppVersion.getPackageName = function (success, fail) {
  return getPromisedCordovaExec('getPackageName', success, fail);
};

getAppVersion.getVersionNumber = function (success, fail) {
  return getPromisedCordovaExec('getVersionNumber', success, fail);
};

getAppVersion.getVersionCode = function (success, fail) {
  return getPromisedCordovaExec('getVersionCode', success, fail);
};

module.exports = getAppVersion;

然后再去执行cordova.exec(success, fail, "AppVersion", command, []);这个方法,这个方法通过cordova的操作,分析不同的设备调用不同的java类中的execute(),(这里以android为例)

package uk.co.whiteoctober.cordova;

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.PackageManager;

public class AppVersion extends CordovaPlugin {
  @Override
  public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

    try {
      if (action.equals("getAppName")) {
        PackageManager packageManager = this.cordova.getActivity().getPackageManager();
        ApplicationInfo app = packageManager.getApplicationInfo(this.cordova.getActivity().getPackageName(), 0);
        callbackContext.success((String)packageManager.getApplicationLabel(app));
        return true;
      }
      if (action.equals("getPackageName")) {
        callbackContext.success(this.cordova.getActivity().getPackageName());
        return true;
      }
      if (action.equals("getVersionNumber")) {
        PackageManager packageManager = this.cordova.getActivity().getPackageManager();
        callbackContext.success(packageManager.getPackageInfo(this.cordova.getActivity().getPackageName(), 0).versionName);
      return true;
      }
      if (action.equals("getVersionCode")) {
        PackageManager packageManager = this.cordova.getActivity().getPackageManager();
        callbackContext.success(packageManager.getPackageInfo(this.cordova.getActivity().getPackageName(), 0).versionCode);
      return true;
      }
      return false;
    } catch (NameNotFoundException e) {
      callbackContext.success("N/A");
      return true;
    }
  }

}

function(winParam) {}:成功回调函数。假设您的 exec成功完成,此功能将随您传递给它的任何参数一起执行。

function(error) {}:错误回调函数。如果操作未成功完成,则此功能将执行可选的错误参数。

"service":在本机端呼叫的服务名称。上面图中的java代码中未AppVersion.java,那么此处对应的就是AppVersion。

"action":在本机端调用的动作名称。原生代码通过对action进行判断,从而知道JS让原生端执行什么样的功能。

[/* arguments */]:传到原生环境的参数数组。

ionic 运行原理

相关文章:

  • 2021-11-23
  • 2021-11-28
  • 2021-11-28
  • 2021-11-28
  • 2021-12-22
  • 2021-05-29
  • 2021-06-12
  • 2021-05-11
猜你喜欢
  • 2021-09-08
  • 2021-07-02
  • 2022-12-23
  • 2022-12-23
  • 2021-06-07
  • 2021-12-05
  • 2021-11-18
相关资源
相似解决方案