简体   繁体   中英

Conditional compilation of Objective-C framework

I'm developing an Objective-C framework and I need to be able to build 2 different versions of that framework:

  • The basic framework,
  • The basic framework + some extra functionalities (some of which are written in Swift).

In the header of my framework, I expose my public headers like this:

#import <MyFramework/FrameworkPublicHeader1.h>
#import <MyFramework/FrameworkPublicHeader2.h>

#import <MyFramework/FrameworkExtraFunctionalityPublicHeader1.h>
#import <MyFramework/FrameworkExtraFunctionalityPublicHeader2.h>

So far, I created a second target for my framework with extra functionalities.

So I have my "basic framework" target with all the classes it needs referenced in the Build Phases tab of the target. And I have my "framework with extra functionalities" target, with a few more classes referenced in the Build Phases tab of the target.

When I want to build the "basic framework", I select my "basic framework" target, and I edit the header of the framework to import only what's needed:

#import <MyFramework/FrameworkPublicHeader1.h>
#import <MyFramework/FrameworkPublicHeader2.h>

//#import <MyFramework/FrameworkExtraFunctionalityPublicHeader1.h>
//#import <MyFramework/FrameworkExtraFunctionalityPublicHeader2.h>

When I want to build the "framework with extra functionalities", I select my "framework with extra functionalities" target, and I edit my framework header to import the headers I need:

#import <MyFramework/FrameworkPublicHeader1.h>
#import <MyFramework/FrameworkPublicHeader2.h>

#import <MyFramework/FrameworkExtraFunctionalityPublicHeader1.h>
#import <MyFramework/FrameworkExtraFunctionalityPublicHeader2.h>

It works, I can build both versions of the framework and use them in my iOS projects.

But I would like it to be simpler than having to edit my framework header every time I switch targets.

I tried to define a preprocessor macro "EXTRAFUNCTIONALITIES=1" in my target's build settings, in Apple Clang - Preprocessing and in Custom Compiler Flags, so that I could just have my framework header like this:

#import <MyFramework/FrameworkPublicHeader1.h>
#import <MyFramework/FrameworkPublicHeader2.h>

#if EXTRAFUNCTIONALITIES
#import <MyFramework/FrameworkExtraFunctionalityPublicHeader1.h>
#import <MyFramework/FrameworkExtraFunctionalityPublicHeader2.h>
#endif

But even though it compiles, the framework cannot be used on my iOS projects anymore when I do that (I have "Unresolved identifier" errors when trying to compile the app that uses my framework).

What am I doing wrong? Is there an alternative solution to have my framework header expose different public headers depending on the target selected?

I solved the problem by using a different umbrella header file for each target, as suggested by Cy-4AH.

I tried it previously and it didn't work. But now it does! Thank you!

The name of the umbrella header needs to match the product name (as indicated in this answer from another question ), and the import statements need to be updated with that product name, like so:

Product name: MyFrameworkWithExtraFunctionalities

Umbrella header: MyFrameworkWithExtraFunctionalities.h

#import <MyFrameworkWithExtraFunctionalities/FrameworkPublicHeader1.h>
#import <MyFrameworkWithExtraFunctionalities/FrameworkPublicHeader2.h>

#import <MyFrameworkWithExtraFunctionalities/FrameworkExtraFunctionalityPublicHeader1.h>
#import <MyFrameworkWithExtraFunctionalities/FrameworkExtraFunctionalityPublicHeader2.h>

When I tried previously to make a new umbrella header, I wasn't giving it the same name as the product name, and the framework was still compiling well, but it wasn't usable by my iOS projects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM