Home » Ios » What is the preprocessor macro to test whether an application extension is being built?

What is the preprocessor macro to test whether an application extension is being built?

Posted by: admin February 24, 2018 Leave a comment


This question is based purely on publicly released documents regarding the introduction of application extensions in iOS.

With the introduction of app extensions in iOS 8, it is now possible to “extend custom functionality and content beyond your app and make it available to users while they’re using other apps”.

In my implementation of my extension, I am including some classes from my actual app in my extension (models, etc). The problem is that these classes make calls to UIApplication, which is not available in an app extension, and the compiler tells me so.

I thought an easy solution to this would to be enclose any calls to UIApplication in an #if directive.

For example, if I wanted to only include code if I was running on a simulator, I would use:

    // Code Here

Is there a similar defined macro when the target is an application extension?


You can define your own macro.

In the project settings use the dropdown in the topbar to select your extension target:
enter image description here


  1. Click Build Settings
  2. Find (or search) Preprocessor Macros under Apple LLVM 6.0 - Preprocessing
  3. Add TARGET_IS_EXTENSION or any other name of your choice in both the debug and release sections.

Then in your code:

    // Do your calls to UIApplication


You can use the same technique than Apple uses for raising the compilation error.

#if !(defined(__has_feature) && __has_feature(attribute_availability_app_extension))
  //Not in the extension
  //In extension


Update: Unfortunately, it doesn’t actually work because it is working in __has_feature(attribute_availability_app_extension)-feature manner. Sad.

It is not actually that was asked but it is should be noted:

If you are using Swift, you have @available(iOSApplicationExtension) attribute! It is not actually preprocessor’s feature but it is kind of compile time feature.


@available(iOSApplicationExtension, message="It is meaningless outside keyboard extension")
public var rootInputViewController: UIInputViewController {
    return storedInputViewController

Or with #-notation (but probably not):

public static var rootInputViewController: UIInputViewController! {
    guard #available(iOSApplicationExtension 8, *) else {
        return nil

    return storedInputViewController!