-1

I have a class UserAccountManager that needs to be accessible from two targets, the main iOS app and an extension respectively. During initialization, this class will call out to a separate framework that due to its dependence on the AppDelegate is only accessible from the main iOS application. Unfortunately this does not compile as that framework is not available in the extension.

How can I check the target membership of that framework, and only make the call if its available?

Aleksander
  • 2,735
  • 5
  • 34
  • 57

1 Answers1

1

You can use conditional compilation.

Add -DHAS_APP_DELEGATE (or however you'd like to call this) to "Other C Flags" build setting to the target that has AppDelegate.

Then you can conditionally compile your ObjC code.

#ifdef HAS_APP_DELEGATE
    // Call to framework that depends on AppDelegate
#else
    // Do something else instead
#endif

You can ignore the else branch, if you just want to skip the call to the framework. You can use #ifdef when doing imports too, to only import the framework when it can be used.

Here is a question that might contain other approaches too: Conditional compilation check for framework before importing

juhan_h
  • 3,965
  • 4
  • 29
  • 35