简体   繁体   中英

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

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.

The Info.plist preprocessor will let you use build settings in your Info.plist, not #define s. 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:

构建设置

To access in plist:

Create a new variable
在此处输入图片说明


Define it
在此处输入图片说明


Use it in your info plist
在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM