項(xiàng)目到了一定規(guī)模, 項(xiàng)目的代碼組織和結(jié)構(gòu)顯得尤為重要.
重構(gòu)項(xiàng)目結(jié)構(gòu), 可以從分離代碼開(kāi)始.
代碼分離, 可以把常用穩(wěn)定的組件封裝抽離出來(lái).
我的做法是使用 cocoapods 來(lái)管理.
下面進(jìn)入今天的主題: 使用 cocoapods 管理自己的本地代碼.
使用 xcode 創(chuàng)建一個(gè)工程, 工程名就起為T(mén)estPods.在桌面或者你喜歡的目錄下面都可以.
在 TestPods 下面創(chuàng)建 LocalLib 目錄, 用來(lái)放置分離的代碼.
在 LocalLib 下面, 我的 pod 庫(kù)代碼名稱(chēng)為 download.可以新建這個(gè)目錄.
我的目錄如下
在 download 目錄下面, 創(chuàng)建 podspec 文件, 創(chuàng)建命令如下
pod spec create download
具體文件內(nèi)容, 大家在創(chuàng)建后, 可以自行查看.
修改 download.podspec, 主要修改幾個(gè)關(guān)鍵地方:
1. 源碼位置
2. 源碼版本
# ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# Specify the location from where the source should be retrieved.
# Supports git, hg, bzr, svn and HTTP.
#
s.source = { :git => "", :tag => "0.0.1" }
# ――― 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 = "Source", "Source/**/*.{h,m}"
s.exclude_files = "Source/Exclude"
另外, 配置好相關(guān)描述信息, 不要包含'Example'的字樣, 不然, 新版的 pod 在執(zhí)行 pod install 時(shí)候, 會(huì)報(bào)出警告和錯(cuò)誤.
其他工程可以使用 pods 庫(kù)了.
將 TestPods 改為 cocoapods 項(xiàng)目.
在 TestPods 目錄, 執(zhí)行
pod init
修改 Podfile 文件
# Uncomment this line to define a global platform for your project
# platform :ios, '7.0'
# Uncomment this line if you're using Swift
# use_frameworks!
target 'TestPods' do
pod 'download', :path => './LocalLib/download/'
#pod 'core_lib_spec', :svn => 'http://svn.ids111.com/o2o/client/ios/trunks/master/Frameworks/CoreLibrary'
end
target 'TestPodsTests' do
end
target 'TestPodsUITests' do
end
關(guān)鍵是指明 pod 庫(kù)的位置.
pod 'download', :path => './LocalLib/download/'
在 TestPods 下面, pod install 即可.
如果, pod install 報(bào)錯(cuò), 一般都是你的 pod 庫(kù)的配置文件(.podspec)里面寫(xiě)的不符合要求.
根據(jù)報(bào)錯(cuò)信息, 加以修改即可.
xcode 打開(kāi)工程.