簡體   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