目前iOS組件化經常使用的解決方案是Pod+路由+持續集成,一般架構設計完成後第一步就是將原來工程裏的模塊按照架構圖分解爲一個個獨立的pod工程(組件),今天咱們就來看看如何建立一個Pod私有庫。html
假設咱們須要建立的庫名爲TestLib,下面咱們使用Pod官方提供的建立模板:ios
pod lib create TestLib
import Foundation public class ClassA { //這是public類,可被外部工程訪問 public let name = "ClassA" let age = 18 } class ClassB { //不是public類,不能被外部工程訪問 let name = "ClassB" }
pod install
public
修飾的屬性到這裏使用Pod新建一個私有庫就完成了。git
新建完成後,咱們還須要驗證,須要修改配置文件,經過下面的截圖路徑找到新建的私有庫的配置文件:
github
或者在Xcode裏的:
swift
文件內容:xcode
# # 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| # 名稱、版本號、概述 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! # 詳細描述 s.description = <<-DESC TODO: Add long description of the pod here. DESC # 主頁、截圖、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'] # } # 對外公開的h文件地址,swift通常用不到 # s.public_header_files = 'Pod/Classes/**/*.h' # 包含的系統framework # s.frameworks = 'UIKit', 'MapKit' # 包含的第三方pod # s.dependency 'AFNetworking', '~> 2.3' end
更詳細的介紹能夠訪問官網https://guides.cocoapods.org/syntax/podspec.html服務器
配置好之後咱們須要作一次驗證,在工程目錄下使用命令架構
pod lib lint
Could not find a `ios` simulator (valid values: ). Ensure that Xcode -> Window -> Devices has at least one `ios` simulator listed or otherwise add one.
1.sudo gem uninstall fourflusher 2.sudo gem install fourflusher
必要的話還須要更新podapp
sudo gem update cocoapods
[!] TestLib did not pass validation, due to 3 warnings (but you can use `--allow-warnings` to ignore them).
--allow-warnings
pod lib lint --allow-warnings
若是驗證經過,會看到TestLib passed validation.
,到這一步既完成驗證ide
經過pod官方模板作出來的工程iOS版本爲8.0,Swift版本爲4.0,咱們有時須要根據須要修改版本號,須要在spec文件中添加:
# iOS版本 s.ios.deployment_target = '9.0' # Swift版本 s.swift_versions = '5.0'
而後執行pod install
更新工程便可
如今私有Git服務器建立TestLib項目,而後回到工程目錄,使用Git初始化命令:
git init git remote add origin http://私有倉庫地址/TestLib.git git add . git commit -m 'init' git push -u origin master
而後修改spec文件內容
# 主頁、截圖、license證書、做者信息、源代碼地址、社交地址 s.homepage = 'http://私有庫地址/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://私有庫地址/TestLib.git', :tag => s.version.to_s }
若是須要對外發布版本時需打tag,而後建立同名branch
git tag -a 0.1.0 -m '0.1.0'
git branch 0.1.0
有時咱們不但願提供源代碼,只提供framework給外部調用,可使用package
,首先安裝pod插件:
sudo gem install cocoapods-packager
package參數:
參數名 | 註釋 |
---|---|
--force | 覆蓋以前的文件 |
--no-mangle | 1.表示不使用name mangling技術,pod package默認是使用這個技術的。咱們能在用pod package生成二進制庫的時候會看到終端有輸出Mangling symbols和Building mangled framework。表示使用了這個技術。2.若是你的pod庫沒有其餘依賴的話,那麼不使用這個命令也不會報錯。可是若是有其餘依賴,不使用--no-mangle這個命令的話,那麼你在工程裏使用生成的二進制庫的時候就會報錯:Undefined symbols for architecture x86_64。 |
--embedded | 生成靜態framework |
--library | 生成靜態.a |
--dynamic | 生成動態framework |
--bundle-identifier | 動態framework須要的簽名 |
--exclude-deps | 不包含依賴的符號表,生成動態庫的時候不能包含這個命令,動態庫必定須要包含依賴的符號表 |
--configuration | 表示生成的庫是debug仍是release,默認是release。--configuration=Debug |
--subspecs | 若是你的pod庫有subspec,那麼加上這個命名錶示只給某個或幾個subspec生成二進制庫,--subspecs=subspec1,subspec2。生成的庫的名字就是你podspec的名字,若是你想生成的庫的名字跟subspec的名字同樣,那麼就須要修改podspec的名字。 這個腳本就是批量生成subspec的二進制庫,每個subspec的庫名就是podspecName+subspecName。 |
--spec-sources=private,https://github.com/CocoaPods/Specs.git | 一些依賴的source,若是你有依賴是來自於私有庫的,那就須要加上那個私有庫的source,默認是cocoapods的Specs倉庫。--spec-sources=private,https://github.com/CocoaPods/Specs.git。 |
可使用下面的命令打包:
pod package TestLib.podspec --force