一、CocoaPods簡(jiǎn)介
iOS 和 OS X下的一個(gè)第三方庫(kù)管理工具,類似的iOS工具還有Carthage(比較輕量級(jí),需要手動(dòng)配置,非侵入式),與Java里的Maven也比較類似,但是沒有maven的構(gòu)建、運(yùn)行程序、打包等功能,僅僅是庫(kù)依賴配置和庫(kù)版本管理工具。
作用:依賴庫(kù)版本管理、庫(kù)依賴自動(dòng)配置;
二、安裝與使用
安裝
- sudo gem install cocoapods
由于被墻,通常需要切換gem源
查看gem源
gem sources -l
刪除gem源
gem sources --remove https://ruby.taobao.org/
修改gem源
gem sources -a https://gems.ruby-china.org
- pod setup
在本地建立一個(gè)Cocoapods里描述文件主倉(cāng)庫(kù),然后建立遠(yuǎn)程庫(kù)索引,出現(xiàn)Setup completed 則安裝完成
基本使用
- 命令pod search xx, 查找?guī)煜嚓P(guān)信息
- podfile配置
Podfile文件描述了工程中一個(gè)或者多個(gè)Target的依賴關(guān)系。Podfile會(huì)默認(rèn)創(chuàng)建一個(gè)隱式的Target鏈接到工程中用戶的第一個(gè)target,名稱未“default”;
簡(jiǎn)單的Podfile
pod 'AFNetworking', '~> 1.0'
復(fù)雜的Podfile
platform :ios, '9.0'
inhibit_all_warnings!
target 'MyApp' do
pod 'ObjectiveSugar', '~> 0.5'
target "MyAppTests" do
inherit! :search_paths
pod 'OCMock', '~> 2.0.1'
end
end
post_install do |installer|
installer.pods_project.targets.each do |target|
puts "#{target.name}"
end
end
依賴配置
pod 'Lib' 或者 pod 'Lib', '1.0.1' 或者 pod 'lib', ~>'1.0.1'
指定了依賴庫(kù)的名稱和版本
- 不填,默認(rèn)使用最新版本
- = 0.1
- > 0.1
- >= 0.1
- < 0.1
- <= 0.1
- ~> 0.1.2 等價(jià)于 0.1.2<=版本<0.2.0, 并使用這個(gè)范圍內(nèi)最新的版本。
pod一些用法
pod 'Objection', :head使用指定spec下最新可用版本,會(huì)強(qiáng)制下載高風(fēng)險(xiǎn)版本
pod 'AFNetworking', :path => '~/Documents/AFNetworking'
pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git'
pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :branch => 'dev'
pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :tag => '0.7.0'
Target 語(yǔ)法
target 'name' do
pod xxx
end
指定target對(duì)應(yīng)的依賴庫(kù)
其他一些不常用的語(yǔ)法如:podspec、abstract_target、abstract!、inherit!。
Sources
Podfile從這里去搜索specs,全局定義,不能定義在單獨(dú)一個(gè)Target里。
編譯配置
platform
platform ios, '8.0' 或 platform (不填寫)
目前的默認(rèn)設(shè)置是iOS4.3、OS X10.6、tvOS 9.0、watchOS 2.0
use_frameworks!
該命令的作用是讓依賴庫(kù)使用framework的方式被主工程使用,否則是以靜態(tài)庫(kù)的形式;
framework:系統(tǒng)的framework是動(dòng)態(tài)庫(kù)(dylib),自己開發(fā)的framework是靜態(tài)庫(kù).
framework = 頭文件 + .a + 資源文件
用途:
當(dāng)OC和Swift庫(kù)互相引用的時(shí)候,原本需要使用XXX-Bridging-Header.h橋接文件來導(dǎo)入,使用framework方式則可以直接像本語(yǔ)言里導(dǎo)入頭文件方式import "AFNetwork/AFNetwork.h"
使用了use_frameworks!
use_frameworks!
未使用use_frameworks!(使用.a的靜態(tài)鏈接庫(kù)方式)
#use_frameworks!
其他不常用的還有inhibit_all_warnings!、link_with、project。
Hooks、Workspace、Root Options等內(nèi)容配置官方文檔。
- 依賴庫(kù)安裝
pod install
工程目錄
- 運(yùn)行
執(zhí)行.xcworkplace文件
- 頭文件導(dǎo)入
三、內(nèi)部詳解
文件組成說明
-
Podfile
描述本工程第三方庫(kù)的依賴關(guān)系
Podfile.lock 和 Pods/Manifest.lock
Podfile.lock描述了pod install之后各個(gè)依賴庫(kù)的具體版本,主要作用是在Podfile里未具體指定版本號(hào)的情況下,保證協(xié)同開發(fā)人員使用的版本一致;除非修改了Podfile文件或者使用pod update 'PodName',否則Podfile.lock不會(huì)更新。
舉個(gè)例子:
sequenceDiagram
A->>Podfile: pod install
Podfile->>Podfile.lock: 描述了準(zhǔn)備的依賴庫(kù)版本號(hào)
Podfile.lock->>Repo: 版本控制
B->>Repo: 拉去代碼, 包括Podfile、Podfile.lock;
B 從倉(cāng)庫(kù)拉下代碼,也執(zhí)行pod install,這個(gè)時(shí)候未具體指定版本號(hào)的依賴庫(kù)不再是拉去最新版本或者其他一些規(guī)則,而是根據(jù)Podfile.lock里的版本去下載依賴庫(kù),這樣就保證了大家使用的是同一個(gè)版本。
Manifest.lock是存放在Pods目錄下的,這個(gè)目錄一般不加入git版本控制;也是在pod install之后生成,是Podfile.lock的拷貝,主要作用是為了校驗(yàn)已經(jīng)安裝的依賴庫(kù)和Podfile.lock里的是否一致,保證大家使用相同的依賴版本。
pod install具體的執(zhí)行過程:
pod install --verbose 或者pod install --no-repo-update --verbose
Preparing
Analyzing dependencies
Inspecting targets to integrate
Using `ARCHS` setting to build architectures of target
`Pods-LiveStream_IM_Demo`: (``)
Finding Podfile changes
- Masonry
- RealReachability
- SVProgressHUD
- YYKeyboardManager
- YYModel
- YYText
- YYWebImage
Resolving dependencies of `Podfile`
Comparing resolved specification to the sandbox manifest
- Masonry
- RealReachability
- SVProgressHUD
- YYCache
- YYImage
- YYKeyboardManager
- YYModel
- YYText
- YYWebImage
Downloading dependencies
-> Using Masonry (1.0.2)
-> Using RealReachability (1.1.8)
-> Using SVProgressHUD (2.1.2)
-> Using YYCache (1.0.4)
-> Using YYImage (1.0.4)
-> Using YYKeyboardManager (1.0.1)
-> Using YYModel (1.0.4)
-> Using YYText (1.0.7)
-> Using YYWebImage (1.0.5)
- Running pre install hooks
Generating Pods project
- Creating Pods project
- Adding source files to Pods project
- Adding frameworks to Pods project
- Adding libraries to Pods project
- Adding resources to Pods project
- Linking headers
- Installing targets
- Installing target `Masonry` iOS 6.0
- Installing target `RealReachability` iOS 6.0
- Installing target `SVProgressHUD` iOS 7.0
- Installing target `YYCache` iOS 6.0
- Installing target `YYImage` iOS 6.0
- Installing target `YYKeyboardManager` iOS 6.0
- Installing target `YYModel` iOS 6.0
- Installing target `YYText` iOS 6.0
- Installing target `YYWebImage` iOS 6.0
- Installing target `Pods-LiveStream_IM_Demo` iOS 8.0
- Running post install hooks
- Writing Xcode project file to `Pods/Pods.xcodeproj`
- Generating deterministic UUIDs
- Writing Lockfile in `Podfile.lock`
- Writing Manifest in `Pods/Manifest.lock`
Integrating client project
Integrating target `Pods-LiveStream_IM_Demo` (`LiveStream_IM_Demo.xcodeproj` project)
- Running post install hooks
- cocoapods-stats from
`/Users/netease/.rvm/gems/ruby-2.3.0@global/gems/cocoapods-stats-1.0.0/lib/cocoapods_plugin.rb`
Sending stats
- Masonry, 1.0.2
- RealReachability, 1.1.8
- SVProgressHUD, 2.1.2
- YYCache, 1.0.4
- YYImage, 1.0.4
- YYKeyboardManager, 1.0.1
- YYModel, 1.0.4
- YYText, 1.0.7
- YYWebImage, 1.0.5
-> Pod installation complete! There are 7 dependencies from the Podfile and 9 total pods installed.
[!] Automatically assigning platform ios with version 8.0 on target LiveStream_IM_Demo because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.
- 'Inspecting targets to integrate',確定項(xiàng)目的架構(gòu);
- 'Finding Podfile changes',
- 'Resolving dependencies of
Podfile
', 解析Podfile文件 - 下載依賴庫(kù)文件
- 生成一個(gè)Pods.project
- 添加源文件
- 添加frameworkworks
- 靜態(tài)庫(kù)
- 資源
- 鏈接頭文件
- 對(duì)每個(gè)依賴庫(kù)在Pods.project中生成一個(gè)target
- 生成Podfile.lock文件
- 生成Pods/Manifest.lock文件
- 與原有的工程集成
pod outdated
當(dāng)你運(yùn)行pod outdated命令,CocoaPods會(huì)列出那些所有較Podfile.lock里面有新版本的庫(kù)(那些當(dāng)前被安裝著的庫(kù)的版本)。
pod update
當(dāng)你運(yùn)行 pod update PODNAME 命令時(shí),CocoaPods會(huì)幫你更新到這個(gè)庫(kù)的新版本,而不需要考慮Podfile.lock里面的限制,它會(huì)更新到這個(gè)庫(kù)盡可能的新版本,只要符合Podfile里面的版本限制。
如果你運(yùn)行pod update,后面沒有跟庫(kù)的名字,CocoaPods就會(huì)更新每一個(gè)Podfile里面的庫(kù)到盡可能的最新版本。
四、podspec制作
步驟
- 創(chuàng)建庫(kù)工程,添加git,
$ git add .
$ git commit -m ""
$ git push origin master
podspec文件中需要配置tag號(hào),所以需要打一個(gè)tag
$ git tag -m "comments" "0.1.2"
$ git push --tags
- 創(chuàng)建podspec配置文件
pod spec create Name
自動(dòng)生成Name.podspec文件
Pod::Spec.new do |s|
s.name = "iOS_Category"
s.version = "0.1.2"
s.summary = "all kinds of categories for iOS develop"
s.description = <<-DESC
this project provide all kinds of categories for iOS developer
DESC
s.homepage = "https://github.com/sevenuncler/iOS_Category"
s.license = "MIT"
s.license = { :type => "MIT", :file => "LICENSE" }
s.author = { "fanghe" => "fanghe@xxx.com" }
s.platform = :ios
s.source = { :git => "https://github.com/fanghe/iOS_Category.git", :tag => "0.0.1" }
s.source_files = "Classes", "iOS_Category/Classes/**/*.{h,m}"
s.exclude_files = "Classes/Exclude"
s.public_header_files = "iOS_Category/Classes/UIKit/UI_Categories.h","iOS_Category/Classes/Foundation/Foundation_Category.h","iOS_Category/Classes/**/*.h"
s.requires_arc = true
end
- 驗(yàn)證文件
pod lib lint
任何的編譯錯(cuò)誤、警告、源文件找不到、podspec文件缺少一些必須的配置項(xiàng)都會(huì)驗(yàn)證失敗;
如果有警告
$ pod lib lint --allow-warnings
如果有錯(cuò)誤可以添加--verbose查看具體的錯(cuò)誤信息
$ pod lib lint --verbose
驗(yàn)證通過:
- pod trunk上傳到庫(kù)配置文件到遠(yuǎn)程Cocoapods倉(cāng)庫(kù)
$ pod trunk register orta@cocoapods.org 'Orta Therox' --description='macbook pro'
首先需要使用個(gè)人郵箱注冊(cè)一個(gè)賬號(hào),在本地設(shè)備上建立一個(gè)session,不需要密碼,根據(jù)當(dāng)前注冊(cè)設(shè)備自動(dòng)提供session令牌。
$ pod trunk push Name.podspec
驗(yàn)證遠(yuǎn)程倉(cāng)庫(kù),成功之后將.podspec文件推送到Cocoapods官方庫(kù),這個(gè)過程比較耗時(shí)間;
- 查看自己的庫(kù)是否添加成功
$ pod search SUAdvancedPlayer
找不到自己寫的
rm ~/Library/Caches/CocoaPods/search_index.json
再次輸入
$ pod search SUAdvancePlayer
參考鏈接
http://www.lxweimin.com/p/a1d2d148fdd3
http://blog.jobbole.com/53365/
https://guides.cocoapods.org/syntax/podfile.html#group_target_configuration