swift 对cocopod使用“供应商框架”和“源文件”使用“use框架!”

0x6upsns  于 2023-01-19  发布在  Swift
关注(0)|答案(4)|浏览(162)

我正在构建一个cocopod,它基本上包含一个框架(私有源代码)和一个依赖于这个框架的视图(开放源代码),所有这些都是用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
我哪里做错了?

woobm2wo

woobm2wo1#

对于今天遇到这个问题的任何人:这是CocoaPods中的一个已知问题,Github herehere上提出了一些问题来讨论这个问题。
建议的解决方法是将您的podspec拆分为两个:一个podspec只包含您的vendored_frameworks,另一个podspec只包含使用该框架的source_files
Github上的用户crsantos已经发布了一个关于这个变通方案的例子,我在下面复制了两个单独的播客规范。
供应商框架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
lymnna71

lymnna712#

如果使用use_frameworks!,pod本身将成为一个框架,因此应该使用#import MyPod而不是#import MyFramework,然后使用MyView
如果需要,也可以查看public_header_files

r6l8ljro

r6l8ljro3#

由于项目的pod现在是一个框架,您可以使用**@import**MyFramework尝试importing it as a module
但是,如果这不起作用,那么尝试备份您的项目,然后运行pod deintegrate && pod install。另外,this question非常相似,它的一些注解和答案可能会有帮助。

hof1towb

hof1towb4#

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

相关问题