Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using preprocessor definitions in iOS App info.plist

I have an iOS app that uses SSO with Facebook as part of getting your app to work with Facebook's SSO you have to specify the FacebookAppId and callback url in the info.plist. I have 2 different Facebook groups one for testing and one for production. I want to be able to have the values of the two keys specified above set based on preprocessor directives.

In my MessageBomb-Prefix.pch i have the following, which works fine:

#ifdef TEST
    //Development environments
    #define PARSE_APP_ID "dev parse app id"
    #define PARSE_CLIENT_ID "dev parse client id"
    #define FB_APP_ID "dev facebook id"
#else
    //Production environments
    #define PARSE_APP_ID "prod parse app id"
    #define PARSE_CLIENT_ID "prod parse client id"
    #define FB_APP_ID "prod facebook id"
#endif

However in my info.plist i have done the below, but it doesn't seem to work:

<key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>fb${FB_APP_ID}</string>
            </array>
        </dict>
    </array>
    <key>FacebookAppID</key>
    <string>${FB_APP_ID}</string>

Is it even possible to do what i'm trying to do. I've set the project to preprocess the info.plist.

Thanks Peter

like image 480
user1956608 Avatar asked Jan 08 '13 00:01

user1956608


3 Answers

If you go into Build Settings you can set Preprocess Info.plist File to YES and then set Info.plist Preprocessor Prefix File to a .h file you've placed in your project with #define directives.

So for example if I add a key to my Info.plist file: testKey with the value TEST_VALUE. Then I place test.h in my project with:

#define TEST_VALUE hello_world

And then I set Preprocess Info.plist File to YES and Info.plist Preprocessor Prefix File to test.h

The value of that key, when retrieved is now "hello_world". If you change the value of the macro a lot you may find you need to do Product/Clean to see changes because XCode seems to cache the compiled value.

like image 180
Andrew Tetlaw Avatar answered Oct 04 '22 23:10

Andrew Tetlaw


To access in plist:

Create a new variable
enter image description here


Define it
enter image description here


Use it in your info plist
enter image description here

like image 22
hfossli Avatar answered Oct 04 '22 22:10

hfossli


The Info.plist preprocessor will let you use build settings in your Info.plist, not #defines. So you can define FB_APP_ID as a custom user build setting in your target (and give it overrides for different schemes), and this value will then be available to Info.plist. However, user build settings don't get exposed to your code. That is, unless you muck with your Preprocessor Definitions build setting and add an entry that looks like

FB_APP_ID=@\"$(FB_APP_ID)\"

(the backslashes are required to get the double-quotes past the shell invocation)

If your app ID may contain spaces, then you'll need to add quotes to get past the shell invocation:

FB_APP_ID="@\"$(FB_APP_ID)\""

In the end, you'll have build settings that looks something like this:

Build settings

like image 40
Lily Ballard Avatar answered Oct 04 '22 22:10

Lily Ballard