Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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.

Thanks!

like image 836
andrewpthorp Avatar asked Jul 18 '12 21:07

andrewpthorp


People also ask

What do you need in IOS to develop the phone gap App?

To develop apps using PhoneGap, the developer does not require to have knowledge of mobile programming language but only web-development languages like, HTML, CSS, and JScript. PhoneGap produces apps for all popular mobile OS platforms such as iOS, Android, BlackBerry, and Windows Mobile OS etc.

What does Cordova build IOS do?

Cordova build command converts our app code to an xcode project. Using Xcode we create an . ipa file which is the actual app to be installed. Before moving forward double tap on both Certificates to add them to your keychain.

What is Cordova for IOS?

Introduction. Apache Cordova is a library used to create native mobile applications using Web technologies. The application is created using HTML, CSS and JavaScript and compiled for each specific platform using the platform native tools.


2 Answers

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:

Dragging files into the project.

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

Editing Cordova.plist

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.

like image 124
CWSpear Avatar answered Nov 16 '22 02:11

CWSpear


As a small tweak to @CWSpear's awesome answer, I also wanted to grab the 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 = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dict];

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.

Steve

like image 30
daxiang28 Avatar answered Nov 16 '22 03:11

daxiang28