Search code examples
iphoneobjective-cxcodebuild-process

How to get Target name?


We know that Xcode maintains environment variable of ${TARGET_NAME} but how to access this variable in objective-C code ?

What I have tried ?
I have added "TARGET_NAME=${TARGET_NAME}" this in Preprocessor macros section of Build Settings. But now I am not sure how to use this variable "TARGET_NAME" as a string in objective-C code.

In my case product name and target name are different so, no chance to use that.

I tried to access using

#ifdef TARGET_NAME
 NSLog(@"TargetIdentifier %@",TARGET_NAME);
#endif

This code is giving error like "Use of undeclared identifier 'myapptargetname'"


Solution

  • You can add "TargetName" key to your Info.plist file:

    enter image description here

    Then you can access it (swift code):

    var plistFileName = NSBundle.mainBundle().infoDictionary?["TargetName"] as String