Search code examples
iosxcodebuild

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


I have an Xcode project with several targets. Let's say the target names are AppFreeVersion and AppPaidVersion. They share the same codebase, but I want to implement some logic that is only for one of the targets.

Without modifying my scheme or build settings, is there a way to get the string of my current target name? The solution given in this question requires me to pass an environment variable in the build setting, which I don't want to do.


Solution

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

    And I guess you already know how to get it

    Obj-C

    [[NSBundle mainBundle] objectForInfoDictionaryKey:key_name];
    

    Swift

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