目前iOS組件化常用的解決方案是Pod+路由+持續(xù)集成,通常架構(gòu)設(shè)計(jì)完成后第一步就是將原來(lái)工程里的模塊按照架構(gòu)圖分解為一個(gè)個(gè)獨(dú)立的pod工程(組件),今天我們就來(lái)看看如何創(chuàng)建一個(gè)Pod私有庫(kù)廊谓。
新建:pod lib create
假設(shè)我們需要?jiǎng)?chuàng)建的庫(kù)名為TestLib,下面我們使用Pod官方提供的創(chuàng)建模板:
首先進(jìn)入我們的工作目錄,如workspace险污,輸入命令
pod lib create TestLib
創(chuàng)建過(guò)程中需要填寫幾個(gè)問(wèn)題,如下圖所示富岳,按個(gè)人所需填寫:
創(chuàng)建完成以后工程會(huì)自動(dòng)打開蛔糯,Xcode目錄和實(shí)際路徑有一定區(qū)別,截圖如下:
解決這個(gè)問(wèn)題也很簡(jiǎn)單窖式,將文件夾作為Group拖動(dòng)到Xcode中即可:(如果Xcode工程中本身已經(jīng)包含Classes目錄蚁飒,可以忽略這一步)
然后刪除ReplaceMe.swift文件,在Class目錄創(chuàng)建一個(gè)名為TestClass的swift文件:
在TestClass中定義ClassA和ClassB萝喘,注意其中一個(gè)是public的
import Foundation
public class ClassA { //這是public類淮逻,可被外部工程訪問(wèn)
public let name = "ClassA"
let age = 18
}
class ClassB { //不是public類,不能被外部工程訪問(wèn)
let name = "ClassB"
}
重要:為了讓改動(dòng)生效阁簸,一定要重啟Xcode一次爬早,然后在Example工程下執(zhí)行(有時(shí)Xcode不會(huì)更新Pod代碼,需要重啟)
pod install
就可以在代碼中調(diào)用新寫的類强窖,注意到只能調(diào)用public
修飾的屬性
到這里使用Pod新建一個(gè)私有庫(kù)就完成了凸椿。
驗(yàn)證: pod lib lint (podspec配置文件說(shuō)明)
新建完成后,我們還需要驗(yàn)證翅溺,需要修改配置文件脑漫,通過(guò)下面的截圖路徑找到新建的私有庫(kù)的配置文件:
或者在Xcode里的:
文件內(nèi)容:
#
# Be sure to run `pod lib lint TestLib.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
# 名稱髓抑、版本號(hào)、概述
s.name = 'TestLib'
s.version = '0.1.0'
s.summary = 'A short description of TestLib.'
# 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!
# 詳細(xì)描述
s.description = <<-DESC
TODO: Add long description of the pod here.
DESC
# 主頁(yè)优幸、截圖吨拍、license證書、作者信息网杆、源代碼地址羹饰、社交地址
s.homepage = 'https://github.com/xxx/TestLib'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'xxx' => 'xxx@xxx.com' }
s.source = { :git => 'https://github.com/xxx/TestLib.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
# iOS版本
s.ios.deployment_target = '8.0'
# 源碼所在路徑
s.source_files = 'TestLib/Classes/**/*'
# 資源文件所在地址
# s.resource_bundles = {
# 'TestLib' => ['TestLib/Assets/*.png']
# }
# 對(duì)外公開的h文件地址,swift一般用不到
# s.public_header_files = 'Pod/Classes/**/*.h'
# 包含的系統(tǒng)framework
# s.frameworks = 'UIKit', 'MapKit'
# 包含的第三方pod
# s.dependency 'AFNetworking', '~> 2.3'
end
更詳細(xì)的介紹可以訪問(wèn)官網(wǎng)https://guides.cocoapods.org/syntax/podspec.html
配置好以后我們需要做一次驗(yàn)證碳却,在工程目錄下使用命令
pod lib lint
初次驗(yàn)證可能遇到的幾個(gè)問(wèn)題:
-
Could not find a `ios` simulator (valid values: ). Ensure that Xcode -> Window -> Devices has at least one `ios` simulator listed or otherwise add one.
這個(gè)問(wèn)題是pod依賴的組件fourflusher與xcode版本不匹配造成的队秩,可以使用如下命令更新
1.sudo gem uninstall fourflusher
2.sudo gem install fourflusher
必要的話還需要更新pod
sudo gem update cocoapods
-
[!] TestLib did not pass validation, due to 3 warnings (but you can use `--allow-warnings` to ignore them).
pod驗(yàn)證發(fā)現(xiàn)3個(gè)及以上的warning就會(huì)報(bào)這個(gè)錯(cuò),如果只是驗(yàn)證一下工程昼浦,能確保對(duì)外發(fā)布之前能修復(fù)馍资,可以使用--allow-warnings
pod lib lint --allow-warnings
如果驗(yàn)證通過(guò),會(huì)看到TestLib passed validation.
,到這一步既完成驗(yàn)證
版本:iOS和Swift管理
通過(guò)pod官方模板做出來(lái)的工程iOS版本為8.0关噪,Swift版本為4.0鸟蟹,我們有時(shí)需要根據(jù)需要修改版本號(hào),需要在spec文件中添加:
# iOS版本
s.ios.deployment_target = '9.0'
# Swift版本
s.swift_versions = '5.0'
然后執(zhí)行pod install
更新工程即可
Git上傳到私有庫(kù)
現(xiàn)在私有Git服務(wù)器創(chuàng)建TestLib項(xiàng)目使兔,然后回到工程目錄建钥,使用Git初始化命令:
git init
git remote add origin http://私有倉(cāng)庫(kù)地址/TestLib.git
git add .
git commit -m 'init'
git push -u origin master
然后修改spec文件內(nèi)容
# 主頁(yè)、截圖虐沥、license證書熊经、作者信息、源代碼地址置蜀、社交地址
s.homepage = 'http://私有庫(kù)地址/TestLib.git'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'xxx' => 'xxx@xxx.com' }
s.source = { :git => 'http://私有庫(kù)地址/TestLib.git', :tag => s.version.to_s }
如果需要對(duì)外發(fā)布版本時(shí)需打tag,然后創(chuàng)建同名branch
git tag -a 0.1.0 -m '0.1.0'
git branch 0.1.0
打包:pod package
有時(shí)我們不希望提供源代碼奈搜,只提供framework給外部調(diào)用,可以使用package
盯荤,首先安裝pod插件:
sudo gem install cocoapods-packager
package參數(shù):
參數(shù)名 | 注釋 |
---|---|
--force | 覆蓋之前的文件 |
--no-mangle | 1.表示不使用name mangling技術(shù)馋吗,pod package默認(rèn)是使用這個(gè)技術(shù)的。我們能在用pod package生成二進(jìn)制庫(kù)的時(shí)候會(huì)看到終端有輸出Mangling symbols和Building mangled framework秋秤。表示使用了這個(gè)技術(shù)宏粤。2.如果你的pod庫(kù)沒(méi)有其他依賴的話,那么不使用這個(gè)命令也不會(huì)報(bào)錯(cuò)灼卢。但是如果有其他依賴绍哎,不使用--no-mangle這個(gè)命令的話,那么你在工程里使用生成的二進(jìn)制庫(kù)的時(shí)候就會(huì)報(bào)錯(cuò):Undefined symbols for architecture x86_64鞋真。 |
--embedded | 生成靜態(tài)framework |
--library | 生成靜態(tài).a |
--dynamic | 生成動(dòng)態(tài)framework |
--bundle-identifier | 動(dòng)態(tài)framework需要的簽名 |
--exclude-deps | 不包含依賴的符號(hào)表崇堰,生成動(dòng)態(tài)庫(kù)的時(shí)候不能包含這個(gè)命令,動(dòng)態(tài)庫(kù)一定需要包含依賴的符號(hào)表 |
--configuration | 表示生成的庫(kù)是debug還是release,默認(rèn)是release海诲。--configuration=Debug |
--subspecs | 如果你的pod庫(kù)有subspec繁莹,那么加上這個(gè)命名表示只給某個(gè)或幾個(gè)subspec生成二進(jìn)制庫(kù),--subspecs=subspec1,subspec2特幔。生成的庫(kù)的名字就是你podspec的名字咨演,如果你想生成的庫(kù)的名字跟subspec的名字一樣,那么就需要修改podspec的名字蚯斯。 這個(gè)腳本就是批量生成subspec的二進(jìn)制庫(kù)薄风,每一個(gè)subspec的庫(kù)名就是podspecName+subspecName。 |
--spec-sources=private,https://github.com/CocoaPods/Specs.git | 一些依賴的source拍嵌,如果你有依賴是來(lái)自于私有庫(kù)的遭赂,那就需要加上那個(gè)私有庫(kù)的source,默認(rèn)是cocoapods的Specs倉(cāng)庫(kù)撰茎。--spec-sources=private,https://github.com/CocoaPods/Specs.git嵌牺。 |
可以使用下面的命令打包:
pod package TestLib.podspec --force