1、Cocoapods安裝
通過RubyGems安裝CocoaPods笋庄。
打開Terminal,執(zhí)行以下步驟:
$ sudo gem install cocoapods
執(zhí)行完這句如果報(bào)告以下錯(cuò)誤:
ERROR: Could not find a valid gem 'cocoapods' (>= 0), here is why:Unable to download data from https://rubygems.org/ - Errno::ETIMEDOUT: Operation timed out - connect(2)(https://rubygems.org/latest_specs.4.8.gz)ERROR: Possible alternatives: cocoa pods
原因是:ruby的軟件源rubygems.org使用亞馬遜的云服務(wù)倔监,被屏蔽了直砂,需要更新一下ruby的源:
查看當(dāng)前ruby來源.
$ gem sources -l
$ gem sources --remove https://rubygems.org/
$ gem sources -a https://ruby.taobao.org/
$ gem sources -l
如果gem太老,可以嘗試升級(jí)gem:
$ sudo gem update --system
升級(jí)成功會(huì)有以下提示:
RubyGems system software updated
執(zhí)行安裝命令:
$ sudo gem install cocoapods
或安裝制定版本
$ sudo gem install cocoapods -v 0.39.0
- 若出現(xiàn)以下錯(cuò)誤:
ERROR: While executing gem ... (Errno::EPERM)
Operation not permitted - /usr/bin/pod
可嘗試以下命令安裝:
sudo gem install -n /usr/local/bin cocoapods
- 若提示以下錯(cuò)誤:
ERROR: Error installing cocoapods:activesupport requires Ruby version >= 2.2.2.
說明ruby版本過低浩习,需要升級(jí):
使用 ruby -v 查看當(dāng)前版本
并通過rvm升級(jí)ruby
- 安裝 RVM
參考:Mac上更新Ruby
$ curl -L get.rvm.io | bash -s stable
$ source ~/.bashrc
$ source ~/.bash_profile
測(cè)試:rvm -v
- 用RVM升級(jí)Ruby
RubyGems 鏡像
$ ruby -v ## 查看當(dāng)前ruby版本
$ rvm list known ## 列出版本
$ rvm install 1.9.3 ## 安裝ruby 1.9.3
若報(bào)以下錯(cuò)誤
則更新brew:brew update
$ brew outdated (查看哪些軟件可以升級(jí))
$ brew upgrade (升級(jí)可升級(jí)軟件)
$ brew cleanup (清理無用安裝包)
重新執(zhí)行: $ rvm install 1.9.3
重新執(zhí)行:$ sudo gem install cocoapods
$ pod setup ## 安裝
到此cocoapods安裝完成
- 使用pod創(chuàng)建項(xiàng)目
pod lib create ProjectName
- 查找三方庫(kù):
pod search AFNetworking
若提示:Unable to find a pod with name, author, summary, or descriptionmatching 静暂。。
則需要清理以下緩存:rm ~/Library/Caches/CocoaPods/search_index.json
注冊(cè)pod賬戶谱秽,上傳自制庫(kù)
- 查看當(dāng)前賬戶:
pod trunk me
若提示:[!]You need to register a session first.
說明沒有注冊(cè)cocoapods洽蛀,需要進(jìn)行注冊(cè):pod trunk register orta@cocoapods.org 'Orta Therox' --description='macbook air'
- 再次查看
pod trunk me
- 添加新成員(可更改、上傳pod庫(kù)):
pod trunk add-owner ARAnalytics kyle@cocoapods.org
- 移除成員:
pod trunk remove-owner 郵箱地址
2. Cocoapods制作
具體可參考文章1:http://blog.csdn.net/hytzxd/article/details/46652173
github上傳可參考我的這篇文章2:如何mac客戶端上傳github
官方文檔:公開pod Making a CocoaPod
私有pod Private Pods
① 創(chuàng)建github工程疟赊,拷貝文件庫(kù)
② 添加podspec文件
③ 提交git庫(kù)
④ 驗(yàn)證spec文件郊供,推送到pod的主干
⑤ 更新本地pod文件
① 在github自己賬號(hào)下,創(chuàng)建工程并clone到本地近哟,將庫(kù)文件拷貝至工程
② 添加podspec文件
podspec語法驮审。
創(chuàng)建podspec文件,文件名跟工程名稱相同
$ pod spec create EGMonitor ## 無需加后綴
spec配置文件解釋:
#
# Be sure to run `pod spec lint EGMonitor.podspec' to ensure this is a
# valid spec and to remove all comments including this before submitting the spec.
#
# To learn more about Podspec attributes see http://docs.cocoapods.org/specification.html
# To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/
#
Pod::Spec.new do |s|
# ――― Spec Metadata ――― #
# sepc文件的名字
s.name = "EGMonitor"
# pod對(duì)應(yīng)版本
s.version = "1.0.0"
#簡(jiǎn)單的介紹 會(huì)顯示到pod search中
s.summary = "EGMonitor 第二次修改文件."
# 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
# DESC
# 主頁
s.homepage = "https://github.com/ShaochongDu/EGMonitor"
# s.screenshots = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif"
# ――― Spec License ―――#
# 開源協(xié)議
s.license = "MIT"
# s.license = { :type => "MIT", :file => "FILE_LICENSE" }
# ――― 作者信息 ―――#
# 作者及郵箱
s.author = { "Running Du" => "dushaochong@icloud.com" }
# Or just: s.author = "Running Du"
# s.authors = { "Running Du" => "dushaochong@icloud.com" }
# s.social_media_url = "http://twitter.com/Running Du"
# ――― 平臺(tái)信息 ―――#
# 平臺(tái)及使用版本
# s.platform = :ios
s.platform = :ios, "7.0"
# When using multiple platforms
# s.ios.deployment_target = "5.0"
# s.osx.deployment_target = "10.7"
# s.watchos.deployment_target = "2.0"
# s.tvos.deployment_target = "9.0"
# ――― 資源信息 ―――#
# 資源位置吉执,注意后邊的tag 疯淫,其實(shí)就是他的版本,pods是以git為基礎(chǔ)管理的戳玫。版本就是tag值了
s.source = { :git => "https://github.com/ShaochongDu/EGMonitor.git", :tag => s.version }
# 若tag帶有前綴 "v" 則tag定義為:
s.source = { :git => "https://github.com/ShaochongDu/EGMonitor.git", :tag => "v#{s.version}" }
# ――― 源代碼 ―――#
# 源代碼位置峡竣,pod會(huì)以這個(gè)地址下載需要的文件構(gòu)建pods
s.source_files = "EGMonitor/**/*.{h,m}"
# 不包含資源
#s.exclude_files = "Classes/Exclude"
#公共的頭文件地址
# s.public_header_files = "AFNetworking/*.h" #靜態(tài)庫(kù)時(shí)必須有
# ――― 資源信息 ―――#
# s.resource = "icon.png"
# s.resources = "Resources/*.png"
# s.preserve_paths = "FilesToSave", "MoreFilesToSave"
# ――― 工程鏈接庫(kù) ―――#
# 引用系統(tǒng)公有framework:’(.ios).framework’. 用”,”分割. 去掉尾部的”.framework”
# s.framework = "SomeFramework"
# s.frameworks = "UIKit", "AnotherFramework"
# 引用系統(tǒng)靜態(tài)庫(kù):’(.ios).library’。去掉頭尾的lib量九,用”,”分割
s.library = "z"
# s.libraries = "iconv", "xml2"
# 引用自己生成的framework:’(.ios).vendored_frameworks’适掰。用”,”分割
# 路徑寫從.podspec所在目錄為根目錄的相對(duì)路徑 ps:這個(gè)不要省略.framework
# s.vendored_frameworks = 'AnalysysSDK/IDFA/*.framework'
# 引用自己生成的.a靜態(tài)庫(kù)(必須以lib打頭)
s.vendored_libraries = "libEGMonitor/libEGMonitor.a"
# ――― 工程配置 ――― #
# 是否是arc模式
s.requires_arc = true
# s.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }
# 依賴的第三方類庫(kù)
# s.dependency "JSONKit", "~> 1.4"
# ――― 類庫(kù)依賴 ――― #
# 組件化中經(jīng)常用到颂碧,將每個(gè)功能拆分為一個(gè)庫(kù),參考[subspec說明](http://www.reibang.com/p/85c97dc9ab83)类浪,如 afnetworking
s.subspec 'Serialization' do |ss|
ss.source_files = 'AFNetworking/AFURL{Request,Response}Serialization.{h,m}'
ss.public_header_files = 'AFNetworking/AFURL{Request,Response}Serialization.h'
ss.watchos.frameworks = 'MobileCoreServices', 'CoreGraphics'
ss.ios.frameworks = 'MobileCoreServices', 'CoreGraphics'
ss.osx.frameworks = 'CoreServices'
end
# Podfile中配置:
# pod 'AFNetworking', '~> 3.0', :subspecs => ["Serialization", "Reachability"]
end
③ 提交git庫(kù)
git add * # 添加本地git庫(kù)
git commit -m 'add tag' #提交git
git push origin master # push
git tag -a v1.0 -m "version 0.1 released" # 添加標(biāo)簽及備注
git push origin v1.0/git push --tags # 提交本地所有標(biāo)簽
④ 驗(yàn)證spec文件
根據(jù)錯(cuò)誤提示來修改
$ pod lib lint ## 不需要聯(lián)網(wǎng)
或
$ pod spec lint ## 會(huì)聯(lián)網(wǎng)檢查sepc repo并關(guān)聯(lián)tag
-- 多個(gè)xcode應(yīng)用载城,檢查報(bào)錯(cuò)如下,需要指定xcode费就,默認(rèn)指定安裝列表中xcode sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer
xcrun: error: active developer path ("/Users/shaochongdu/Downloads/Xcode.app/Contents/Developer") does not exist .....
-- 首次發(fā)布cocoapods需要添加tag: git tag '版本號(hào)'
-- 執(zhí)行時(shí)關(guān)閉.xcworkspace文件
-- 若報(bào)錯(cuò)如下圖诉瓦,則檢查靜態(tài)庫(kù)文件名是否以lib***.a開頭(必須以lib開頭)
如下報(bào)錯(cuò):
Updating spec repo `trunk`
Validating podspec
-> xxx (4.4.0)
- ERROR | [iOS] file patterns: The `resources` pattern did not match any file.
- ERROR | [iOS] file patterns: The `vendored_frameworks` pattern did not match any file.
- NOTE | xcodebuild: note: Using new build system
- NOTE | [iOS] xcodebuild: note: Planning build
- NOTE | [iOS] xcodebuild: note: Constructing build description
- NOTE | [iOS] xcodebuild: warning: Skipping code signing because the target does not have an Info.plist file. (in target 'App')
[!] The spec did not pass validation, due to 2 errors.
產(chǎn)生原因:第一次推送git時(shí)文件中忘記放入相應(yīng)類庫(kù)文件,導(dǎo)致檢查時(shí)無法找到力细,第二次修改后push-tag睬澡,但扔未找到,可能pod存在緩存
解決方案:刪除cache文件/Users/xxx/Library/Caches/CocoaPods/Pods
眠蚂,重新執(zhí)行 pod spec lint
把項(xiàng)目推送到spec目錄
$ pod trunk push EGMonitor.podspec
若執(zhí)行失敗如下圖煞聪,則更新本地的pod文件:$ pod repo update
⑤ 搜索pod庫(kù)
$ pod search EGMonitor
若提示:Unable to find a pod with name, author, summary, or descriptionmatching 。逝慧。
清理本地緩存昔脯,重新search:
$ rm ~/Library/Caches/CocoaPods/search_index.json
⑥ trunk操作
$ pod --version
$ pod trunk deprecate EGMonitor ## pods所有版本將失效
成功后提示:
[!] No pod found with the specified name.
# 刪除指定版本,版本號(hào)必須大于1.0笛臣。從主干和主規(guī)范回購(gòu)中刪除指定的pod版本云稚。一旦刪除,這個(gè)版本將永遠(yuǎn)不能再被推送沈堡。静陈,圖3
$ pod trunk delete EGMonitor 1.0.0
成功后提示:
[!] No pod found with the specified name.
刪除后,需要pod repo一下诞丽,重新search
$ pod repo update
成功后如 圖1
$ pod search EGMonitor
以上刪除有時(shí)需要執(zhí)行多次不知為何鲸拥,刪除后再次查詢?nèi)?圖2
Cocoapods使用
官方文檔 :
Using CocoaPods
pod install vs. pod update
The Podfile
Podfile Syntax Reference
- 檢查是否含有pods文件
$ pod search AFNetworking
- 命令行到工程主目錄下,新建一個(gè)名為Podfile的文件率拒,文件名必須為“Podfile”崩泡,該文件的作用是配置依賴庫(kù)信息禁荒,就是告訴CocoaPods去下載和管理哪些依賴庫(kù)猬膨,文件創(chuàng)建好以后,打開文件并加入如下內(nèi)容
$ vim Podfile
簡(jiǎn)易版呛伴,復(fù)雜版請(qǐng)看官方文檔
platform :ios, ‘7.0’
target 'EGMonitorDemo' do
pod 'EGMonitor'
pod 'AFNetworking', '~> 3.0'
end
- 安裝
安裝前關(guān)閉當(dāng)前工程文件
在Podfile目錄下執(zhí)行
$ pod install ## 次命令回檢查specs庫(kù)并升級(jí)較為漫長(zhǎng)
可以使用如下命令勃痴,跳過specs庫(kù)檢查
$ pod install --verbose --no-repo-update
或
$ pod install --repo-update
查看是否有新版本
$ pod outdated --verbose --no-repo-update
單獨(dú)更新某個(gè)pod
$ pod update AFNetworking
更新所有比當(dāng)前版本高的pod第三方類庫(kù)
$ pod update --verbose --no-repo-update
完成之后,重新打開.xcworkspace工程即可