简体   繁体   English

如何获取应用程序版本并构建iOS PhoneGap应用程序?

[英]How to get the application version and build in an iOS PhoneGap Application?

When you are setting up a PhoneGap project, you see the following: 在设置PhoneGap项目时,您会看到以下内容:

建立

How can I get that information inside of the iOS application? 如何在iOS应用程序中获取该信息? Is there a way to do it with phonegap? 有没有办法用phonegap做到这一点? What about a plugin? 插件怎么样? If no plugin exists, and there is a way to do it in an iOS application, a plugin can be written. 如果没有插件,并且有一种方法可以在iOS应用程序中执行,则可以编写插件。 I just haven't been able to find any answers. 我只是找不到任何答案。

Thanks! 谢谢!

I wanted to offer my solution (based off of Adam Ware's solution). 我想提供我的解决方案(基于Adam Ware的解决方案)。

Normally I don't like just giving all the code for people to copy and paste, but I feel like this is a bit of an exception as a lot of people diving into PhoneGap know nothing about Objective-C and its funny-looking syntax (like me). 通常情况下,我不喜欢只给人们复制和粘贴的所有代码,但我觉得这有点例外,因为许多潜入PhoneGap的人对Objective-C及其看起来很滑稽的语法一无所知(像我这样的)。

So here's what I went through using the code and following the guide Adam pointed to: 所以这就是我使用代码并遵循Adam指出的指南:

In my project plugin folder at <project name>/Plugins/ , I created MyCDVPlugin.m and MyCDVPlugin.h . <project name>/Plugins/项目插件文件夹中,我创建了MyCDVPlugin.mMyCDVPlugin.h

I've written in C before, so I understand headers, but for those of you that don't, it's basically telling the complier what to look for, so we just tell it the name of our method and import Cordova's header: 我之前用C语言写过,所以我理解标题,但是对于那些没有标题的人来说,它基本上是告诉编码器要查找的内容,所以我们只告诉它我们方法的名称并导入Cordova的标题:

#import <Cordova/CDVPlugin.h>

@interface MyCDVPlugin : CDVPlugin

- (void)getVersionNumber:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;

@end

Those parameters are the standard Cordova plugin parameters (as far as I know). 这些参数是标准的Cordova插件参数(据我所知)。 Our function, as a getter, doesn't actually have any parameters in one sense, but those are still required. 作为一个吸气剂,我们的功能在某种意义上实际上没有任何参数,但仍然需要这些参数。 ( options might actually be optional? I didn't test.) options可能实际上是可选的?我没有测试。)

In our .m , all we need is the actual function, our header from before, and CDVPluginResult.h : 在我们的.m ,我们需要的只是实际的函数,我们之前的标题和CDVPluginResult.h

#import "MyCDVPlugin.h"
#import <Cordova/CDVPluginResult.h>

@implementation MyCDVPlugin

- (void)getVersionNumber:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
    NSString* version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    NSString* callbackId = [arguments objectAtIndex:0];

    CDVPluginResult* pluginResult = nil;
    NSString* javaScript = nil;

    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:version];
    javaScript = [pluginResult toSuccessCallbackString:callbackId];

    [self writeJavascript:javaScript];
}

@end

Basically, this gets the version number and passes it back to your success callback. 基本上,这会获取版本号并将其传递回成功回调。 (I didn't think this method really has a fail case, so it doesn't have a fail callback.) (我不认为这种方法确实有失败的情况,所以它没有失败的回调。)

For completeness' sake, Xcode doesn't auto-update files (which actually makes sense), but I always forget. 为了完整起见,Xcode不会自动更新文件(实际上有意义),但我总是忘记。 Just because your files are in your project directory, doesn't mean they're in your project. 仅仅因为您的文件位于项目目录中,并不意味着它们在您的项目中。 Don't forget to drag them into your Plugins directory in your project: 不要忘记将它们拖到项目的Plugins目录中:

将文件拖到项目中。

Also, make sure you add your plugin to your Cordova.plist Plugins entry: 另外,请确保将插件添加到Cordova.plist Plugins条目中:

编辑Cordova.plist

From there, it's pretty simple to call the method from JavaScript (make sure to use it after deviceready is triggered): 从那里,从JavaScript调用该方法非常简单(确保在触发deviceready之后使用它):

// get and show the version number
var gotVersionNumber = function(version) {
    // cache value so we can use it later if we need it
    Hub.Global.Version = version;
    $('.version-number').text(version);
};

// my plugin doesn't even have a failure callback, so we pass null.
// 5th param (parameters to pass into our Obj-C method) is NOT 
// optional. must be at least empty array
cordova.exec(gotVersionNumber, null, "MyCDVPlugin", "getVersionNumber", []);

That's it! 而已! Simple, right...? 简单,对...? Hope this helps someone else a little overwhelmed by the Obj-C side of PhoneGap. 希望这有助于其他人对PhoneGap的Obj-C方面有点不知所措。

As a small tweak to @CWSpear's awesome answer, I also wanted to grab the Build: 作为对@ CWSpear的精彩回答的一个小调整,我也想抓住Build:

Grab the Build and Version: 抓住构建和版本:

NSString* version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
NSString* build = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];

Throw them into a Dict: 把它们扔进Dict:

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:[NSString stringWithString:version] forKey:@"version"];
[dict setObject:[NSString stringWithString:build] forKey:@"build"];

Modify the pluginResult to return the new Dict: 修改pluginResult以返回新的Dict:

pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dict];

Cordova.exec: Cordova.exec:

    cordova.exec(function(response){
        console.log(response.build);
        console.log(response.version);
    }, null, "MyCDVPlugin", "getVersionNumber", []);

I don't know enough about objective C to return it as two args in the JS cordova.exec callback function, otherwise that would probably be the most straight forward. 我不太了解客观C将它作为JS cordova.exec回调函数中的两个args返回,否则这可能是最直接的。

Steve 史蒂夫

A plugin exists now, : http://plugins.cordova.io/#/package/uk.co.whiteoctober.cordova.appversion 现在有一个插件, http//plugins.cordova.io/#/package/uk.co.whiteoctober.cordova.appversion

I just test it, works like a charm. 我只是测试它,就像一个魅力。

If you are using jquery then we can get the app version from this function cordova.getAppVersion.getVersionNumber of cordova-plugin-app-version cordova plugin. 如果您正在使用jquery,那么我们可以从此函数cordova.getAppVersion.getVersionNumber获取 cordova-plugin-app-version cordova插件的应用程序版本

cordova.getAppVersion.getVersionNumber().then(function (version) {
   $('.version').text(version);
});

But if we use angular then we have to put the version setting code inside the $timeout then it will work fine. 但是如果我们使用angular然后我们必须将版本设置代码放在$ timeout内,那么它将正常工作。

angular.module('testApp', [])
.controller('MainCtrl', function($scope, $rootScope, $timeout) {
// Calling cordova plugin cordova-plugin-app-version
    cordova.getAppVersion.getVersionNumber().then(function (version) {
    $timeout(function() {
          $rootScope.app = {};
          $rootScope.app.version = version;
    }, 0);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM