使用Swift构建Cocoapod,并依赖Objective-C框架 [英] Building a Cocoapod with Swift and dependency on Objective-C framework

查看:579
本文介绍了使用Swift构建Cocoapod,并依赖Objective-C框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我设法拥有一个Podspec文件,该文件几乎可以使用pod lib lint命令进行验证.但是,既然我已经将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上找到了另一个线程来处理类似的问题: https://stackoverflow. com/a/28471830/145997 .另一篇对理解Podspecvendored_frameworks设置的工作方式也很感兴趣的文章是:

我现在只需执行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,该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文件打补丁.

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.

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. The project builds OK in Xcode, however I have troubles publishing the lib to Cocoapods.

I managed to have a Podspec file that almost validates using the pod lib lint command. 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:

$ 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)

$

Is this expected? Why can't I add the Google Maps iOS SDK as a pod reference in my own Swift-based pod?

Here's the 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).

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.

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.

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.

It's actually possible to workaround the issues by using @fz.'s revised Podspec file for Google Maps: 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/.

So, to correctly import the Google Maps iOS SDK in a Pod project, first use the following 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. And, to distribute the Pod, my final Podspec now resembles the following:

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:

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.

这篇关于使用Swift构建Cocoapod,并依赖Objective-C框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆