How to enable AFNetworking as a basis for use in iOS app and extension through CocoaPods

NB: This is related to this question about the structure of the project, but I decided to use a use case to improve the abstract problem.

Problem

How to enable afnetworking in my iOS application and the accompanying iOS extension ( ios8-extention ios8-today-widget or ios8-share-extension ) through CocoaPods?

Questions

  • For use in extensions, you need to create AFNetworking using #define AF_APP_EXTENSIONS , does this mean I need 2 versions of AFNetworking? One for expansion and one for application?

  • How to configure Podfile so that the frames are built and copied to the right places? Documentation on use_frameworks! a little thin.

+13
ios xcode6 cocoapods afnetworking ios8-extension
Mar 11 '15 at 13:13
source share
3 answers

Update:

As Rhythmic Fistman noted, the original response method is overwritten when a new pod installation is performed.

Aelam provided the following method in this Github issue :

Add this to your file. don't forget to replace the target name

 post_install do |installer_representation| installer_representation.project.targets.each do |target| if target.name == "Pods-YOU_EXTENSION_TARGET-AFNetworking" target.build_configurations.each do |config| config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'AF_APP_EXTENSIONS=1'] end end end end 

Deprecated answer:

1) Make sure the pod 'AFNetworking' enabled for both purposes (your container application and extension) in your subcode.

An example in my case:

 target 'ContainerAppTarget', :exclusive => true do pod "SDKThatInternallyUsesAFNetworking" end target 'ExtensionTarget', :exclusive => true do pod 'AFNetworking' end 

2) In Xcode, click on Pods in the hierarchy view to display its build options. Then, in the build options, select the target for which you are looking at the build options in the drop-down list. There, select Pods-{Extension Target Name}-AFNetworking (it should have been created automatically using pod install, then select “Build Settings.” Then in Apple LLVM 6.0 - Language, make sure the Prefix header has a file name. This is the file name in my case was Target Support Files/Pods-{Extension Target Name}-AFNetworking/Pods-{Extension Target Name}-AFNetworking-prefix.pch . 't have that file name or the like, and then add it.

3) Go to this prefix header file that was listed there or you added it. It will be almost empty, then add the following line to the end:

 #define AF_APP_EXTENSIONS 

This should allow your container application to point to the version of the embedded AFNetworking network, and your add-on application to another, built with the flag set. So there is only one version of the library, but it is built in two different ways, each for one of its goals.

+13
Mar 29 '15 at 22:06
source share
— -

For beginners in this post, things have changed a bit.

I spent a lot of time hitting my head against the wall, I hope this will save some of you from the same fate.

Cocoapods have changed so that now it only generates one library for each module, so to properly set the AF_APP_EXTENSIONS macro AF_APP_EXTENSIONS it really needs to be installed in the AFNetworking target, not your extension target.

For example (with some pretty magazine 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 is pods_project in installer_representation.pods_project.targets.each do |target|

Cocoapods deprecated project and was changed to pods_project

The only drawback to this is that AFNetworking will not use any UIApplication APIs in the container, but this was not a problem in my project.

Hope this helps.

+9
Aug 13 '15 at 15:26
source share

Upgrading your module to version 2.6 may solve this problem. See the requirements table at: https://github.com/AFNetworking/AFNetworking

0
Aug 31 '16 at 16:18
source share



All Articles