Xcode - Importing different header file with same

2020-02-24 04:39发布

问题:

I have a project with multiple targets each of which builds a pretty similar versions of the app but with different images assets and plists. For plists/images that's fine but I use the ShareKit and Appirater frameworks which have header files with #defines for their config. For each version I believe need to import a different version of this header file, as the config is different for each app built by each target.

So target A has SHConfig.h and target B has a DIFFERENT SHConfig.h

I could edit the source for these frameworks to import different headers based on the target but that'd be messy when I come to upgrade the frameworks.

Is there a better way to import different header files (with the same name) based on the target?

回答1:

Assuming they're in different directories, set the Header Search Paths in each target to put the correct directory first.

You may want to set it to something like $(SRCROOT)/foo:$(HEADER_SEARCH_PATHS), though I'm not sure whether that's necessary.



回答2:

What I found useful was to put the Common directory name in the header search path, and then to use a different #import. My directory structure was Common/Views/v1 and Common/Views/v2. I wanted the v1 for one target and the v2 for another.

In my case, the search path I used in Header Search Paths was:

$(SRCROOT)/../Common/

Then, I used:

#import <Views/v2/ActivityIndicator.h>

In the target that needed the second version (this finds $(SRCROOT)/../Common/Views/v2/ActivityIndicator.h).

Oddly, the other target (the first one I created) is fine without specifying the full path. I.e.,

#import "ActivityIndicator.h"

works to find $(SRCROOT)/../Common/Views/v1/ActivityIndicator.h



回答3:

Following process solved the issue for me

Select specific target Under "Build Phases" --> add "New Headers Phase" --> Expand "Headers" --> click on add(plus symbol) and --> browse to the file to be added specific for the target. (It will add file under 'project' section).

Repeat the process for other targets. Tested on Xcode 10.2