How to include AFNetworking as a Framework for using in an iOS App and Extension via CocoaPods

前端 未结 3 1488
醉梦人生
醉梦人生 2020-12-13 16:18

NB: This is related to this question on project structure, but I have decided to a vastly the use-case to better abstract the problem.

Problem

How d

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-13 16:35

    For newcomers to this post, things have changed a little.

    I spent a fair amount of time hitting my head against a wall, hopefully this will spare some of you from that same fate.

    Cocoapods changed so that it now only generates one library per pod, so in order to properly set the AF_APP_EXTENSIONS macro, it actually needs to be set in the AFNetworking target, not your extension's target.

    For example (with some pretty log statements):

    post_install do |installer_representation|
        installer_representation.pods_project.targets.each do |target|
            puts "=== #{target.name}"
            if target.name == "AFNetworking"
                puts "Setting AFNetworking Macro AF_APP_EXTENSIONS so that it doesn't use UIApplication in extension."
                target.build_configurations.each do |config|
                    puts "Setting AF_APP_EXTENSIONS macro in config: #{config}"
                    config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'AF_APP_EXTENSIONS=1']
                end
            end
        end
    end
    

    Also worth noting the pods_project in installer_representation.pods_project.targets.each do |target|

    Cocoapods has deprecated project and it has been changed to pods_project

    The one somewhat downside to this is that AFNetworking won't use any UIApplication APIs in the container app either, but that was a non-issue in my project.

    Hope this helps.

提交回复
热议问题