如何将AFNetworking包含为通过CocoaPods在iOS应用程序和扩展中使用的框架

rjs*_*ing 13 ios afnetworking cocoapods xcode6 ios8-extension

注意:这与项目结构的这个问题有关,但我已经决定使用大量的用例来更好地抽象问题.

问题

如何通过在我的iOS应用程序和随附的iOS扩展(,)中包含afnetworking?

问题

  • 要在扩展中使用,需要构建AFNetworking #define AF_APP_EXTENSIONS,这是否意味着我需要2个版本的AFNetworking?一个用于扩展,一个用于应用程序?

  • 如何设置Podfile以构建框架并将其复制到正确的位置?文档use_frameworks!有点薄.

jus*_*unt 13

更新:

正如Rhythmic Fistman所说,在进行新的pod安装时,原始答案的方法会被覆盖.

Aelam在这个Github 问题中提供了以下方法:

将其添加到您的podfile中.记得替换目标名称

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
Run Code Online (Sandbox Code Playgroud)

过时的答案:

1)确保pod 'AFNetworking'podfile中包含两个目标(您的容器应用程序和扩展程序).

我案例中的例子:

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

target 'ExtensionTarget', :exclusive => true do
   pod 'AFNetworking'
end
Run Code Online (Sandbox Code Playgroud)

2)在XCode中,单击层次结构视图上的Pods以显示其构建选项.然后在构建选项中,选择要在下拉列表中查看构建选项的目标.有选择Pods-{Extension Target Name}-AFNetworking(它应该是由pod安装自动创建的.然后选择Build Settings.然后在Apple LLVM 6.0 - 语言下,验证Prefix标题是否有文件名.我的案例中的文件名是Target Support Files/Pods-{Extension Target Name}-AFNetworking/Pods-{Extension Target Name}-AFNetworking-prefix.pch.如果它没有这样的文件名或类似然后添加它.

3)转到那个在那里命名的前缀头文件或者你在那里添加的那个.它几乎是空的,然后在最后添加以下行:

#define AF_APP_EXTENSIONS
Run Code Online (Sandbox Code Playgroud)

这应该允许您的容器应用程序指向正常构建的AFNetworking版本,并将扩展应用程序指向另一个使用标志集构建的应用程序.所以只有一个版本的库,但是以两种不同的方式构建,每个都在一个目标上.

  • 每次执行pod安装时都会重新生成该pch文件,这会清除您的修改. (4认同)

wyz*_*207 9

对于这篇文章的新人来说,情况有所改变.

我花了相当多的时间撞在墙上,希望这会使你们中的一些人免于同样的命运.

Cocoapods改变了,现在它每个pod只生成一个库,所以为了正确设置AF_APP_EXTENSIONS宏,它实际上需要在AFNetworking目标中设置,而不是你的扩展目标.

例如(有一些漂亮的日志语句):

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
Run Code Online (Sandbox Code Playgroud)

另外值得注意的pods_projectinstaller_representation.pods_project.targets.each do |target|

Cocoapods已被弃用project,现已更改为pods_project

这有点不利于AFNetworking也不会在容器应用程序中使用任何UIApplication API,但这在我的项目中不是问题.

希望这可以帮助.