How to get the name of the target that is currently running in Xcode?

I have an Xcode project with several goals. Let's say the target names are AppFreeVersion and AppPaidVersion . They have the same code base, but I want to implement some logic, which is intended only for one of the purposes.

Without changing my schema or assembly settings, is there a way to get the string of my current target name? The solution given in this question requires me to pass the environment variable in the build setup, which I do not want to do.

+5
source share
3 answers

How about adding ${TARGET_NAME} to your info.plist?

And I think you already know how to get it

Obj-c

 [[NSBundle mainBundle] objectForInfoDictionaryKey:key_name]; 

Swift

 let targetName = NSBundle.mainBundle().infoDictionary?[key_name] as String 
+5
source

Swift 3.0 solution.

 func getTargetName() -> String { return Bundle.main.infoDictionary?["CFBundleName"] as! String } 
+5
source

Just set the target name in your schema -> environment variables -> add a name and value. for example: targetName = "mytesttarget"

Obj-c

NSDictionary * envir = [[NSProcessInfo process environment]];

NSString * targetName = envir [@ "targetName"];

Swift

let envir = NSProcessInfo.processInfo (). environment

let targetName = envir ["targetName"]

0
source

Source: https://habr.com/ru/post/1247573/


All Articles