使用CocoaPods建立本身的私有庫-iOS組件化第一步

目前iOS組件化經常使用的解決方案是Pod+路由+持續集成,一般架構設計完成後第一步就是將原來工程裏的模塊按照架構圖分解爲一個個獨立的pod工程(組件),今天咱們就來看看如何建立一個Pod私有庫。html

新建:pod lib create

假設咱們須要建立的庫名爲TestLib,下面咱們使用Pod官方提供的建立模板:ios

首先進入咱們的工做目錄,如workspace,輸入命令

pod lib create TestLib

建立過程當中須要填寫幾個問題,以下圖所示,按我的所需填寫:

建立完成之後工程會自動打開,Xcode目錄和實際路徑有必定區別,截圖以下:

解決這個問題也很簡單,將文件夾做爲Group拖動到Xcode中便可:(若是Xcode工程中自己已經包含Classes目錄,能夠忽略這一步)

而後刪除ReplaceMe.swift文件,在Class目錄建立一個名爲TestClass的swift文件:

在TestClass中定義ClassA和ClassB,注意其中一個是public的

import Foundation

public class ClassA {   //這是public類,可被外部工程訪問
    public let name = "ClassA"
    let age = 18
}

class ClassB {  //不是public類,不能被外部工程訪問
    let name = "ClassB"
}

重要:爲了讓改動生效,必定要重啓Xcode一次,而後在Example工程下執行(有時Xcode不會更新Pod代碼,須要重啓)

pod install

就能夠在代碼中調用新寫的類,注意到只能調用public修飾的屬性

到這裏使用Pod新建一個私有庫就完成了。git

驗證: pod lib lint (podspec配置文件說明)

新建完成後,咱們還須要驗證,須要修改配置文件,經過下面的截圖路徑找到新建的私有庫的配置文件:
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.
    這個問題是pod依賴的組件fourflusher與xcode版本不匹配形成的,可使用以下命令更新
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).
    pod驗證發現3個及以上的warning就會報這個錯,若是隻是驗證一下工程,能確保對外發布以前能修復,可使用--allow-warnings
pod lib lint --allow-warnings

若是驗證經過,會看到TestLib passed validation.,到這一步既完成驗證ide

版本:iOS和Swift管理

經過pod官方模板作出來的工程iOS版本爲8.0,Swift版本爲4.0,咱們有時須要根據須要修改版本號,須要在spec文件中添加:

# iOS版本
  s.ios.deployment_target = '9.0'

  # Swift版本
  s.swift_versions = '5.0'

而後執行pod install更新工程便可

Git上傳到私有庫

如今私有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

打包:pod package

有時咱們不但願提供源代碼,只提供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
相關文章
相關標籤/搜索