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

18

我知道这个主题在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)

$

这是否是预期的行为?为什么我不能将Google Maps iOS SDK作为自己基于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(仍处于Beta版)的错误还是我做错了什么。
谢谢您的帮助!
1个回答

12

我最终在SO上找到了另一个与类似问题有关的帖子:使用CocoaPods添加Google Maps for iOS后,在Swift项目中出现链接器错误

看起来这些错误是由于Google Maps iOS SDK方面的不良Podspec文件和Cocoapods 0.36 Beta中的错误组合而成的。

实际上,可以通过使用@fz.的修订后的Google Maps Podspec文件来解决这些问题:https://dev59.com/Q4fca4cB1Zd3GeqPl67t#28471830。另一篇也非常有用的文章是:了解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来引用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能尽快修补其Podspec文件,以便于Swift开发。


1
这太棒了。通过创建自定义样式的podspec,它帮助我刚刚在0.38.2上运行Google-Mobile-Ads-SDK。谢谢,@Romain! - ArtSabintsev
1
谢谢!这个答案非常有帮助。顺便提一下,目前 GoogleMaps 1.12.3 需要 Accelerate.framework - Daishi Nakajima

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接