隨著產(chǎn)品的迭代升級,增加的模塊越來越多,項(xiàng)目也會越來越復(fù)雜.隨之也就帶來的一個非常頭疼的問題:代碼量龐大(動輒上百M(fèi)),更讓人難以忍受的是蝸牛般的編譯速度.為了解決這個問題,我們試著對項(xiàng)目進(jìn)行基于功能的模塊拆分,也就是近來一直提出的模塊化.
前期我們參考了豆瓣的開源模塊化和github上一個star數(shù)字較多的框架,它們都是對一個項(xiàng)目中代碼的分離,利用NSClassFromString方法,通過對路徑處理或者直接傳遞字符串,創(chuàng)建所需要的類型.這樣能對不同模塊的代碼進(jìn)行解耦,但是仍然無法將項(xiàng)目拆解成不同功能的小項(xiàng)目.經(jīng)過不斷的討論,我們最終選擇了基于cocoapod的模塊化.
將不同的模塊獨(dú)立成一個項(xiàng)目,讓后再主工程中通過cocoapod對不同的項(xiàng)目進(jìn)行管理和集成,使負(fù)責(zé)人員專注于自己的模塊進(jìn)行開發(fā),而主工程通過不同的tag對模塊進(jìn)行引用,避免了在所有人都要在主工程運(yùn)行全部代碼的頭疼問題.
基于cocoapod,所以需要對cocoapod有較全面的理解,請參考我上篇寫的:
從零創(chuàng)建cocoapods私有倉庫
下面以一個簡單的Demo,詳細(xì)介紹實(shí)現(xiàn)思路.
項(xiàng)目中"我的"模塊,一般都會帶有登錄驗(yàn)證,兒其他模塊也有可能會用到登錄部分,所以我們可以把登錄獨(dú)立出一個模塊.
首先在gitlub新建一個repository,命名為LoginModule,如下圖:
具體選擇請參考上圖.
然后通過SourceTree將工程下載到本地,添加Xcode新建的LoginModule工程,最后的文件層級圖應(yīng)該是下圖這樣的:
為了更方便的創(chuàng)建.podsepc文件,如果不是上述文件層級,請手動調(diào)整.其中.podspec文件,是cocoapods引用時的識別文件,由于需要與文件層級很嚴(yán)格的配置,建議直接拷貝,下面是我經(jīng)過測試正確的配置:
Pod::Spec.new do |s| #s代表文件夾位置為一級,ss代表文件夾位置為二級
s.name = "LoginModule"
s.version = "0.0.16"
s.summary = "LoginModule for iOS project."
s.description = <<-DESC
LoginModule
DESC
s.homepage = "https://github.com/zhudong10/LoginModule"
s.license = { :type => "MIT", :file => "LICENSE" }
#導(dǎo)入工程的pch文件
s.prefix_header_file = 'LoginModule/ZDPrefixHeader.pch'
s.author = { "zhudong" => "zhudongdong@91guoxin.com" }
s.source = { :git => "https://github.com/zhudong10/LoginModule", :tag => "#{s.version}" }
s.source_files = "LoginModule/LoginModule.h" #此處需要在本級目錄下找到文件
# 目的文件夾名稱 #
s.subspec 'LoginModule' do |ss|
# 文件來源 #
ss.source_files = 'LoginModule/LoginUI/**/*.{h,m}'
# 資源文件 #
ss.resources = ['LoginModule/Login.xcassets', 'LoginModule/LoginUI/**/*.xib']
#ss.resources = 'LoginModule/LoginUI/**/*.xib'
#ss.resources = 'LoginModule/**/*.xcassets'
end
s.subspec 'Category' do |ss|
# 文件來源 #
ss.source_files = 'LoginModule/Category/**/*.{h,m}'
end
end
.podspec文件識別的是實(shí)體文件夾,為了便于對文件管理,在LoginModule中建立了如下的文件夾和文件,和上述的配置相匹配.
由于cocoapod引用時,如果沒有tag的區(qū)分,只會引用第一次提交的代碼,所以需要對提交的代碼分支,添加tag,方法如下圖:
而且需要推動到遠(yuǎn)程分支:
經(jīng)歷上面的步驟,就完成了LoginModule工程的創(chuàng)建.下面讓我們在主工程中對其用cocoapod進(jìn)行引用.
新建工程MineModule,對其進(jìn)行pod init,創(chuàng)建podfile文件,對podfile文件進(jìn)行如下配置:
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'MineModule' do
# Uncomment the next line if you're using Swift or would like to use dynamic frameworks
# use_frameworks!
pod 'LoginModule', :git => 'https://github.com/zhudong10/LoginModule', :tag =>'0.0.16', :inhibit_warnings => false
end
其中的tag就是對LoginModule項(xiàng)目引用的區(qū)分.
執(zhí)行pod install命令后,就能順利引如LoginModule模塊,如果出現(xiàn)錯誤:
zhudongdeMacBook-Pro:MineModule zhudong$ pod install
Analyzing dependencies
Pre-downloading: `LoginModule` from `https://github.com/zhudong10/LoginModule`, tag `0.0.3`
[!] Unable to find a specification for 'LoginModule'.
[!] Unable to load a podspec from `LoginModule.podspec`, skipping:
Pod::DSLError
是LoginModule工程中.podspec文件配置錯誤,請?jiān)敿?xì)檢查;
如果pod install成功引用LoginModuel,但是編譯時出現(xiàn):
/Users/zhudong/Documents/Demo/Later2017.5.5/MineModule/MineModule/Pods/LoginModule/LoginModule/LoginUI/LoginSuccessController.m:17:1: Cannot synthesize weak property because the current deployment target does not support weak references
則是因?yàn)閤ib文件沒有正確引用,需要在MineModule的podfile中添加修改代碼,最終的語句如下:
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'MineModule' do
# Uncomment the next line if you're using Swift or would like to use dynamic frameworks
# use_frameworks!
pod 'LoginModule', :git => 'https://github.com/zhudong10/LoginModule', :tag =>'0.0.16', :inhibit_warnings => false
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '8.0'
end
end
end
end
如果大功告成,文件層級結(jié)果應(yīng)該如下圖:
最終運(yùn)行MineModule,我們看到:
如果開發(fā)過程中,登錄內(nèi)容有所變化,那就只要在LoginModule中進(jìn)行更改,然后在主工程中更新pod就行了,感覺是不是超棒?
點(diǎn)擊收藏和喜歡都是對我的支持和鼓勵