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
Using preprocessor definitions in iOS App info.plist
提问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 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
为.h
您已使用#define
指令放置在项目中的文件。
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:
例如,如果我向我的Info.plist
文件添加一个键:testKey
值为TEST_VALUE
. 然后我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 File
到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.
检索时该键的值现在是“hello_world”。如果您大量更改宏的值,您可能会发现需要执行 Product/Clean 才能看到更改,因为 XCode 似乎缓存了编译后的值。
回答by Lily Ballard
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
Info.plist 预处理器将允许您在 Info.plist 中使用构建设置,而不是#define
s。因此,您可以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:
最后,您将拥有如下所示的构建设置:
回答by hfossli
To access in plist:
在 plist 中访问:
Create a new variable
创建一个新变量
Define it
定义它
Use it in your info plist
在您的信息 plist 中使用它