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
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
#else
//In extension
#end
You can define your own macro.
In the project settings use the dropdown in the topbar to select your extension target:
Then:
- Click
Build Settings
- Find (or search)
Preprocessor Macros
underApple LLVM 6.0 - Preprocessing
- Add
TARGET_IS_EXTENSION
or any other name of your choice in both the debug and release sections.
Then in your code:
#ifndef TARGET_IS_EXTENSION
// Do your calls to UIApplication
#endif
__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.
Example:
@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!
}