Cocoapods私有庫及庫之間沖突解決

私有庫的使用場景


場景一、在iOS開發的過程中,我們會因開發需要而使用Cocoapods引入各種三方庫,但有時為了更切合項目需求,不得不去修改三方庫的源碼。一旦pod update升級了三方庫,原來修改的代碼就沖掉了,這就不得不使用git回滾修改的區塊或者再改一遍,這就造成升級困難。尤其是在團隊里開發的過程中,如果其它隊員不知道你改了三方源碼,這一升級直接導致你的修改代碼丟失而出現bug。

場景二、有一些成熟的三方庫,不支持pods導入,這個比較少見。

場景三、項目里成熟的功能模塊可以從項目里剝離,通過pods導入。


對于這幾種場景,我們可以通過以下幾個方法來解決,下面來詳細介紹下:

一、pods導入本地庫

1.創建Podfile

touch Podfile 或 vim Podfile(可直接編輯)

填寫完成輸入 :wq 保存并退出

2.創建MyTestKit文件夾,并創建podspec文件

pod spec create MyTestKit

文件目錄結構如下:

3.把需要pods引入的文件放到MyTestKit目錄下

4.修改podspec文件

這是最重要的一步,需要了解podspec文件的寫法,各種字段的涵義。可刪除不必要的注釋(更多字段請參考http://guides.cocoapods.org/syntax/podspec.html)

《如何編寫一個 CocoaPods 的 spec 文件》

修改后的podspec文件

#

#? Be sure to run `pod spec lint MyTestKit.podspec' to ensure this is a

#? valid spec and to remove all comments including this before submitting the spec.

#

#? To learn more about Podspec attributes see http://docs.cocoapods.org/specification.html

#? To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/

#

Pod::Spec.new do |s|

# ―――? Spec Metadata? ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

#

#? These will help people to find your library, and whilst it

#? can feel like a chore to fill in it's definitely to your advantage. The

#? summary should be tweet-length, and the description more in depth.

#

s.name? ? ? ? = "MyTestKit"

s.version? ? ? = "0.0.1"

s.summary? ? ? = "MyTestKit demo."

# This description is used to generate tags and improve search results.

#? * Think: What does it do? Why did you write it? What is the focus?

#? * Try to keep it short, snappy and to the point.

#? * Write the description between the DESC delimiters below.

#? * Finally, don't worry about the indent, CocoaPods strips it!

s.description? = "description"

s.homepage? ? = "https://github.com/zhfeng20108"

# s.screenshots? = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif"

# ―――? Spec License? ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

#

#? Licensing your code is important. See http://choosealicense.com for more info.

#? CocoaPods will detect a license file if there is a named LICENSE*

#? Popular ones are 'MIT', 'BSD' and 'Apache License, Version 2.0'.

#

s.license? ? ? = "MIT"

# s.license? ? ? = { :type => "MIT", :file => "FILE_LICENSE" }

# ――― Author Metadata? ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

#

#? Specify the authors of the library, with email addresses. Email addresses

#? of the authors are extracted from the SCM log. E.g. $ git log. CocoaPods also

#? accepts just a name if you'd rather not provide an email address.

#

#? Specify a social_media_url where others can refer to, for example a twitter

#? profile URL.

#

s.author? ? ? ? ? ? = { "feng.zhang" => "hhzhangfeng2008@163.com" }

# Or just: s.author? ? = "feng.zhang"

# s.authors? ? ? ? ? ? = { "feng.zhang" => "hhzhangfeng2008@163.com" }

# s.social_media_url? = "http://twitter.com/feng.zhang"

# ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

#

#? If this Pod runs only on iOS or OS X, then specify the platform and

#? the deployment target. You can optionally include the target after the platform.

#

# s.platform? ? = :ios

s.platform? ? = :ios, "8.0"

#? When using multiple platforms

# s.ios.deployment_target = "5.0"

# s.osx.deployment_target = "10.7"

# s.watchos.deployment_target = "2.0"

# s.tvos.deployment_target = "9.0"

# ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

#

#? Specify the location from where the source should be retrieved.

#? Supports git, hg, bzr, svn and HTTP.

#

s.source? ? ? = { :git => "", :tag => "#{s.version}" }

# ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

#

#? CocoaPods is smart about how it includes source code. For source files

#? giving a folder will include any swift, h, m, mm, c & cpp files.

#? For header files it will include any header in the folder.

#? Not including the public_header_files will make all headers public.

#

# s.source_files? = "Classes", "Classes/**/*.{h,m}"

# s.exclude_files = "Classes/Exclude"

# s.public_header_files = "Classes/**/*.h"

# ――― Resources ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

#

#? A list of resources included with the Pod. These are copied into the

#? target bundle with a build phase script. Anything else will be cleaned.

#? You can preserve files from being cleaned, please don't preserve

#? non-essential files like tests, examples and documentation.

#

# s.resource? = "icon.png"

# s.resources = "Resources/*.png"

# s.preserve_paths = "FilesToSave", "MoreFilesToSave"

# ――― Project Linking ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

#

#? Link your library with frameworks, or libraries. Libraries do not include

#? the lib prefix of their name.

#

s.vendored_frameworks = 'HeheTest.framework'

# s.framework? = "SomeFramework"

# s.frameworks = "SomeFramework", "AnotherFramework"

s.vendored_libraries = 'libHeheTestStatic.a'

# s.library? = "iconv"

# s.libraries = "iconv", "xml2"

# ――― Project Settings ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

#

#? If your library depends on compiler flags you can set them in the xcconfig hash

#? where they will only apply to your library. If you depend on other Podspecs

#? you can include multiple dependencies to ensure it works.

s.requires_arc = true

# s.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }

# s.dependency "JSONKit", "~> 1.4"

s.subspec "HeaderFiles" do |ss|

ss.public_header_files = "include/**/*.h"

ss.source_files = "include/**/*.h"

ss.requires_arc = true

end

end

5.執行pod install命令

按照提示把podspec文件修改正確,直到pod install執行成功

最后工程結構如下圖所示:

二、pods導入網絡上的庫并解決沖突問題

1.導入GitHub上的三方庫

這個是我們用的最多的情況,不再贅述。

2.導入私有倉庫里的庫

隨著項目開發的迭代,工程越來越大,很有必要把一些自定義控件、功能模塊從工程里剝離出來,移植給更多的項目使用。要導入庫,需要具備兩個條件:(1)源碼倉庫(2)對應的podspec.json文件的倉庫

對于源碼倉庫和podspec.json文件的倉庫,可以是github上的,也可以是自己公司的私有倉庫。解決多個三方庫沖突的方法就是在這里做文章。咱們就先來講講如何導入私有庫。如下是一個示例:

platform :ios, '8.0'

source 'https://github.com/zhfeng20108/podsecRepo.git'

source 'https://github.com/CocoaPods/Specs.git'

target 'TestDemo' do

pod 'MyTestKit', :path => 'MyTestKit'

pod 'gRPC-Core'

end


可以看出 gRPC-Core 下載的版本是1.3.0, 而GitHub 上的最新版本是1.6.0

從中可以看出,會優先在'https://github.com/zhfeng20108/podsecRepo.git'里查找podspec.json文件。這樣我們就可以對gRpc-Core.podspec.json文件做任何更改,上面的例子中,是對依賴做了修改,由BoringSSL改成了OpenSSL。這里的修改是為了避免BoringSSL和OpenSSL同時引入pods造成方法重命名的沖突。

3.沖突解決

(1)上面這個案例只是修改了dependencies字段的值,當然了我們可以根據需要去修改platforms,source,source_files,vendored_frameworks,vendored_libraries等等字段的值來避免與其它三方庫的沖突。

(2)碰上源代碼方法名重名的情況,這時就需要修改三方的源代碼。

方案一:把修改好的源代碼存到一個新的倉庫里,比如公司的一個倉庫里,同時把修改好的podspec文件(這時主要修改的是source字段里的值,git地址指向公司的倉庫)存在公司的另一個倉庫里。

方案二:fork源代碼到自己的github帳號下,然后修改代碼,引入時采用 pod 'RSKImageCropper',:git => 'https://github.com/zhfeng20108/RSKImageCropper',即可指向修改后的代碼庫。復雜的可能就需要在自己帳號下再建一個庫存放podspec.json文件,以便精確控制代碼。

以上兩種方案,無論哪種,都會遇上三方源碼升級問題,對于方案一,我們只能把最新代碼copy過去,檢查下原來修改的代碼現在是否需要調整。對于方案二,可通過方法fork后代碼和源代碼同步來解決。

用CocoaPods做iOS程序的依賴管理

git安裝和使用

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,825評論 6 546
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,814評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,980評論 0 384
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 64,064評論 1 319
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,779評論 6 414
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 56,109評論 1 330
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,099評論 3 450
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,287評論 0 291
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,799評論 1 338
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,515評論 3 361
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,750評論 1 375
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,221評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,933評論 3 351
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,327評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,667評論 1 296
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,492評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,703評論 2 380

推薦閱讀更多精彩內容