【问题标题】:Building a Cocoapod with Swift and dependency on Objective-C framework使用 Swift 构建 Cocoapod 并依赖于 Objective-C 框架
【发布时间】:2015-05-09 04:07:08
【问题描述】:

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

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

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

$ pod lib lint

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

$

这是预期的吗?为什么我不能将 Google Maps iOS SDK 作为 pod 引用添加到我自己的基于 Swift 的 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(仍处于测试阶段)的错误还是我做错了什么。

感谢您的帮助!

【问题讨论】:

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


    【解决方案1】:

    我终于在 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 中的工作原理非常感兴趣的文章是: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 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 文件。

    【讨论】:

    • 这是完美的。它通过创建自定义样式的 podspec 帮助我让 Google-Mobile-Ads-SDK 在 0.38.2 上运行。谢谢,@Romain!
    • 谢谢!这是非常有帮助的答案。仅供参考:目前,GoogleMaps 1.12.3 需要 Accelerate.framework
    猜你喜欢
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-03
    • 2015-05-21
    • 2018-12-05
    • 2016-11-06
    • 2020-06-10
    相关资源
    最近更新 更多