使用CocoaPods創(chuàng)建自己的私有庫-iOS組件化第一步

目前iOS組件化常用的解決方案是Pod+路由+持續(xù)集成,通常架構設計完成后第一步就是將原來工程里的模塊按照架構圖分解為一個個獨立的pod工程(組件),今天我們就來看看如何創(chuàng)建一個Pod私有庫。

新建:pod lib create

假設我們需要創(chuàng)建的庫名為TestLib,下面我們使用Pod官方提供的創(chuàng)建模板:

首先進入我們的工作目錄,如workspace,輸入命令

pod lib create TestLib

創(chuàng)建過程中需要填寫幾個問題,如下圖所示,按個人所需填寫:

創(chuàng)建完成以后工程會自動打開,Xcode目錄和實際路徑有一定區(qū)別,截圖如下:

image

解決這個問題也很簡單,將文件夾作為Group拖動到Xcode中即可:(如果Xcode工程中本身已經包含Classes目錄,可以忽略這一步)

然后刪除ReplaceMe.swift文件,在Class目錄創(chuàng)建一個名為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工程下執(zhí)行(有時Xcode不會更新Pod代碼,需要重啟)

pod install

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

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

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

新建完成后,我們還需要驗證,需要修改配置文件,通過下面的截圖路徑找到新建的私有庫的配置文件:


或者在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'
  
  # 包含的系統(tǒng)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

必要的話還需要更新pod

sudo gem update cocoapods
  • [!] TestLib did not pass validation, due to 3 warnings (but you can use `--allow-warnings` to ignore them).
    pod驗證發(fā)現(xiàn)3個及以上的warning就會報這個錯,如果只是驗證一下工程,能確保對外發(fā)布之前能修復,可以使用--allow-warnings

pod lib lint --allow-warnings

如果驗證通過,會看到TestLib passed validation.,到這一步既完成驗證

版本:iOS和Swift管理

通過pod官方模板做出來的工程iOS版本為8.0,Swift版本為4.0,我們有時需要根據(jù)需要修改版本號,需要在spec文件中添加:

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

  # Swift版本
  s.swift_versions = '5.0'

然后執(zhí)行pod install更新工程即可

Git上傳到私有庫

現(xiàn)在私有Git服務器創(chuàng)建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 }

如果需要對外發(fā)布版本時需打tag,然后創(chuàng)建同名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參數(shù):

參數(shù)名 注釋
--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 生成靜態(tài)framework
--library 生成靜態(tài).a
--dynamic 生成動態(tài)framework
--bundle-identifier 動態(tài)framework需要的簽名
--exclude-deps 不包含依賴的符號表,生成動態(tài)庫的時候不能包含這個命令,動態(tài)庫一定需要包含依賴的符號表
--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
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容