简体   繁体   English

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

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

I know there are already a few questions on this theme here on SO, but very few have accepted answers, and I don't think I have found the exact same problem as mine.我知道在 SO 上已经有一些关于这个主题的问题,但很少有人接受答案,而且我认为我没有发现与我完全相同的问题。

I'm building a Swift pod, and in my code I rely on the Google Maps iOS SDK, which is bundled as a .framework file.我正在构建一个 Swift pod,在我的代码中,我依赖 Google Maps iOS SDK,它被捆绑为一个.framework文件。 The project builds OK in Xcode, however I have troubles publishing the lib to Cocoapods.该项目在 Xcode 中构建正常,但是我无法将 lib 发布到 Cocoapods。

I managed to have a Podspec file that almost validates using the pod lib lint command.我设法拥有一个几乎可以使用pod lib lint命令进行验证的Podspec文件。 However, now that I've added the Google-Maps-iOS-SDK pod as a dependency in the Podspec file, it fails with the following message:但是,现在我已经在Podspec文件中添加了Google-Maps-iOS-SDK pod 作为依赖Podspec ,它失败并显示以下消息:

$ pod lib lint $ 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) [!] 'Pods' 目标具有传递依赖项,包括静态二进制文件:(/private/var/folders/n2/qyjfpk6n7zz_mngtwswlmsy00000gn/T/CocoaPods/Lint/Pods/Google-Maps-iOS-SDK/GoogleMaps.framework)

$ $

Is this expected?这是预期的吗? Why can't I add the Google Maps iOS SDK as a pod reference in my own Swift-based pod?为什么我不能在我自己的基于 Swift 的 pod 中添加 Google Maps iOS SDK 作为 pod 引用?

Here's the Podspec :这是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

If I don't include the Google Maps iOS SDK as a dependency, then pod lib lint fails in the Bridging Header, and complains it cannot find <GoogleMaps/GoogleMaps.h> (file not found).如果我不将 Google Maps iOS SDK 作为依赖项包含在内,则pod lib lint在桥接标头中失败,并抱怨它找不到<GoogleMaps/GoogleMaps.h> (未找到文件)。

I'm stuck, and I don't know if it's a bug from Cocoapods 0.36 (still in Beta) or if I'm doing something wrong.我被卡住了,我不知道这是 Cocoapods 0.36(仍处于 Beta 版)的错误还是我做错了什么。

Thanks for your help!谢谢你的帮助!

I finally found another thread on SO dealing with similar problems: Linker errors in a Swift project with Google Maps for iOS added via CocoaPods .我终于在 SO 上找到了另一个处理类似问题的线程: Linker errors in a Swift project with Google Maps for iOS added via CocoaPods

It appears that the errors were due to a combination of bad Podspec file (on the Google Maps iOS SDK side), and bugs in Cocoapods 0.36 Beta.错误似乎是由于错误的 Podspec 文件(在 Google Maps iOS SDK 端)和 Cocoapods 0.36 Beta 中的错误的组合造成的。

It's actually possible to workaround the issues by using @fz.'s revised Podspec file for Google Maps: https://stackoverflow.com/a/28471830/145997 .实际上可以通过使用@fz. 为 Google 地图修订的 Podspec 文件来解决这些问题: https ://stackoverflow.com/a/28471830/145997。 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/ .另一篇对理解vendored_frameworks设置如何在vendored_frameworks中工作也很感兴趣的文章是: http : Podspec

So, to correctly import the Google Maps iOS SDK in a Pod project, first use the following Podfile :因此,要在 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!

I'm now able to reference Google Maps classes from my Swift code simply by doing import GoogleMaps .我现在可以通过import GoogleMaps从我的 Swift 代码中引用 Google Maps 类。 And, to distribute the Pod, my final Podspec now resembles the following:而且,为了分发 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

I am now able to start a new iOS app in Xcode and use the following Podfile to link against my own pod, itself referencing the Google Maps iOS SDK:我现在可以在 Xcode 中启动一个新的 iOS 应用程序,并使用以下Podfile链接到我自己的 pod,它本身引用了 Google Maps iOS SDK:

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

Not that easy, but feasible after all!没那么容易,但毕竟可行! Hoping Google will soon patch its Podspec file for Swift developments, though.不过,希望 Google 很快会为 Swift 开发修补其Podspec文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM