ios – 用Swift构建Cocoapod并依赖Objective-C框架

前端之家收集整理的这篇文章主要介绍了ios – 用Swift构建Cocoapod并依赖Objective-C框架前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我知道这个主题在这方面已经有几个问题,但很少有人接受答案,我不认为我发现了和我一样的问题.

我正在构建一个Swift pod,在我的代码中,我依靠的是谷歌地图iOS SDK,它被捆绑为一个.framework文件.该项目在Xcode中建立起来,但是我有麻烦将lib发布到Cocoapods.

我设法有一个Podspec文件,几乎使用pod lib lint命令进行验证.但是,现在我已将Google-Maps-iOS-SDK pod作为依赖关系添加到Podspec文件中,它会失败,并显示以下消息:

$pod lib lint

[!] The ‘Pods’ target has transitive dependencies that include static
binaries:
(/private/var/folders/n2/qyjfpk6n7zz_mngtwswlmsy00000gn/T/CocoaPods/Lint/Pods/Google-Maps-iOS-SDK/GoogleMaps.framework)

$

这是预期的吗为什么我无法在我自己的基于Swift的pod中添加Google Maps iOS SDK作为pod参考?

这是Podspec:

  1. Pod::Spec.new do |s|
  2. s.name = '(name)'
  3. s.version = '1.0.0'
  4. s.summary = '(summary)'
  5. s.platforms = { :ios => '8.0',:osx => '10.10' }
  6. s.ios.deployment_target = '8.0'
  7. s.osx.deployment_target = '10.10'
  8. s.license = { :type => 'BSD',:file => 'LICENSE' }
  9. s.source_files = 'Sources/*.{h,swift}','*.framework'
  10. s.source = { :git => "https://github.com/(Github repo).git",:tag => "1.0.0" }
  11. s.requires_arc = true
  12. s.frameworks = "Foundation","CoreLocation"
  13. s.author = { 'Romain L' => '(email)' }
  14. s.dependency 'Google-Maps-iOS-SDK'
  15. end

如果我没有将Google Maps iOS SDK作为依赖关系,那么pod lib lint在桥接头中失败,并且抱怨找不到< GoogleMaps / GoogleMaps.h> (文件未找到).

我被卡住了,我不知道这是否是Cocoapods 0.36(仍然是Beta版)的错误,或者我做错了.

谢谢你的帮助!

解决方法

我终于在SO上找到了另一个线程,处理类似的问题: Linker errors in a Swift project with Google Maps for iOS added via CocoaPods.

似乎错误是由于Podspec文件(Google Maps iOS SDK端)和Cocoapods 0.36 Beta版的错误组合.

实际上可以通过使用@ fz.修订的Google地图的Podspec文件解决这些问题:https://stackoverflow.com/a/28471830/145997.另外一篇文章,也是非常感兴趣的了解在Podspec中的vendered_frameworks设置是如何工作的:http://codereaper.com/blog/2014/creating-a-pod-with-crashreporter/.

因此,要在Pod项目中正确导入Google Maps iOS SDK,请首先使用以下Podfile:

  1. source 'https://github.com/CocoaPods/Specs.git'
  2. platform :ios,'8.0'
  3. # altered version of Google's Podspec
  4. pod 'Google-Maps-iOS-SDK',:podspec => "https://raw.githubusercontent.com/Reflejo/GoogleMapsPodspec/master/Google-Maps-iOS-SDK.podspec.json"
  5. use_frameworks! # don't forget this!

我现在可以通过简单的导入Google地图,从Swift代码中引用Google Maps类.而且,为了分发Pod,我的最终Podspec现在类似于以下内容

  1. Pod::Spec.new do |s|
  2. s.name = 'MyPod'
  3. s.version = '1.0.0'
  4.  
  5. s.homepage = "https://github.com/..."
  6. s.summary = '(pod summary)'
  7. #s.screenshot = ""
  8.  
  9. s.author = { 'Romain L' => '(email)' }
  10. s.license = { :type => 'BSD',:file => 'LICENSE' }
  11. s.social_media_url = "https://twitter.com/_RomainL"
  12. s.platforms = { :ios => '8.0' }
  13. s.ios.deployment_target = '8.0'
  14.  
  15. s.source_files = 'MyCode/*.{h,swift}'
  16. s.module_name = 'MyPod'
  17. s.source = { :git => "https://github.com/....git",:tag => "1.0.0" }
  18. s.requires_arc = true
  19. s.libraries = "c++","icucore","z" # required for GoogleMaps.framework
  20. s.frameworks = "AVFoundation","CoreData","CoreLocation","CoreText","Foundation","GLKit","ImageIO","OpenGLES","QuartzCore","SystemConfiguration","GoogleMaps" # required for GoogleMaps.framework
  21. s.vendored_frameworks = "Dependencies/GoogleMaps.framework" # Put the Google-provided framework in that subfolder of your Pod project
  22. #s.dependency 'Google-Maps-iOS-SDK' # Careful! this will cause errors if enabled!
  23. end

我现在可以在Xcode中启动一个新的iOS应用程序,并使用以下Podfile链接到我自己的pod,本身引用了Google Maps iOS SDK:

  1. source 'https://github.com/CocoaPods/Specs.git'
  2. platform :ios,'8.0'
  3. pod 'MyPod'
  4. use_frameworks! # do not forget this!

毕竟不是那么容易,但可行希望谷歌即将推出适用于Swift开发的Podspec文件.

猜你在找的iOS相关文章