NS_EXTENSION_UNAVAILABLE and Preprocessor Macros not working in Cocoapod

I have a NotificationExtension that shares a codebase with a main application, but should not run some of the code. For a long time I've used NS_EXTENSION_UNAVAILABLE in Objective C on some methods that should not be used in the extension, but in tracking down a recent bug I noticed that those methods are being called in a CocoaPod where they should not (because of NS_EXTENSION_UNAVAILABLE). I verified this when running the Extension target in XCode and setting breakpoints.

I also have a Preprocessor Macro "TARGET_IS_NOTIFICATION=1" in the extension Build Settings and a Swift Active Compilation Condition to filter out some other code inside shared methods/functions. When I realized NS_EXTENSION_UNAVAILABLE wasn't working, I tried the Preprocessor Macro and found that wasn't working in the CocoaPod either. If I use the Preprocessor macro in the main Extension class, it works correctly, but when used in the Cocoapod that is called by the extension, it does not.

I am currently using XCode 13.3 on an M1 Macbook Pro. I do remember that a few weeks ago XCode updated itself and stopped building so that paths needed to be changed, but I think that is all straightened out and everything else seems to be working normally.

  • I forgot to mention, in the Podfile file, I have the following:

    post_install do |installer| installer.pods_project.targets.each do |target| target.build_configurations.each do |config| ... if target.name == "Notifications" config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'TARGET_IS_NOTIFICATION=1'] end ...

  • I should probably also mention that in this case it is a forked version of a Cocoapod, so I can change things as needed.

Add a Comment

Replies

Looks like i have a workaround for now using the following at the beginning of the methods that shouldn't be run in extensions:

if ([[[NSBundle mainBundle] bundlePath] hasSuffix:@".appex"]) { return; // this is an app extension }