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

时间:2015-03-07 17:16:48

标签: ios swift cocoapods google-maps-sdk-ios lint

我知道关于这个主题已经有一些关于SO的问题,但是很少有人接受了答案,我认为我没有找到与我相同的问题。

我正在构建一个Swift pod,在我的代码中,我依赖于Google Maps iOS SDK,它捆绑为.framework文件。该项目在Xcode中构建正常,但是我很难将lib发布到Cocoapods。

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

  

$ pod lib lint

     

[!]'Pods'目标具有包含静态的传递依赖性   二进制文件:   (/private/var/folders/n2/qyjfpk6n7zz_mngtwswlmsy00000gn/T/CocoaPods/Lint/Pods/Google-Maps-iOS-SDK/GoogleMaps.framework)

     

$

这是预期的吗?为什么我不能在我自己的基于Swift的pod中添加Google Maps iOS SDK作为pod引用?

这是Podspec

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

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

我被卡住了,我不知道这是Cocoapods 0.36(仍处于测试版)中的错误,还是我做错了。

感谢您的帮助!

1 个答案:

答案 0 :(得分:12)

我终于在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。另一篇对vendored_frameworks Podspec Podfile source 'https://github.com/CocoaPods/Specs.git' platform :ios, '8.0' # altered version of Google's Podspec pod 'Google-Maps-iOS-SDK', :podspec => "https://raw.githubusercontent.com/Reflejo/GoogleMapsPodspec/master/Google-Maps-iOS-SDK.podspec.json" use_frameworks! # don't forget this! 设置工作原理非常感兴趣的文章是:http://codereaper.com/blog/2014/creating-a-pod-with-crashreporter/

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

Podspec

我现在只需执行Pod::Spec.new do |s| s.name = 'MyPod' s.version = '1.0.0' s.homepage = "https://github.com/..." s.summary = '(pod summary)' #s.screenshot = "" s.author = { 'Romain L' => '(email)' } s.license = { :type => 'BSD', :file => 'LICENSE' } s.social_media_url = "https://twitter.com/_RomainL" s.platforms = { :ios => '8.0' } s.ios.deployment_target = '8.0' s.source_files = 'MyCode/*.{h,swift}' s.module_name = 'MyPod' s.source = { :git => "https://github.com/....git", :tag => "1.0.0" } s.requires_arc = true s.libraries = "c++", "icucore", "z" # required for GoogleMaps.framework s.frameworks = "AVFoundation", "CoreData", "CoreLocation", "CoreText", "Foundation", "GLKit", "ImageIO", "OpenGLES", "QuartzCore", "SystemConfiguration", "GoogleMaps" # required for GoogleMaps.framework s.vendored_frameworks = "Dependencies/GoogleMaps.framework" # Put the Google-provided framework in that subfolder of your Pod project #s.dependency 'Google-Maps-iOS-SDK' # Careful! this will cause errors if enabled! end 即可从我的Swift代码中引用Google Maps类。而且,为了分发Pod,我的最终Podfile现在类似于以下内容:

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

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

{{1}}

不是那么容易,但毕竟是可行的!希望Google尽快修补其{{1}}文件以获取Swift开发。