Phonegap中不同平台的不同基础

时间:2014-09-10 10:36:12

标签: android ios cordova

我正在为Phonegap框架上的Android和iOS平台开发移动应用程序。我正在使用一些在Android和iOS中具有不同行为的函数。因此,为此,我应该保留两组不同的代码,或者还有其他替代方法。

哪种框架更适合此类任务。我目前正在使用Sencha touch。

1 个答案:

答案 0 :(得分:0)

device插件和if / switch声明是您的朋友。

$ cordova plugin add org.apache.cordova.device

然后(收到deviceready后):

var currentPlatform = "unknown";
if (typeof device !== "undefined") {
  currentPlatform = device.platform.toLowerCase();
}
switch (currentPlatform) {
    case "android":
      // do android stuff
      break;
    case "ios":
      // do iOS stuff
      break;
    .
    . // repeat for platforms you need //
    .
    default:
      // default is technically optional here, but might be useful if you have some
      // fallback that could work on other platforms. Good for throwing an error
      // if running on an unsupported platform for some odd reason or for supporting
      // mobile + desktop during development (could have mock data here or mock
      // operations, etc.)
}