我正在尝试使用 Xcode 6 提供的用于创建 Cocoa Touch 框架的新模板构建 iOS 框架 (Test.framework)。 该框架在 Podfile 中指定了不同的依赖项(如 AFNetworking 或 FacebookSDK)。我不想将依赖项包含在框架中,我只想链接到它们。
问题是当我构建框架时,链接并包含了 libPods.a。 问:如何链接 libPods.a 库,但不将其包含在框架中?
更多详情:
我读过有关弱链接的内容: https://developer.apple.com/library/ios/documentation/MacOSX/Conceptual/BPFrameworks/Concepts/WeakLinking.html 但是我没有太多使用项目设置的经验,我不是专业人士。
我尝试将 libPods.a 标记为“可选”但没有任何改变,依赖项仍然包含在内。
我尝试从 Build Phases 的“Link Binary With Libraries”部分删除 libPods.a,但出现此错误(清理项目文件夹并重新构建后):
ld: library not found for -lPods-MyFramework-AFNetworking
我尝试从“其他链接器标志”中删除所有标志,但它给了我 undefined symbol :
Undefined symbols for architecture armv7:
"_OBJC_CLASS_$_FBSession", referenced from:
objc-class-ref in TestClass.o
ld: symbol(s) not found for architecture armv7
这是一个包含库的干净项目的源代码: https://www.dropbox.com/sh/0ymuzw6kiagz02j/AABzyHiZVaQQvBEnjBgRBq3ua?dl=0
最佳答案
下面的 post_install 代码位于 Podfile 的底部。它允许您指定您想要的目标以及弱链接的框架。我们能够利用它在我们的动态框架目标中弱链接一个框架,但在编译我们的核心应用程序时继续正确链接它。
targets_to_weaklink=['Target1']
frameworks_to_weaklink=['Framework1']
post_install do |installer|
targets_to_weaklink.map!{|t| t="Pods-#{t}"}
installer.pods_project.targets.each do |target|
next unless targets_to_weaklink.include?(target.name)
target.build_configurations.each do |config|
base_config_reference = config.base_configuration_reference
unless base_config_reference.nil?
xcconfig_path = base_config_reference.real_path
xcconfig = File.read(xcconfig_path)
frameworks_to_weaklink.each do |framework|
xcconfig = xcconfig.gsub(/-framework "#{framework}"/, "-weak_framework \"#{framework}\"")
end
File.open(xcconfig_path, "w") { |file| file << xcconfig }
end
end
end
end
关于ios - 如何使用弱链接 CocoaPods 库构建 iOS 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26791207/