在运行时查找 Xcode Build 方案名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15857285/
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
Find the Xcode Build scheme name at run time
提问by user390687
Is there a way to find out the Scheme used at the run time ?
有没有办法找出运行时使用的方案?
May be there is a better way to do this, I'm trying to set the Access Environment (Production vs Development) by using the Scheme name.
可能有更好的方法来做到这一点,我正在尝试使用 Scheme 名称设置访问环境(生产与开发)。
Currently I use #define ENV @"PROD"
in App-Prefix.pch
file to do this, but there is a chance to miss this when sending pkg to apple for review :(
目前我使用#define ENV @"PROD"
in App-Prefix.pch
file 来执行此操作,但是在将 pkg 发送给 Apple 进行审核时有可能会错过此操作:(
采纳答案by ldiqual
When running an app on the simulator or on your device, the DEBUG
variable is set, allowing you to use it from your code:
在模拟器或您的设备上运行应用程序时,该DEBUG
变量已设置,允许您从代码中使用它:
#ifdef DEBUG
// Something
#else
// Something else
#endif
You can see this variable from your target's build settings:
您可以从目标的构建设置中看到此变量:
As soon as the Run
configuration is set on Debug
(Product -> Scheme -> Edit Scheme), this variable will be set:
一旦Run
在Debug
(产品 -> 方案 -> 编辑方案)上设置配置,此变量将被设置:
回答by David K. Hess
I searched far and wide for an answer to this because I really don't like the idea of creating extra Targets nor extra Configuration sets. Both of those options just create a huge Configuration synchronization problem.
我四处寻找答案,因为我真的不喜欢创建额外目标或额外配置集的想法。这两个选项只会造成巨大的配置同步问题。
So, after hacking Xcode for a couple of hours, this is what I came up with:
所以,在破解 Xcode 几个小时后,这就是我想出的:
Step 1: Add the key "SchemeName" to your Info.plist with type string.
第 1 步:使用类型字符串将密钥“SchemeName”添加到您的 Info.plist。
Step 2: Edit your default scheme and on Build -> Pre-actions add a new Run Script with the following:
第 2 步:编辑您的默认方案并在 Build -> Pre-actions 上添加一个新的 Run Script,其内容如下:
/usr/libexec/PlistBuddy -c "Set :SchemeName \"$SCHEME_NAME\"" "$PROJECT_DIR/$INFOPLIST_FILE"
Make sure and select a target from under "Provide build settings from".
确保并从“提供构建设置来自”下选择一个目标。
Step 3: Now duplicatethat scheme as many times as you like (Manage Schemes... -> Select existing scheme -> Click gear icon -> Duplicate) For instance, you can create Development, Staging, Production, App Store, etc. Don't forget to click "shared" if you want these schemes carried around in version control.
第 3 步:现在根据需要多次复制该方案(管理方案... -> 选择现有方案 -> 单击齿轮图标 -> 复制)例如,您可以创建开发、暂存、生产、应用商店等。如果您希望在版本控制中使用这些方案,请不要忘记单击“共享”。
Step 4: In the code, you can retrieve the value like this:
第 4 步:在代码中,您可以像这样检索值:
NSString *schemeName = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"SchemeName"];
Or in Swift:
或者在 Swift 中:
let schemeName = Bundle.main.infoDictionary?["SchemeName"] as? String ?? ""
Now, the code can configure itself correctly at runtime. No nasty preprocessor macros to deal with and no brittle configuration mess to maintain.
现在,代码可以在运行时正确配置自身。无需处理讨厌的预处理器宏,也无需维护易碎的配置混乱。
UPDATE: According to @JAHelia, $PROJECT_DIR
needs to be removed for Xcode 11.4. See their comment below.
更新:根据@JAHelia,$PROJECT_DIR
需要为 Xcode 11.4 删除。请参阅下面的评论。