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

私有庫的使用場景


場景一饵较、在iOS開發(fā)的過程中,我們會(huì)因開發(fā)需要而使用Cocoapods引入各種三方庫,但有時(shí)為了更切合項(xiàng)目需求,不得不去修改三方庫的源碼洞慎。一旦pod update升級(jí)了三方庫,原來修改的代碼就沖掉了嘿棘,這就不得不使用git回滾修改的區(qū)塊或者再改一遍劲腿,這就造成升級(jí)困難。尤其是在團(tuán)隊(duì)里開發(fā)的過程中鸟妙,如果其它隊(duì)員不知道你改了三方源碼焦人,這一升級(jí)直接導(dǎo)致你的修改代碼丟失而出現(xiàn)bug。

場景二重父、有一些成熟的三方庫花椭,不支持pods導(dǎo)入,這個(gè)比較少見房午。

場景三矿辽、項(xiàng)目里成熟的功能模塊可以從項(xiàng)目里剝離,通過pods導(dǎo)入郭厌。


對(duì)于這幾種場景袋倔,我們可以通過以下幾個(gè)方法來解決,下面來詳細(xì)介紹下:

一折柠、pods導(dǎo)入本地庫

1.創(chuàng)建Podfile

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

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

2.創(chuàng)建MyTestKit文件夾宾娜,并創(chuàng)建podspec文件

pod spec create MyTestKit

文件目錄結(jié)構(gòu)如下:

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

4.修改podspec文件

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

《如何編寫一個(gè) 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.執(zhí)行pod install命令

按照提示把podspec文件修改正確承冰,直到pod install執(zhí)行成功

最后工程結(jié)構(gòu)如下圖所示:

二华弓、pods導(dǎo)入網(wǎng)絡(luò)上的庫并解決沖突問題

1.導(dǎo)入GitHub上的三方庫

這個(gè)是我們用的最多的情況,不再贅述困乒。

2.導(dǎo)入私有倉庫里的庫

隨著項(xiàng)目開發(fā)的迭代寂屏,工程越來越大,很有必要把一些自定義控件顶燕、功能模塊從工程里剝離出來凑保,移植給更多的項(xiàng)目使用。要導(dǎo)入庫涌攻,需要具備兩個(gè)條件:(1)源碼倉庫(2)對(duì)應(yīng)的podspec.json文件的倉庫

對(duì)于源碼倉庫和podspec.json文件的倉庫欧引,可以是github上的,也可以是自己公司的私有倉庫恳谎。解決多個(gè)三方庫沖突的方法就是在這里做文章芝此。咱們就先來講講如何導(dǎo)入私有庫憋肖。如下是一個(gè)示例:

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

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

3.沖突解決

(1)上面這個(gè)案例只是修改了dependencies字段的值廓译,當(dāng)然了我們可以根據(jù)需要去修改platforms评肆,source,source_files非区,vendored_frameworks瓜挽,vendored_libraries等等字段的值來避免與其它三方庫的沖突。

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

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

方案二:fork源代碼到自己的github帳號(hào)下,然后修改代碼排惨,引入時(shí)采用 pod 'RSKImageCropper',:git => 'https://github.com/zhfeng20108/RSKImageCropper',即可指向修改后的代碼庫吭敢。復(fù)雜的可能就需要在自己帳號(hào)下再建一個(gè)庫存放podspec.json文件,以便精確控制代碼暮芭。

以上兩種方案,無論哪種欲低,都會(huì)遇上三方源碼升級(jí)問題辕宏,對(duì)于方案一,我們只能把最新代碼copy過去砾莱,檢查下原來修改的代碼現(xiàn)在是否需要調(diào)整瑞筐。對(duì)于方案二,可通過方法fork后代碼和源代碼同步來解決腊瑟。

用CocoaPods做iOS程序的依賴管理

git安裝和使用

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末聚假,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子闰非,更是在濱河造成了極大的恐慌膘格,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,509評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件财松,死亡現(xiàn)場離奇詭異瘪贱,居然都是意外死亡纱控,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人昧辽,你說我怎么就攤上這事应又。” “怎么了孕锄?”我有些...
    開封第一講書人閱讀 163,875評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我闹获,道長,這世上最難降的妖魔是什么河哑? 我笑而不...
    開封第一講書人閱讀 58,441評(píng)論 1 293
  • 正文 為了忘掉前任避诽,我火速辦了婚禮,結(jié)果婚禮上璃谨,老公的妹妹穿的比我還像新娘沙庐。我一直安慰自己,他們只是感情好佳吞,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評(píng)論 6 392
  • 文/花漫 我一把揭開白布拱雏。 她就那樣靜靜地躺著,像睡著了一般底扳。 火紅的嫁衣襯著肌膚如雪铸抑。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,365評(píng)論 1 302
  • 那天衷模,我揣著相機(jī)與錄音鹊汛,去河邊找鬼。 笑死阱冶,一個(gè)胖子當(dāng)著我的面吹牛刁憋,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播木蹬,決...
    沈念sama閱讀 40,190評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼至耻,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼!你這毒婦竟也來了镊叁?” 一聲冷哼從身側(cè)響起尘颓,我...
    開封第一講書人閱讀 39,062評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎晦譬,沒想到半個(gè)月后疤苹,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,500評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡蛔添,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評(píng)論 3 335
  • 正文 我和宋清朗相戀三年痰催,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了兜辞。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,834評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡夸溶,死狀恐怖逸吵,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情缝裁,我是刑警寧澤扫皱,帶...
    沈念sama閱讀 35,559評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站捷绑,受9級(jí)特大地震影響韩脑,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜粹污,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評(píng)論 3 328
  • 文/蒙蒙 一段多、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧壮吩,春花似錦进苍、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至沈贝,卻和暖如春杠人,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背宋下。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評(píng)論 1 269
  • 我被黑心中介騙來泰國打工嗡善, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人杨凑。 一個(gè)月前我還...
    沈念sama閱讀 47,958評(píng)論 2 370
  • 正文 我出身青樓滤奈,卻偏偏與公主長得像,于是被迫代替她去往敵國和親撩满。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • 項(xiàng)目組件化绅你、平臺(tái)化是技術(shù)公司的共同目標(biāo)伺帘,越來越多的技術(shù)公司推崇使用pod管理第三方庫以及私有組件,一方面使項(xiàng)目架構(gòu)...
    swu_luo閱讀 21,750評(píng)論 0 39
  • 一忌锯、創(chuàng)建的github倉庫原文地址 1 進(jìn)入Github網(wǎng)站www.github.com登陸自己的賬號(hào)后 2 建立...
    freesan44閱讀 3,932評(píng)論 2 12
  • 這篇有點(diǎn)亂伪嫁,簡潔版請(qǐng)點(diǎn)擊CocoaPods建立私有倉庫 spec repo 整理篇 介紹 好多項(xiàng)目里都有公共的組件...
    荔枝lizhi_iOS程序猿閱讀 12,471評(píng)論 3 18
  • 最近在學(xué)習(xí)vue.js的時(shí)候發(fā)現(xiàn),vue的組件化的思想對(duì)于編寫代碼是一個(gè)非常有用的事情偶垮。 首先為什么需要組件化张咳? ...
    拂曉的云閱讀 7,172評(píng)論 6 23
  • Ruby 安裝 要安裝cocospods 首先需要安裝ruby帝洪,可以先安裝xcode,再安裝macport 脚猾,最后...
    山天大畜閱讀 1,856評(píng)論 0 1