Search code examples
iosafnetworkingcocoapods

Cocoapod 0.38.0 and AFNetworking 2.5 AF_APP_EXTENSIONS compilation error


My project has 9 targets :

- Prod
- Prod_app_extension_1
- Prod_app_extension_2
- Beta 
- Beta_app_extension_1
- Beta_app_extension_2
- Dev
- Dev_app_extension_2
- Dev_app_extension_2

I'm using 0.38.2 cocoapod version and 2.5.4 AFNetworking.

I'm trying to use AFNetworking with cocoapod but I get the AF_APP_EXTENSIONS error while compiling. After searching for the solution on the web, I understand the problem and found that defining the 'preprocessor macros' AF_APP_EXTENSIONS can fix the problem.

But here is the struggle : By default, AF_APP_EXTENSIONS is correctly added into my 6 app_extensions. In the other hand, when I navigate through my Pods target, each Pods are separated :

- NSDate+TimeAgo 
- AFNetworking
- iRate
- AppUtils
- Prod
- Prod_app_extension_1
- Prod_app_extension_2
- Beta 
- Beta_app_extension_1
- Beta_app_extension_2
- Dev
- Dev_app_extension_2
- Dev_app_extension_2

In another project I made, all pods are generated this way :

- Prod
- Pods-Prod-NSDate+TimeAgo
- Pods-Prod-AFNetworking
- Pods-Prod-iRate
- Pods-Prod-AppUtils

- Prod_app_extension_1
- Pods-Prod_app_extension_1-NSDate+TimeAgo
- Pods-Prod_app_extension_1-AFNetworking
- Pods-Prod_app_extension_1-iRate

- Prod_app_extension_2
- Pods-Prod_app_extension_2-NSDate+TimeAgo
- Pods-Prod_app_extension_2-AFNetworking
- Pods-Prod_app_extension_2-iRate

- Beta
- Pods-Beta-NSDate+TimeAgo
- Pods-Beta-AFNetworking
- Pods-Beta-iRate
- Pods-Beta-AppUtils 

- Beta_app_extension_1
- Pods-Beta_app_extension_1-NSDate+TimeAgo
- Pods-Beta_app_extension_1-AFNetworking
- Pods-Beta_app_extension_1-iRate

- Beta_app_extension_2
- Pods-Beta_app_extension_2-NSDate+TimeAgo
- Pods-Beta_app_extension_2-AFNetworking
- Pods-Beta_app_extension_2-iRate

- Dev
- Pods-Dev-NSDate+TimeAgo
- Pods-Dev-AFNetworking
- Pods-Dev-iRate
- Pods-Dev-AppUtils 

- Dev_app_extension_1
- Pods-Dev_app_extension_1-NSDate+TimeAgo
- Pods-Dev_app_extension_1-AFNetworking
- Pods-Dev_app_extension_1-iRate

- Dev_app_extension_2
- Pods-Dev_app_extension_2-NSDate+TimeAgo
- Pods-Dev_app_extension_2-AFNetworking
- Pods-Dev_app_extension_2-iRate

I think this is why my 'preprocessor macros' AF_APP_EXTENSIONS isn't define into the 'AFNetworking' Pods target.

Here is my Podfile :

platform :ios, '7.0'

xcodeproj 'myProj.xcodeproj'


def generic_pods
    pod 'NSDate+TimeAgo'
    pod 'AFNetworking', '~> 2.0'
end

def app_pods
    pod 'iRate'
    pod 'AppUtils',
end



target "Prod" do
    generic_pods
    app_pods
end

target "Prod_app_extension_1" do
    generic_pods
end

target "Prod_app_extension_2" do
    generic_pods
end

target "Beta" do
    generic_pods
    app_pods
end

target "Beta_app_extension_1" do
    generic_pods
end

target "Beta_app_extension_2" do
    generic_pods
end

target "Dev" do
    generic_pods
    app_pods
end

target "Dev_app_extension_1" do
    generic_pods
end

target "Dev_app_extension_2" do
    generic_pods
end

I don't know what the problem is, and it's driving me crazy.


Solution

  • Since 0.38.0 cocoapod version, pod target are De-duplicate. It means, instead of bulding an AFNetworking for each of your project target ('Pods-MyApp-AFNetworking', 'Pods-MyExtension-AFNetworking', ...) only one AFNetworking pod target is generated ('Pods-AFNetworking').

    AFNetworking require 'AF_APP_EXTENSIONS' flag into the GCC_PREPROCESSOR_DEFINITIONS to compile when added to app-extensions.

    Until the 0.37.2 cocoapod version, it was possible to add the flag from your project Podfile by adding a post-install routine :

    post_install do |installer_representation|
        installer_representation.pods_project.targets.each do |target|
            if ar.include? target.name
                target.build_configurations.each do |config|
                    config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'AF_APP_EXTENSIONS=1']
                end
            end
        end
    end
    

    Unfortunatly, with the De-duplicate cocoapod feature, it's no longer possible to add the AF_APP_EXTENSIONS flag to the app extention pod targets separately.

    The only way I found is to compile is to create a file : ~/.cocoapods/config.yaml (not present by default when you use cocoapod, you can create it with the terminal) with this content :

    deduplicate_targets: false 
    

    This way, pods aren't not de-duplicated and you can post-install add the AF_APP_EXTENSIONS into your app extensions GCC_PREPROCESSOR_DEFINITIONS.

    I just hope they will found a good way to prevent this problem.

    references :

    https://github.com/CocoaPods/CocoaPods/blob/master/CHANGELOG.md#highlighted-enhancement-that-needs-testing => cocoapod change log

    https://github.com/CocoaPods/CocoaPods/issues/3794 => discussed issue