Xcode - Importing different header file with same name based on Target - iphone

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?

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.

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

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

Related

including a header from a project into a static library

I have a subproject (static library) inside my project.
As this static library may be used by a bunch of app, I have this config.h file on my project that contains the app configuration. The static library must read it.
The problem is that adding
#import "config.h"
on the static library fails, because the file cannot be found.
I could add an absolute path to my project root on the search headers, but I want to make this not hard coded because this static library will be used by other projects. Another problem is that I cannot use relative links like ../.., for example, because the static library is on another volume.
Including $(SRCROOT) on the search paths of the static library will give me the root for that library not for the project using it, that is what I want.
How do I solve that?
Just pay attention to my question. I am inside a static library that is used by a project. Config.h is out there in the project. I want to import that config.h on my static library.
If there is an easy way to do that, please tell me.
I have uploaded a sample project to here and here, so you can see my pain.
thanks
One way, somewhat of a hack, is to add a Run Script to each App's Build Phase, as the first item, and have it copy Config.h to some known place - /tmp/Config.h, and your included library will look for it there. Since the file is copied on every build, it will always be proper.
EDIT1: So not pretty, but you can add a Run Build Script to just under the Dependencies in the library. Just add one and leave the checkbox set to show environmental variables. You can see this one set:
FILE_LIST=/Volumes/Data/Users/dhoerl/Library/Developer/Xcode/DerivedData/MyProject-fdzaatzqtmrnzubseakedvxmsgul/Build/Intermediates/MyStaticLibrary.build/Debug-iphoneos/MyStaticLibrary.build/Objects/LinkFileList
What you can see is that several of these have as the prefix the current project folder:
/Volumes/Data/Users/dhoerl/Library/Developer/Xcode/DerivedData/MyProject-
You can write some script there to get the prefix, then append the local Config.h file path, and now you have a fully qualified path to the Config.h header, which you can then copy to a known location in the library. I'm going to post on the xcode forum as there may be a better solution - there use to be in Xcode 3. I'll update this if I get anything substantive back.
EDIT2: Try This:
1) Click on the library, click on Build Phases, add a Run Script Build phase by tapping bottom right '+' button
2) Drag it so its the second item in the list (below Target Dependencies)
3) Change the Shell to "/bin/ksh"
4) Paste this in, after editing it to have the proper files/paths:
# Get the Project Name (assumes upper/lower/numbers only in name)
PROJ=$(echo $BUILD_DIR | sed -En -e 's/(\/.*\/)([A-Za-z0-9]+)-([a-z]+\/Build\/Products)/\2/p')
# Use this variable to construct a full path
FULL_PATH="/Volumes/Data/Users/dhoerl/Downloads/nightmare/"$PROJ"/"$PROJ"/HelloWorldLayer.h"
echo FULL_PATH equals $FULL_PATH
# Make Sure MyStaticLibrary is correct
echo PROJ_DIR equals "$PROJECT_DIR/MyStaticLibrary"
cp -f "$FULL_PATH" "$PROJECT_DIR/MyStaticLibrary"
5) This assumes that you put the library anywhere you want, but each App project has to havethe same parent folder (not much of a restriction - what I did in the past).
PS: When I do this kind of thing, I usually don't include an actual file of the app, but create a header for a class that is not instantiated in the library. Lets call this Foo. So in your library, you have Foo.h, and it has lots of methods that return info - the number of widgets, the location of some special folder, plists, arrays, dictionaries, whatever. The idea is the library knows how to get whatever it needs through this interface (class singleton, or just a class with class methods. YMMV.
PSS: anyone else reading this, it pays to create demo projects.
I'd go a different route. Make this config.h file part of the static library using compiler symbols in it to switch features. Then in your projects define those symbols depending on what features you need.

Static Library path nightmare from hell

This is the point. I have a subproject (static library) created. The library compiles well. No errors. When I include this library inside a project and imports a header from that library on the project the library fails to compile because it cannot find a path that belongs to itself.
After following a bunch of tutorials on the web on how to create a static library and embed that in a project, I don't know which one is the correct one, because I have tried all and all failed and some differ.
This is how the library is set:
STATIC LIBRARY
BUILD SETTINGS:
Public header folder path = $(BUILT_PRODUCTS_DIR)
Header search path = $(SRCROOT) (recursive)
BUILD PHASES
COPY FILES = 1 file, myLibrary.h that is basically empty (created by xcode when I used the static library template to start the library.
no ADD COPY HEADERS phase
MAIN PROJECT
BUILD SETTINGS
Header search path = empty
User header search path = $(BUILT_PRODUCTS_DIR) (recursive)
Always search user paths = YES
BUILD PHASES
Yes, myLibrary.a is on target dependencies
What amazes me is that the library compiles fine alone but when put inside a project, is unable to find a header that belongs to the own library.
NOTE: I have also tried to create a copy headers phase on the library making public all .h on that library, but it failed too.
This is an example of one error:
/Users/mike/Library/Developer/Xcode/DerivedData/MyApp-dnaimaovscreasadhmvgttwjnabc/Build/Products/Debug-iphoneos/include/myLibrary/ccTypes.h:39:9:
fatal error: 'Platforms/CCGL.h' file not found
#import "Platforms/CCGL.h"
I have lost two days trying to solve this nightmare.
any thoughts?
I have uploaded a sample project to here and here
After downloading your sample project, I had it working in a few minutes by making the following changes.
First, you need to add an entry for the MyProject target's Build Settings under Header Search Paths, so that the files such as HelloWorldLayer.h, which #import "cocos2d.h", know where to find that file.
Under the Header Search Paths, I entered ../MyStaticLibrary/MyStaticLibrary/libs/** like shown in the image below:
That solved the problem of the inability of the preprocessor to find the necessary source files from the MyStaticLibrary, for the MyProject project, but after compiling, I got an error about missing symbols. I needed to add the built libMyStaticLibrary.a to the Link Binary With Libraries step like shown in the image below:
After that, the project compiles fine.
(Not enough rep to post comment...)
Did you try specifically including the Platforms directory in the header search path? Presumably, Platforms is in the source directory, not in $(BUILT_PRODUCTS_DIR) so may not be searched in your current setting.
Regarding script to show you env variables, here's how I do it (open images at the new tab for better scaling):
Added dummy shell script
Observed its output at Log Navigator
As you can see, BUILT_PRODUCTS_DIR doesn't have any headers copied from the library. Either put headers there manually (strongly not recommended) or add search path to the location that you know the headers must be at:
Now as there were two headers cocos2d.h and MyStaticLibrary.h, cocos2d.h was successfully imported although it will have additional dependency.
The path ../MyStaticLibrary/build/$(BUILD_STYLE)-$(PLATFORM_NAME) will also (recursively) have public headers of the library.
After almost 5 days of a nightmare trying to solve that, I finally found this tutorial: http://www.youtube.com/watch?v=XUhLETxgcoE
This is the only solution that worked for me. The simplest and the best.
Thanks for every one trying to solve this.
Please try this…
step1: right click on the 'projectName.xcodeproj'(static library).Choose 'Get info' and copy the full path from(General->where)
step2: Paste full path to 'Header search paths' for your main project(Both Targets and Project )

Creating an IPA, "contains multiple products"

I am working on an email client based on ReMail. Basically, I reused the ReMail project so that the MailCore etc dependencies would be all set up out of the box so I could build a new app on top of them. I'm trying to give the app to some beta testers via ad hoc, but when I try to create an IPA, I receive this error:
xxxx does not contain a single–bundle application or contains multiple products. Please select another archive, or adjust your scheme to create a single–bundle application.
I have set Skip Install to YES for all dependencies, and moved all files under Copy Header to the "project" section, but I am still unable to build a single APP file.
I don't know if this is significant, but when I open up the xcarchive file, within the products directory, I find a the APP file under Applications, and then a file structure mirroring the absolute path to the header files for MailCore.
Any ideas as to what might be going on? I'm very near the tearing-out-chunks-of-hair-in-frustration stage, and I don't think the unnaturally bald crazy person look would suit me.
Thanks!
Try removing the “Copy Headers” phase entirely. It's only supposed to be used for frameworks, not static libraries. The Xcode static library template that includes a “Copy Headers” phase is wrong.
Check out the “Working with Schemes and Projects in Xcode” video from WWDC 2012, starting at 45m10s.

Customize iphone app for different clients

I have an existing app that needs to be compiled for different clients
Each client requires their own icon and splash screen.
I would also like to be able to conditionally include various features depending whether the particular client requires them or not.
I have tried setting up different targets for each client, but not having much luck so far.
The different resources with the same name, but a different path keep getting mixed up.
Ideally I would like to be able to build an app by duplicating another client that is similar and then just make the minimum number of changes to create the app for the new client.
What is the best way to set this app up?
Separate targets for each client should be the way to go. For the features, I would suggest first setting up a macro identifying the client in the target settings (under "Preprocessor Macros" on the build tab), then having a FeatureDefines.h file that looks like this:
#ifdef macroClientA // assume client A wants features 1 and 3
# define macroFeature1
# define macroFeature3
#endif
// and similarly for the other clients
Now you can use
#import featureDefines
#ifdef macroFeature1
any place you need to test if feature 1 is desired or not.
For the separate icons, your target settings can specify a different info.plist file for each client, and those files can in turn specify a different filename for the icon.
For the separate splash screens, iOS always requires the splash screen to be named Default.png, but they can go in different subdirectories of your project directory. You can control which one is used for which target by right clicking where Xcode says "Groups & Files", selecting Target Membership, then checking the checkbox for the one you want to use, and making sure the other ones are unchecked.
For resources, I would suggest naming your resource files like this:
resourceName.ext // generic resource to be used if there is no client-specific one
resourceName-clientName.ext // client-specific resource
Next set up a general resource-finder method that looks something like this:
-(NSString *) resourcePathForResourceName: (NSString *) resourceName extension: (NSString *) ext {
NSString *clientName;
#ifdef macroClientA
clientName = #"clientA";
#endif // and similarly for the other clients
NSString *clientSpecificName = [NSString stringWithFormat: #"%#-%#.%#", resourceName, clientName, ext];
NSString *genericName = [NSString stringWithFormat: #"%#.%#"];
if ([[NSFileManager defaultManager] fileExistsAtPath: clientSpecificName])
return clientSpecificName;
else if ([[NSFileManager defaultManager] fileExistsAtPath: genericName])
return genericName;
else
// handle the error
}
Running all your resource file grabs through that method will allow you to add client-specific resources to your project without changing a single line of code.
I have a similar scenario and how I handle it is as follows:
1) the core code of the app is kept in a "application_name-base" folder
2) different clients are in their "application_name-client_name" folder
3) the project file is in the client folder and includes the references from the base folder without using copy.
4) files that need to be unique to the the client's project are in the client folder. Usually images using the same name. or .h .m files that need to be unique to the individual project. Also allows for you to not include files on a project by project basis.
Keeps code central but allows for different code per client without leading to confusion.
You could make a separate target for each client, and put each clients assets in their own folder with the same name. I did something similar where I had two projects which had a lot in common and just added/removed the appropriate assets from each project.
With your scenario, at that point you can just add a new folder with the client specific resources to each target and it wouldn't require any code changes. If the paths are an issue, you could consider using the [UIImage imageNamed:] method which doesn't require the full path, just the filename.
I'd rather write some automator/scripts instead of using XCode for such duties !
http://developer.apple.com/tools/xcode/automatorforxcode.html
Write your code to allow an external configuration file to do the bulk of the setup, then include the correct assets with your targets. Having one project with a new target for each client, you can choose which assets get included with each target. If you're having trouble with the multiple targets you could use git to manage a core project, then branch it for each client.
I would just create a settings interface for a client to go in and customize the app to their company/individual needs. For example, providing the image from their Photo Library, etc.
You can make project for each customer, and include all sources from the same path, without copying them into project directory.
So you'll be able to replace icon/splash screen in a project, while using the same codebase.
This looks like the easiest way so far.

Change a localized InfoPlist.strings using an Xcode target

Here's an obscure problem. I'm using an InfoPlist.strings to localize my app name. It's only got one value: CFBundleDisplayName = "Mon App". The strings file is localized (putting it in a directory for that localization).
I've just made an extra target, where I change things like the non-localized app name (different Info.plists), and the icon. I'm also changing the Default.png using a run script build phase (copying different files depending on the app type I'm building).
I've tried using the script to copy different versions of my InfoPlist.strings, but I couldn't make it work. Here's what I used:
if($TARGET_NAME == "MonApp")then
cp fr.lproj/MonApp_InfoPlist.strings fr.lproj/InfoPlist.strings
endif
I've seen a post suggesting wincent strings util for processing strings, but wanted to see if there's an easy way to do this. Any help greatly appreciated.
You don't need to do this.
If you have fr.lprog/InfoPlist.strings and en.lproj/InfoPlist.strings in your project, you should see just one InfoPlist.strings entry, with two subitems fr and en. If you drag and drop the InfoPlist.strings file into a Copy Bundle Resources build phase, all localized versions will be copied into the appropriate .lproj files at build time. You don't need to create a separate target or write a script to do this; the right thing happens.