Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
615 views
in Technique[技术] by (71.8m points)

ios - Find the Xcode Build scheme name at run time

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.

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 :(

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

Step 1: Add the key "SchemeName" to your Info.plist with type string.

Step 2: Edit your default scheme and on Build -> Pre-actions add a new Run Script with the following:

/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 duplicate that 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.

Step 4: In the code, you can retrieve the value like this:

NSString *schemeName = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"SchemeName"];

Or in 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 the comments below, $PROJECT_DIR might need to be removed depending on where your Info.plist is located in your project.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...