xcode 在 iOS App info.plist 中使用预处理器定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14206621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:28:54  来源:igfitidea点击:

Using preprocessor definitions in iOS App info.plist

objective-ciosxcode

提问by user1956608

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.

我有一个 iOS 应用程序,它使用 Facebook 的 SSO 作为让您的应用程序与 Facebook 的 SSO 一起使用的一部分,您必须在 info.plist 中指定 FacebookAppId 和回调 URL。我有 2 个不同的 Facebook 群组,一个用于测试,另一个用于生产。我希望能够根据预处理器指令设置上面指定的两个键的值。

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

谢谢彼得

回答by Andrew Tetlaw

If you go into Build Settings you can set Preprocess Info.plist Fileto YESand then set Info.plist Preprocessor Prefix Fileto a .hfile you've placed in your project with #definedirectives.

如果您进入构建设置,您可以设置Preprocess Info.plist FileYES然后设置Info.plist Preprocessor Prefix File.h您已使用#define指令放置在项目中的文件。

So for example if I add a key to my Info.plistfile: testKeywith the value TEST_VALUE. Then I place test.hin my project with:

例如,如果我向我的Info.plist文件添加一个键:testKey值为TEST_VALUE. 然后我test.h在我的项目中放置:

#define TEST_VALUE hello_world

And then I set Preprocess Info.plist Fileto YESand Info.plist Preprocessor Prefix Fileto test.h

然后我设置Preprocess Info.plist FileYESInfo.plist Preprocessor Prefix Filetest.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.

检索时该键的值现在是“hello_world”。如果您大量更改宏的值,您可能会发现需要执行 Product/Clean 才能看到更改,因为 XCode 似乎缓存了编译后的值。

回答by Lily Ballard

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

Info.plist 预处理器将允许您在 Info.plist 中使用构建设置,而不是#defines。因此,您可以FB_APP_ID在目标中定义为自定义用户构建设置(并为不同的方案赋予它覆盖),然后该值将可用于 Info.plist。但是,用户构建设置不会暴露给您的代码。也就是说,除非你搞砸了你的预处理器定义构建设置并添加了一个看起来像的条目

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:

最后,您将拥有如下所示的构建设置:

Build settings

构建设置

回答by hfossli

To access in plist:

在 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

在您的信息 plist 中使用它
在此处输入图片说明