CocoaPods簡(jiǎn)介
CocoaPods是是一個(gè)管理項(xiàng)目中用到的第三方開源代碼的工具,使用目的是為了能集中管理和整合第三方開源庫。
安裝方法
1.在終端輸入以下命令進(jìn)行安裝
$ sudo gem install cocoapods
2.如果沒有翻墻可以將源換成淘寶的來解決這個(gè)問題
$ gem sources --remove https://rubygems.org/
$ gem sources -a http://ruby.taobao.org/
3.驗(yàn)證方法Ruby鏡像是并且僅是taobao,可以用以下命令查看
$ gem sources -l
在終端中顯示一下提示 說明安裝成功
$ CURRENT SOURCES
$ https://ruby.taobao.org/
16.12.26更新
注:如果淘寶源不可用可以將其替換為https://gems.ruby-china.org/
$ gem sources --add https://gems.ruby-china.org/ --remove https://ruby.taobao.org/
4.替換完ruby后 再次執(zhí)行以下命令
$ sudo gem install cocoapods
如果沒有錯(cuò)誤提示則跳過以下部分
問題:出現(xiàn)ERROR: While executing gem ... (Errno::EPERM)
Operation not permitted - /usr/bin/xcodeproj錯(cuò)誤提示
解決:用以下命令執(zhí)行
$ sudo gem install -n /usr/local/bin cocoapods
因?yàn)樗许?xiàng)目的Podspec文件都托管在 https://github.com/CocoaPods/Specs 第一次執(zhí)行 pod setup時(shí),CocoaPods會(huì)將這些podspec索引文件更新到本地的 ~/.cocoapods/目錄下,這個(gè)索引文件比較大會(huì)比較慢,最后安裝完成后可以在Finder 中查找到以下目錄 /Users/用戶名/.cocoapods
如果順利可以在得到如下圖所示的結(jié)果,Specs內(nèi)有很多第三方庫目錄。
Xcode中的使用方法
1.終端命令打開項(xiàng)目文件夾并創(chuàng)建Podfile
$ cd Desktop/項(xiàng)目文件夾
$ touch Podfile
$ open Podfile
2.添加內(nèi)容,完成后保存。
platform :ios, '7.0'
pod 'SBJson', '~> 4.0.0
3.安裝pod
pod install
如果在安裝時(shí)提示"The dependency 'xxx' is not used in any concrete target" 就需要指明 target。
如下修改Podfile文件
platform :ios, '7.0'
target "test" do
pod 'SBJson', '~> 4.0.0
end
如果有多個(gè)target則需要用到abstract_target方法
一.區(qū)分target使用
Note: There are no targets called "Shows" in any Xcode project
sabstract_target 'Shows' do
pod 'ShowsKit'
pod 'Fabric'
Has its own copy of ShowsKit + ShowWebAuth target 'ShowsiOS' do
pod 'ShowWebAuth'
end
Has its own copy of ShowsKit + ShowTVAuth
target 'ShowsTV' do
pod 'ShowTVAuth'
end
end
二.不區(qū)分target 多個(gè)target使用同一個(gè)podfile
sabstract_target 'Defaults' do
pod 'CorePlot', '~> 2.0' #繪制數(shù)據(jù)統(tǒng)計(jì)圖
pod 'pop', '~> 1.0' #Animation
pod 'Masonry' #自動(dòng)布局
target 'ShowsiOS-inhouse-test'
target 'ShowsiOS-inhouse-test copy'
target 'ShowsiOS-inhouse-dev'
end
首次安裝需要耐心等待 因?yàn)闀?huì)很慢 安裝結(jié)果
項(xiàng)目工程目錄中會(huì)多出以下文件
workspace下多個(gè)工程的Pods如何設(shè)置
首先在桌面新建一個(gè)目錄名為MyProject, 目錄下新建兩個(gè)工程名為TestO和TestT
打開終端輸入如下
$ cd Desktop/MyProject
$ touch Podfile
$ open Podfile
在Podfile中加入如下內(nèi)容
然后在終端中輸入
$ pod Install
安裝成功后的目錄
發(fā)布代碼到CocoaPods
1.創(chuàng)建自己的Pod
$ pod lib create PHMKeyboard
HMMacBook-Pro:CocoaPods phm$ pod lib create PHMKeyboard
Cloning `https://github.com/CocoaPods/pod-template.git` into `PHMKeyboard`.
Configuring PHMKeyboard template.
------------------------------
To get you started we need to ask a few questions, this should only take a minute.
If this is your first time we recommend running through with the guide:
- http://guides.cocoapods.org/making/using-pod-lib-create.html
( hold cmd and double click links to open in a browser. )
What language do you want to use?? [ Swift / ObjC ]
> ObjC
Would you like to include a demo application with your library? [ Yes / No ]
>
yes
Which testing frameworks will you use? [ Specta / Kiwi / None ]
> None
Would you like to do view based testing? [ Yes / No ]
> NO
What is your class prefix?
> PHM
Running pod install on your new library.
Analyzing dependencies
Fetching podspec for `PHMKeyboard` from `../`
Downloading dependencies
Installing PHMKeyboard (0.1.0)
Generating Pods project
Integrating client project
[!] Please close any current Xcode sessions and use `PHMKeyboard.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
Ace! you're ready to go!
We will start you off by opening your project in Xcode
open 'PHMKeyboard/Example/PHMKeyboard.xcworkspace'
To learn more about the template see `https://github.com/CocoaPods/pod-template.git`.
To learn more about creating a new pod, see `http://guides.cocoapods.org/making/making-a-cocoapod`.
2.主要修改PHMKeyboard.podspec文件中的s.homepage,s.source,s.frameworks參數(shù)
#
# Be sure to run `pod lib lint PHMKeyboard.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 http://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
s.name = 'PHMKeyboard' #名稱
s.version = '0.1.0' #版本號(hào)
s.summary = 'A short description of PHMKeyboard.'#簡(jiǎn)短介紹
# 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
s.homepage = 'https://github.com/<GITHUB_USERNAME>/PHMKeyboard'#主頁
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'#截圖
s.license = { :type => 'MIT', :file => 'LICENSE' }#協(xié)議
s.author = { 'PHM' => '251962881@qq.com' }//作者信息
s.source = { :git => 'https://github.com/<GITHUB_USERNAME>/PHMKeyboard.git', :tag => s.version.to_s }#項(xiàng)目地址
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'#多媒體介紹地址
s.ios.deployment_target = '8.0'#支持版本
s.source_files = 'PHMKeyboard/Classes/**/*'#代碼源文件地址
# s.resource_bundles = {
# 'PHMKeyboard' => ['PHMKeyboard/Assets/*.png']
# }#資源文件地址
# s.public_header_files = 'Pod/Classes/**/*.h'#公開頭文件地址
s.frameworks = 'UIKit', 'Foundation'#所需的framework,多個(gè)用逗號(hào)隔開
# s.dependency 'AFNetworking', '~> 2.3'#依賴關(guān)系,該項(xiàng)目所依賴的其他庫,
end
3.添加文件后把本地初始化pod并提交到Github
4.初始化pod:
$ pod lib lint --allow-warnings
HMMacBook-Pro:PHMKeyboard phm$ pod lib lint --allow-warnings
-> PHMKeyboard (0.1.0)
- WARN | summary: The summary is not meaningful.
PHMKeyboard passed validation.
5.注冊(cè)CocoaPods并提交pod
HMMacBook-Pro:PHMKeyboard phm$ pod trunk register 251962881@qq.com 'phm' --description='macbook pro'
[!] Please verify the session by clicking the link in the verification email that has been sent to 251962881@qq.com
6.打開郵件點(diǎn)擊連接出現(xiàn)如下信息
7.部署自己的pod 上傳podspec文件
在上傳之前,先打個(gè)tag標(biāo)記,然后再驗(yàn)證下podspec文件是否正確
創(chuàng)建tag
$ git tag '1.0.0'
$ git push --tags
刪除tag方法
//查看tag
$ git tag
//刪除一個(gè)指定的tag,并上傳一個(gè)空tag到遠(yuǎn)程tag
git tag -d <tagName>
git push origin :refs/tags/< tagName >
版本號(hào)需與podspec文件中的版本號(hào)統(tǒng)一
否則會(huì)版本錯(cuò)誤
驗(yàn)證Podspec
$ pod lib lint
最后執(zhí)行Pod部署
$ pod trunk push PHMKeyboard.podspec --allow-warnings
如果出現(xiàn)ERROR | [iOS] The source_files
pattern did not match any file.提示
解決方法:修改PHMKeyboard.podspec文件中source 添加commit。
s.source = { :git => 'https://github.com/callHM/PHMKeyboard.git', :commit => '878d6c0bf8fa2b6cbe790afa566a7fcd15439f5c'}
s.source_files = 'PHMKeyboard/Classes/*.{h,m}'
HMMacBook-Pro:PHMKeyboard phm$ pod trunk push PHMKeyboard.podspec --allow-warnings
Updating spec repo `master`
CocoaPods 1.2.0.beta.1 is available.
To update use: `sudo gem install cocoapods --pre`
[!] This is a test version we'd love you to try.
For more information, see https://blog.cocoapods.org and the CHANGELOG for this version at https://github.com/CocoaPods/CocoaPods/releases/tag/1.2.0.beta.1
Validating podspec
-> PHMKeyboard (1.0.0)
- WARN | summary: The summary is not meaningful.
Updating spec repo `master`
CocoaPods 1.2.0.beta.1 is available.
To update use: `sudo gem install cocoapods --pre`
[!] This is a test version we'd love you to try.
For more information, see https://blog.cocoapods.org and the CHANGELOG for this version at https://github.com/CocoaPods/CocoaPods/releases/tag/1.2.0.beta.1
--------------------------------------------------------------------------------
?? Congrats
?? PHMKeyboard (1.0.0) successfully published
?? December 26th, 09:29
?? https://cocoapods.org/pods/PHMKeyboard
?? Tell your friends!
--------------------------------------------------------------------------------
驗(yàn)證方法
pod spec lint PHMKeyboard.podspec --allow-warnings
HMMacBook-Pro:PHMKeyboard phm$ pod spec lint PHMKeyboard.podspec --allow-warnings
-> PHMKeyboard (1.0.0)
- WARN | summary: The summary is not meaningful.
Analyzed 1 podspec.
PHMKeyboard.podspec passed validation.```
更新自己的pod
先修改完代碼之后,提交到Github并push到CocoaPods:
>$ git tag '0.1.1'
$ git push --tags
$ pod trunk push PHMKeyboard.podspec --allow-warnings
查看CocoaPods上的個(gè)人信息
>pod trunk me
HMMacBook-Pro:PHMKeyboard phm$ pod trunk me
- Name: phm
- Email: 251962881@qq.com
- Since: December 26th, 08:03
- Pods:
- PHMKeyboard
- Sessions:
- December 26th, 08:03 - May 3rd, 2017 09:30.
查看庫在CocoaPods上能否被找到
>$ pod search PHMKeyboard
##其它命令及相關(guān)內(nèi)容
---
1.查看版本信息
>$ pod --version
2.卸載cocoaPods
>$ sudo gem uninstall cocoa pods
3.CocoaPods在Xcode上有插件 `Xcode8.0后不可用`
[XCode插件管理工具alcatraz](http://alcatraz.io)

##擴(kuò)展閱讀
---
http://www.lxweimin.com/p/ba56c628d190
http://blog.csdn.net/keleyundou/article/details/49635589