【问题标题】:using 'vendored_frameworks' and 'source_files' for cocoapod using 'use_frameworks!'使用 'vendored_frameworks' 和 'source_files' 用于 cocoapod 使用 'use_frameworks!'
【发布时间】:2016-12-14 09:37:28
【问题描述】:

我正在构建一个 cocoapod,它基本上包含一个框架(私有源代码)和一个依赖于该框架的视图(开源),所有这些都是用 Objective-C 制作的。

在 podspec 我有以下几行:

  • spec.vendored_frameworks = 'MyPod/Framework/MyFramework.framework'
  • spec.source_files = ['MyPod/UI/Views/MyView.{h,m}']

当使用use_frameworks! 语法时,我不能#import MyFramework

我只是不明白发生了什么。

此外,当我删除spec.source_files 行时,我可以#import MyFramework 并且它工作得很好,但是我当然不能使用MyView

我做错了什么?

【问题讨论】:

    标签: ios swift frameworks cocoapods


    【解决方案1】:

    对于今天遇到此问题的任何人:这是 CocoaPods 中的一个已知问题,Github herehere 上讨论了该问题。

    建议的解决方法是将您的 podspec 一分为二:一个 podspec 仅包含您的 vendored_frameworks,另一个 podspec 仅包含使用框架的 source_files

    Github 上的用户 crsantos helpfully posted 是此解决方法的一个示例,我在下面复制了两个单独的 podspec。

    供应商框架 podspec:

    # Regarding https://github.com/CocoaPods/CocoaPods/issues/6409
    Pod::Spec.new do |s|
      s.name             = 'SampleDynamicLibPod'
      s.version          = '0.0.1'
      s.summary          = 'SampleDynamicLibPod. Cool Story bro!'
    
      s.description      = <<-DESC
    Blah Blah Blah Blah Blah description
                           DESC
    
      s.homepage         = 'https://github.com/crsantos/SameRepoForAllPodSpecsAndSource'
      s.license          = { :type => 'MIT', :file => 'LICENSE' }
      s.author           = { 'Carlos Santos' => 'mail@example.com' }
      s.source           = { :git => 'https://github.com/crsantos/SameRepoForAllPodSpecsAndSource.git', :tag => "#{s.name}-#{s.version.to_s}" }
      # this is the way of tagging diferent podspecs on the same repo
      # Dont't forget to tag your repo with `SampleDynamicLibPod-0.0.1` for this specific spec
    
      s.module_name      = 'SampleDynamicLibPod'
    
      s.ios.deployment_target = '9.0'
      s.platform = :ios, '9.0'
      s.pod_target_xcconfig = { 'SWIFT_VERSION' => '3' }
    
      s.vendored_frameworks = 'SampleDynamicLibPod/Frameworks/SampleDynamicLib.framework'
    end
    

    源文件 podspec.注意对供应商框架 podspec 的依赖。

    # Regarding https://github.com/CocoaPods/CocoaPods/issues/6409
    
    Pod::Spec.new do |s|
      s.name             = 'WrapperAroundSampleDynamicLibPod'
      s.version          = '0.0.2' # just a different number to avoid confusion with the other podspec
      s.summary          = 'WrapperAroundSampleDynamicLibPod. Cool Story bro!'
    
      s.description      = <<-DESC
    Wrapper Around Sample Dynamic Lib Pod Blah Blah Blah Blah Blah Blah Blah Blah
                           DESC
    
      s.homepage         = 'https://github.com/crsantos/SameRepoForAllPodSpecsAndSource'
      s.license          = { :type => 'MIT', :file => 'LICENSE' }
      s.author           = { 'Carlos Santos' => 'mail@example.com' }
      s.source           = { :git => 'https://github.com/crsantos/SameRepoForAllPodSpecsAndSource.git', :tag => "#{s.name}-#{s.version.to_s}" }
    
      # Dont't forget to tag your repo with `WrapperAroundSampleDynamicLibPod-0.0.2` for this specific spec
    
      s.module_name      = 'WrapperAroundSampleDynamicLibPod'
    
      s.ios.deployment_target = '9.0'
      s.platform = :ios, '9.0'
      s.pod_target_xcconfig = { 'SWIFT_VERSION' => '3' }
    
      s.source_files = 'WrapperAroundSampleDynamicLibPod/Classes/**/*'
    
      s.dependency 'CocoaLumberjack/Swift'
      s.dependency 'SampleDynamicLibPod', '0.0.1' # you can achieve this with "#{s.name}-#{s.version.to_s}" from the 
    end
    

    【讨论】:

      【解决方案2】:

      如果您使用use_frameworks!,您的 pod 本身将成为一个框架。因此你应该#import MyPod 而不是#import MyFramework 然后使用MyView

      如果需要,也可以查看public_header_files

      【讨论】:

      • 是的,但是,我如何访问 MyFrameworks 类?
      • 就像你说的,MyFramework 依赖是私有的。您想如何从 pod 外部访问它?你说你只想公开MyView 是开放的,而这个依赖于来自MyFramework 的公共标头。 (顺便检查一下你的框架头文件,确保你允许访问它)。
      • 只有MyFramework 的来源是私有的。此外,在不使用use_frameworks! 时,我已经可以毫无问题地访问它们
      【解决方案3】:

      由于项目的 pod 现在是一个框架,您可以尝试使用 @importMyFrameworkimporting it as a module

      但是,如果这不起作用,请尝试备份您的项目,然后运行 ​​pod deintegrate &amp;&amp; pod install。另外,this question 非常相似,它的一些 cmets 和答案可能会有所帮助。

      【讨论】:

        【解决方案4】:

        虽然令人震惊的答案在技术上是正确的,但有一种方法可以将 source_filesvendored_frameworks 结合起来。解决方案是也使用preserve_paths 指向供应商框架的位置。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-05-03
          • 2018-11-25
          • 1970-01-01
          • 2017-05-03
          • 1970-01-01
          相关资源
          最近更新 更多