16

我正在尝试使用 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 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

4

2 回答 2

4

下面的 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
于 2020-11-29T07:46:59.790 回答
1

I can not comment yet so I will answer here. I think you can not do that. Cocoapods create a static library not a dynamic one so all the code should be there when you compile and is embedded in your framework.

Cocoapods documentation, go to "What is happening behind the scenes?" for a detail explanation

If you want to put together some basic configuration for some of your usual pods probably the best way to go is making a custom cocoapod with the other ones as dependencies. That way you can rely in cocoapods to manage versions and have the code updated per your preferences. I am using a similar approach myself and I am quite happy with the results.

于 2015-01-26T17:29:24.957 回答