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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11550705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:56:00  来源:igfitidea点击:

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

iosxcodecordovaxcode4phonegap-plugins

提问by andrewpthorp

When you are setting up a PhoneGap project, you see the following:

当您设置 PhoneGap 项目时,您会看到以下内容:

BUILD

建造

How can I get that information inside of the iOS application? Is there a way to do it with 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. I just haven't been able to find any answers.

如何在 iOS 应用程序中获取该信息?有没有办法用phonegap来做到这一点?插件呢?如果没有插件存在,并且有一种方法可以在 iOS 应用程序中做到这一点,则可以编写一个插件。我只是找不到任何答案。

Thanks!

谢谢!

回答by CWSpear

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:

所以这是我使用代码并遵循亚当指向的指南所经历的:

In my project plugin folder at <project name>/Plugins/, I created MyCDVPlugin.mand 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). Our function, as a getter, doesn't actually have any parameters in one sense, but those are still required. (optionsmight actually be optional? I didn't test.)

这些参数是标准的 Cordova 插件参数(据我所知)。我们的函数,作为一个 getter,在某种意义上实际上并没有任何参数,但这些仍然是必需的。(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. 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:

为了完整起见,Xcode 不会自动更新文件(这实际上是有道理的),但我总是忘记。仅仅因为您的文件在您的项目目录中,并不意味着它们在您的项目中。不要忘记将它们拖到项目中的 Plugins 目录中:

Dragging files into the project.

将文件拖入项目。

Also, make sure you add your plugin to your Cordova.plistPluginsentry:

另外,请确保将插件添加到Cordova.plistPlugins条目中:

Editing Cordova.plist

编辑 Cordova.plist

From there, it's pretty simple to call the method from JavaScript (make sure to use it after devicereadyis 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 方面有点不知所措。

回答by daxiang28

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:

将它们放入字典中:

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 回调函数中将它作为两个参数返回,否则这可能是最直接的。

Steve

史蒂夫

回答by Frederic Fillon

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.

我只是测试它,就像一个魅力。

回答by Prem Kumar Maurya

If you are using jquery then we can get the app version from this function cordova.getAppVersion.getVersionNumberof cordova-plugin-app-versioncordova plugin.

如果你正在使用jQuery的时候,我们就能从该功能的应用程序版本cordova.getAppVersion.getVersionNumber科尔多瓦-插件,应用程序版本科尔多瓦插件。

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 $timeoutthen 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);
});