關(guān)于pod的制作就不細(xì)說了铆惑,這里主要說說carthage的模塊化的制作依賴過程范嘱。
其實(shí)carthage模塊化比pod化更簡單,這里只是梳理一下流程以及對工程配置的注意點(diǎn)
這里講解的demoSDK是依賴了pod庫和其他carthage庫的一個(gè)demo,也是最復(fù)雜的一個(gè)依賴類型
1. 新建一個(gè)framework工程
- 新建一個(gè)
Cocoa Touch FrameWork
類型的工程员魏,
這個(gè)時(shí)候CarthageMoudlesSDK只有一個(gè)xcodeproj工程丑蛤,并沒有工作區(qū)域,因?yàn)楹竺嫖覀冞€要為demoSDK配置一個(gè)demo,理應(yīng)先新建一個(gè)workspace撕阎,這里為了方便受裹,通過引入pod庫的方式自動(dòng)創(chuàng)建一個(gè)workspace(因?yàn)閐emoSDK本來就需要依賴pod庫)
- 新建
Podfile
文件,按照正常的流程先添加幾個(gè)庫闻书,然后執(zhí)行pod install
名斟。將workspace文件生成出來
podfile文件內(nèi)容:
gitConfigUserName = %x{git config --get user.name}
gerritName = gitConfigUserName.split("\n")[0]
gerritURL = "ssh://#{gerritName}@internal.smartdevices.com.cn:29418/apps/Specs-iOS"
platform :ios, '9.0'
inhibit_all_warnings!
## 這里配置的是私有庫source
source gerritURL
source 'https://github.com/CocoaPods/Specs.git'
target 'CarthageMoudlesSDK' do
pod 'ZFPlayer', '3.1.6'
pod 'ZFPlayer/AVPlayer', '3.1.6'
pod 'ZFPlayer/ControlView', '3.1.6'
pod 'KTVHTTPCache', '~> 1.1.5'
end
2. 新建一個(gè)demo工程
-
打開
CarthageMoudlesSDK.xcworkspace
文件冒版,然后cmd+shift+N
新建一個(gè)Single View App
,填好demo名稱呻待,點(diǎn)擊next,注意,這里一定要將新建的project添加到CarthageMoudlesSDK.xcworkspace
工作區(qū)域
-
完成之后的工程目錄應(yīng)該是這樣的
ok,如果這個(gè)demo不需要依賴其他pod庫的話那就這樣序目。
3. 添加其他pod庫依賴
如果demo需要其他pod庫支持或者carthage庫支持的話坑律,那么就需要修改Podfile
文件,其中指定workspace和project
在模塊化的過程中岩梳,這種情況很正常,因?yàn)槟愕哪K是從主項(xiàng)目剝離出來晃择,但是你的項(xiàng)目只依賴A庫冀值,可以要想整個(gè)demo運(yùn)行起來,并且可以跑單元測試宫屠,那么就需要依賴主項(xiàng)目的網(wǎng)絡(luò)庫和kiwi等庫文件,
修改后的podfile文件內(nèi)容:
gitConfigUserName = %x{git config --get user.name}
gerritName = gitConfigUserName.split("\n")[0]
gerritURL = "ssh://#{gerritName}@internal.smartdevices.com.cn:29418/apps/Specs-iOS"
platform :ios, '9.0'
inhibit_all_warnings!
## 這里配置的是私有庫source
source gerritURL
source 'https://github.com/CocoaPods/Specs.git'
##設(shè)置workspace文件
workspace 'CarthageMoudlesSDK.xcworkspace'
target 'CarthageModulesDemo' do
project 'CarthageModulesDemo/CarthageModulesDemo.xcodeproj'
pod 'AFNetworking', '~> 3.0'
end
target 'CarthageModulesDemoTests' do
project 'CarthageModulesDemo/CarthageModulesDemoTests.xcodeproj'
pod 'Kiwi'
pod 'OCMock', '3.4'
end
target 'CarthageMoudlesSDK' do
project 'CarthageMoudlesSDK.xcodeproj'
pod 'ZFPlayer', '3.1.6'
pod 'ZFPlayer/AVPlayer', '3.1.6'
pod 'ZFPlayer/ControlView', '3.1.6'
pod 'KTVHTTPCache', '~> 1.1.5'
end
執(zhí)行pod install
成功列疗,目前情況是:
你的CarthageMoudlesSDK
依賴ZFPlayer
庫
你的CarthageModulesDemo
依賴 AFNetworking
來支撐運(yùn)行
你的CarthageModulesDemoTests
依賴Kiwi
來支撐
上面我們已經(jīng)搞定了pod的依賴庫
現(xiàn)在我們?nèi)鄙俚木褪荂arthageMoudlesSDK依賴的carthage庫,和CarthageModulesDemo依賴的carthage庫
4. 添加其他carthage庫依賴
首先說一下浪蹂,carthage庫會(huì)被clone到主項(xiàng)目的本地目錄抵栈,然后通過carthage/Checkouts/.*中clone下來的cartfile文件去自動(dòng)fetch/pull相關(guān)的庫告材,而我們的demo依賴的cartfile庫又不需要主項(xiàng)目去fetch/pull,不然會(huì)浪費(fèi)時(shí)間去拉取一些不必要的庫,所以這里就需要提到cartfile.private
官文:
Frameworks that want to include dependencies via Carthage, but do not want to force those dependencies on parent projects, can list them in the optional Cartfile.private file, identically to how they would be specified in the main Cartfile.
Anything listed in the private Cartfile will not be seen by dependent (parent) projects, which is useful for dependencies that may be important during development, but not when building releases—for example, test frameworks.
大致意思就是說上游的項(xiàng)目不需要去關(guān)注的依賴可以列在private文件中古劲,也就是說你制作的SDK中如果有不需要被上有項(xiàng)目用到斥赋,但是你的demo有用到就可以使用cartfile.private文件,例如單元測試。
關(guān)于Cartfile.private的官文地址:
所以這里為了測試产艾,再次加入一些單元測試的庫
4.1. 編寫cartfile文件
- 生成Cartfile.private和Cartfile文件
Cartfile:
github "CoderMJLee/MJRefresh" == 3.1.15
Cartfile.private:
github "rs/SDWebImage" == 4.1.0
github "specta/specta" ~> 1.0
specta
的官文也推薦了使用Cartfile.private,這里不僅僅適用于測試庫疤剑,比如你的SDKDemo和主工程都需要依賴同一個(gè)庫B,但是最終你的SDK需要集成進(jìn)主工程闷堡,這時(shí)候庫B就應(yīng)該放在Cartfile.private
中
編寫完執(zhí)行carthage update --platform iOS
到現(xiàn)在為止sdk和sdkdemo該依賴的庫都基本完成隘膘,接下來是內(nèi)部依賴
5. framework工程配置
5.1. SDK工程配置
-
工程配置
這里這樣寫,這就要求你
carthage update
之后你的carthage文件夾必須保持和project在同一個(gè)目錄路徑下
注意修改你的工程配置Valid Architectures
, 保持和主工程目錄一直杠览,不然集成進(jìn)去會(huì)報(bào)arm相關(guān)的錯(cuò)誤
- public頭文件的暴露
- framework依賴處理
5.2. SDKDemo工程配置
-
SDKDemo工程配置和集成Carthage庫一樣棘幸,
在build Phases
中新建一個(gè)Run Script Phases
然后從Carthage/Build/iOS文件夾中將幾個(gè)庫一一對應(yīng)的拖入到
Linked Frameworks and Libraries
保證SDK的demo能夠成功運(yùn)行
5.3.分享SDK的scheme
-
將SDK的scheme分享出來
workspace的根目錄下執(zhí)行
carthage build --no-skip-current --platform iOS
上傳項(xiàng)目到遠(yuǎn)倉庫,打上tag
6. 集成SDK
- 在主工程的cartfile文件中加入
github "chaserr/CarthageModulesDemo" "master"
- 執(zhí)行
carthage update CarthageModulesDemo --platform iOS
主工程會(huì)主動(dòng)從clone到Carthage/Checkouts/CarthageModulesSDK文件中去讀取相應(yīng)的cartfile文件和podfile文件倦零,不需要我們?nèi)リP(guān)心