繁体   English   中英

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

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

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

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

我设法拥有一个几乎可以使用pod lib lint命令进行验证的Podspec文件。 但是,现在我已经在Podspec文件中添加了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(仍处于 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。 另一篇对理解vendored_frameworks设置如何在vendored_frameworks中工作也很感兴趣的文章是: http : Podspec

因此,要在 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 Maps 类。 而且,为了分发 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链接到我自己的 pod,它本身引用了 Google Maps iOS SDK:

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

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

暂无
暂无

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

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