简体   繁体   English

在iOS App info.plist中使用预处理程序定义

[英]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. 我有一个iOS应用程序,该应用程序与Facebook一起使用SSO,作为使您的应用程序与Facebook的SSO一起使用的一部分,您必须在info.plist中指定FacebookAppId和回调URL。 I have 2 different Facebook groups one for testing and one for production. 我有2个不同的Facebook组,一个用于测试,另一个用于生产。 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: 在我的MessageBomb-Prefix.pch中,我可以执行以下操作:

#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: 但是,在我的info.plist中,我已完成以下操作,但似乎不起作用:

<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. 我已将项目设置为预处理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. 如果进入“构建设置”,则可以将Preprocess Info.plist File设置为YES ,然后将Info.plist Preprocessor Prefix File设置为使用#define指令放置在项目中的.h文件。

So for example if I add a key to my Info.plist file: testKey with the value TEST_VALUE . 因此,例如,如果我将一个密钥添加到Info.plist文件: testKey ,其值为TEST_VALUE Then I place test.h in my project with: 然后我将test.h放在我的项目中,其中包括:

#define TEST_VALUE hello_world

And then I set Preprocess Info.plist File to YES and Info.plist Preprocessor Prefix File to test.h 然后将Preprocess Info.plist File设置为YES并将Info.plist Preprocessor Prefix Filetest.h

The value of that key, when retrieved is now "hello_world". 现在,该键的值在检索后为“ 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. 如果您更改宏的值很多,您可能会发现您需要执行Product / Clean才能看到更改,因为XCode似乎缓存了编译后的值。

The Info.plist preprocessor will let you use build settings in your Info.plist, not #define s. Info.plist预处理程序将使您可以使用Info.plist中的构建设置,而不是#define 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. 因此,您可以将FB_APP_ID定义为目标中的自定义用户构建设置(并为不同的方案提供覆盖),然后该值将可供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 也就是说,除非您弄混了Preprocessor Definitions构建设置并添加了如下所示的条目

FB_APP_ID=@\"$(FB_APP_ID)\"

(the backslashes are required to get the double-quotes past the shell invocation) (需要反斜杠以使双引号超过shell调用)

If your app ID may contain spaces, then you'll need to add quotes to get past the shell invocation: 如果您的应用程序ID可能包含空格,那么您需要添加引号来跳过shell调用:

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

In the end, you'll have build settings that looks something like this: 最后,您将具有类似于以下内容的构建设置:

构建设置

To access in plist: 要在plist中访问:

Create a new variable 创建一个新变量
在此处输入图片说明


Define it 定义它
在此处输入图片说明


Use it in your info plist 在您的信息列表中使用它
在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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