How do I check the TARGET_NAME of my iPhone app on XCode?

不打扰是莪最后的温柔 提交于 2020-01-01 09:04:10

问题


I'm trying to have 2 version of my iPhone application within the same XCode project. The codebase it's almost the same and where I need to have different behaviours I've decided to use preprocessor's conditionals and the ${TARGET_NAME} tag.

I've set the OTHER_CFLAGS to contain "-DTARGET_NAME=${TARGET_NAME}".

Then in my code I tried to do

#if TARGET_NAME == myApp
  NSLog(@"pro");
#elif TARGET_NAME == myAppLite
  NSLog(@"lite");
#endif

Unfortunately I always get "lite" printed out since TARGET_NAME == myApp it's always true: since TARGET_NAME is defined. I cannot for the life of me figure out how to evaluate this string comparison. Any idea?

thanks in advance


回答1:


You can't compare strings like that in an #if block. Instead, add the defines to each specific target. For instance, on the full version's target, open the Info panel and go to the build tab and add something like FULL_VERSION to the GCC_PREPROCESSOR_DEFINITIONS build setting. Then, for the lite target, enter something like LITE_VERSION. In your code, you can do:

#ifdef FULL_VERSION
NSLog(@"Full");
#else
NSLog(@"Lite");
#endif



回答2:


Actually you can get the target's name to compare it, but this will not skip unnecessary code from other targets at compile time, to do this:

First go to menu Product -> Scheme -> Edit Scheme... (or CMD + <) Then in the arguments section, add inside environment variables something like:

In your code you can get the target's name as:

NSString *targetName = [[NSProcessInfo processInfo] environment][@"TARGET_NAME"];
NSLog(@"target = %@", targetName); // Will print the target's name

You can compare that string now in runtime.

But following your example: if you want that all the Pro version code to be omitted at compile time. You should do what @jason-coco says. And go to preprocessor macros in build settings, and add $(TARGET_NAME) there:

The code inside the #define will be compiled and executed if my target is "MLBGoldPA"

#if defined MLBGoldPA
    NSLog(@"Compiling MLBGoldPA");
#endif



回答3:


to get your conditional evaluation working, you have to do something like:

#define myApp       1
#define myAppLite   2

beforehand, like in your _Prefix.pch file.



来源:https://stackoverflow.com/questions/1027091/how-do-i-check-the-target-name-of-my-iphone-app-on-xcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!