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

Building a Cocoapod with Swift and dependency on Objective-C framework

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

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

我设法得到了一个几乎可以使用 pod lib lint 命令验证的 Podspec 文件。但是,既然我已经将 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-based 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 会在桥接 Header 中失败,并抱怨找不到 <GoogleMaps/GoogleMaps.h>(未找到文件)。

我卡住了,我不知道这是 Cocoapods 0.36(仍处于 Beta 阶段)的错误还是我做错了什么。

感谢您的帮助!

我终于在 SO 上找到了另一个处理类似问题的帖子:

看来错误是由于错误的 Podspec 文件(在 Google 地图 iOS SDK 端)和 Cocoapods 0.36 Beta 中的错误共同造成的。

实际上可以通过使用@fz. 为 Google 地图修改的 Podspec 文件来解决这些问题:. Another article that also was of great interest to understand how the vendored_frameworks setting works in Podspec is: http://codereaper.com/blog/2014/creating-a-pod-with-crashreporter/.

因此,要在 Pod 项目中正确导入 Google Maps iOS SDK,首先使用以下 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!

我现在只需执行 import GoogleMaps 即可从我的 Swift 代码中引用 Google 地图 类。而且,为了分发 Pod,我的最终 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

我现在可以在 Xcode 中启动一个新的 iOS 应用程序并使用以下 Podfile 到 link 针对我自己的 pod,它本身引用 Google 地图 iOS SDK:

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

没那么容易,但毕竟可行!不过,希望 Google 能尽快为 Swift 开发修补其 Podspec 文件。