iosobjective-cversionnsbundlecompile-time-constant

How to get CFBundleShortVersionString as a constant


I append parts to a constant base URL string in my code as such:

#define BASE_URL @"https://example.com/developer/"
#define PHP_SCRIPT BASE_URL @"index.php"

such that the resulting PHP_SCRIPT refers to https://example.com/developer/index.php

I am looking for a way to insert my application's CFBundleShortVersionString into this concatenation. For example, if the CFBundleShortVersionString is 1.12 I want the final URL to be https://example.com/developer/1_12/

I know that the CFBundleShortVersionString can be accessed by

[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] 

and

(__bridge NSString *)(CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(), kCFBundleVersionKey))

but I need help to concatenate it into a constant string.


Solution

  • This should do it.

    #define BASE_URL @"https://example.com/developer/"
    #define PHP_SCRIPT [NSString stringWithFormat:@"%@%@/", BASE_URL, [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] stringByReplacingOccurrencesOfString:@"." withString:@"_"]]