如何获得应用程序版本和构建在iOS PhoneGap应用程序? [英] How to get the application version and build in an iOS PhoneGap Application?

查看:182
本文介绍了如何获得应用程序版本和构建在iOS PhoneGap应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





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



谢谢!

解决方案>

我想提供我的解决方案(基于Adam Ware的解决方案)。



通常我不喜欢只是给人的所有代码复制和粘贴,但我觉得这是一个例外,因为很多人潜入PhoneGap知道Objective-C和其有趣的语法(如我)。



下面是我使用代码和指南Adam指向:



在我的项目插件文件夹< project name> / Plugins / ,我创建了 MyCDVPlugin.m MyCDVPlugin.h / p>

我以前写过C,所以我理解头文件,但是对于那些没有的头文件,它基本上告诉编译器需要寻找什么,所以我们只是告诉它我们的方法的名称,并导入Cordova的标题:

  #import< Cordova / CDVPlugin.h> 

@interface MyCDVPlugin:CDVPlugin

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

@end



这些参数是标准Cordova插件参数我所知)。我们的函数,作为一个getter,实际上没有任何参数在一个意义上,但那些仍然是必需的。 ( options 可能实际上是可选的?我没有测试。)



.m ,所有我们需要的是实际的函数,我们的标头从前,和 CDVPluginResult.h

  #importMyCDVPlugin.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

基本上,编号并将其传回您的成功回调。 (我没有想到这个方法真的有一个失败的情况,所以它没有失败回调。)



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





此外,请务必将您的插件添加到 Cordova.plist $ c>插件条目:





从这里,很容易从JavaScript调用该方法(确保使用 deviceready 被触发):

  //获取并显示版本号
var gotVersionNumber = function (version){
//缓存值,以便我们以后可以使用它,如果我们需要它
Hub.Global.Version = version;
$('。version-number')。text(version);
};

//我的插件甚至没有失败回调,所以我们传递null。
//第五个参数(传递给我们的Obj-C方法的参数)不是
//可选。必须至少为空数组
cordova.exec(gotVersionNumber,null,MyCDVPlugin,getVersionNumber,[]);

就是这样!简单,对吧?希望这有助于别人有点淹没在PhoneGap的Obj-C一边。


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

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.

Thanks!

解决方案

I wanted to offer my solution (based off of Adam Ware's solution).

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).

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.m and MyCDVPlugin.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:

#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. (options might actually be optional? I didn't test.)

In our .m, all we need is the actual function, our header from before, and 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:

Also, make sure you add your plugin to your Cordova.plist Plugins entry:

From there, it's pretty simple to call the method from JavaScript (make sure to use it after deviceready is triggered):

// 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.

这篇关于如何获得应用程序版本和构建在iOS PhoneGap应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆