私有庫的使用場景
場景一饵较、在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(可直接編輯)
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后代碼和源代碼同步來解決腊瑟。