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

后端 未结 4 1099
天涯浪人
天涯浪人 2020-12-14 10:34

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

\"BUILD\"<

4条回答
  •  伪装坚强ぢ
    2020-12-14 11:19

    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

提交回复
热议问题