CocoaPods简介
CocoaPods是是一个管理项目中用到的第三方开源代码的工具,使用目的是为了能集中管理和整合第三方开源库。
安装方法
1.在终端输入以下命令进行安装
$ sudo gem install cocoapods
2.如果没有翻墙可以将源换成淘宝的来解决这个问题
$ gem sources --remove https://rubygems.org/
$ gem sources -a http://ruby.taobao.org/
3.验证方法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后 再次执行以下命令
$ sudo gem install cocoapods
如果没有错误提示则跳过以下部分
问题:出现ERROR: While executing gem ... (Errno::EPERM)
Operation not permitted - /usr/bin/xcodeproj错误提示
解决:用以下命令执行
$ sudo gem install -n /usr/local/bin cocoapods
因为所有项目的Podspec文件都托管在 https://github.com/CocoaPods/Specs 第一次执行 pod setup时,CocoaPods会将这些podspec索引文件更新到本地的 ~/.cocoapods/目录下,这个索引文件比较大会比较慢,最后安装完成后可以在Finder 中查找到以下目录 /Users/用户名/.cocoapods
如果顺利可以在得到如下图所示的结果,Specs内有很多第三方库目录。
Xcode中的使用方法
1.终端命令打开项目文件夹并创建Podfile
$ cd Desktop/项目文件夹
$ touch Podfile
$ open Podfile
2.添加内容,完成后保存。
platform :ios, '7.0'
pod 'SBJson', '~> 4.0.0
3.安装pod
pod install
如果在安装时提示"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
如果有多个target则需要用到abstract_target方法
一.区分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
二.不区分target 多个target使用同一个podfile
sabstract_target 'Defaults' do
pod 'CorePlot', '~> 2.0' #绘制数据统计图
pod 'pop', '~> 1.0' #Animation
pod 'Masonry' #自动布局
target 'ShowsiOS-inhouse-test'
target 'ShowsiOS-inhouse-test copy'
target 'ShowsiOS-inhouse-dev'
end
首次安装需要耐心等待 因为会很慢 安装结果
项目工程目录中会多出以下文件
workspace下多个工程的Pods如何设置
首先在桌面新建一个目录名为MyProject, 目录下新建两个工程名为TestO和TestT
打开终端输入如下
$ cd Desktop/MyProject
$ touch Podfile
$ open Podfile
在Podfile中加入如下内容
然后在终端中输入
$ pod Install
安装成功后的目录
发布代码到CocoaPods
1.创建自己的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参数
#
# 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' #版本号
s.summary = 'A short description of PHMKeyboard.'#简短介绍
# 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
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' }#协议
s.author = { 'PHM' => '251962881@qq.com' }//作者信息
s.source = { :git => 'https://github.com/<GITHUB_USERNAME>/PHMKeyboard.git', :tag => s.version.to_s }#项目地址
# 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,多个用逗号隔开
# s.dependency 'AFNetworking', '~> 2.3'#依赖关系,该项目所依赖的其他库,
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.注册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.打开邮件点击连接出现如下信息
7.部署自己的pod 上传podspec文件
在上传之前,先打个tag标记,然后再验证下podspec文件是否正确
创建tag
$ git tag '1.0.0'
$ git push --tags
删除tag方法
//查看tag
$ git tag
//删除一个指定的tag,并上传一个空tag到远程tag
git tag -d <tagName>
git push origin :refs/tags/< tagName >
版本号需与podspec文件中的版本号统一
否则会版本错误
验证Podspec
$ pod lib lint
最后执行Pod部署
$ pod trunk push PHMKeyboard.podspec --allow-warnings
如果出现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!
--------------------------------------------------------------------------------
验证方法
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上的个人信息
>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
##其它命令及相关内容
---
1.查看版本信息
>$ pod --version
2.卸载cocoaPods
>$ sudo gem uninstall cocoa pods
3.CocoaPods在Xcode上有插件 `Xcode8.0后不可用`
[XCode插件管理工具alcatraz](http://alcatraz.io)
![](http://upload-images.jianshu.io/upload_images/2529977-d6a6ba2462c950f3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
##扩展阅读
---
http://www.jianshu.com/p/ba56c628d190
http://blog.csdn.net/keleyundou/article/details/49635589