CocoaPods是OS X和iOS下的一个第三类库管理工具,经过CocoaPods工具咱们能够为项目添加被称为“Pods”的依赖库(这些类库必须是CocoaPods自己所支持的),而且能够轻松管理其版本。html
使用CocoaPods有如下几点好处:react
sudo gem install cocoapods
pod --version
在开发中安装使用cocoapods要注意版本,由于通常开发过程当中要你们一块儿使用同一个工程,通常为了指定版本咱们会在工程下建立Gemfile来指定使用cocoapods的版本。ios
除了指定Gemfile之外 , 咱们还能够安装指定版本的podsgit
sudo gem install cocoapods -v 1.3.1
再查看一下pod版本咱们就会发现已经安装了1.3.1github
当咱们本地同时存在多个版本的pod的时候能够把多余的卸载掉swift
sudo gem uninstall cocoapods
会提示咱们选择卸载的版本xcode
Select gem to uninstall: 1. cocoapods-1.2.1 2. cocoapods-1.3.1 3. All versions >
咱们选择想要卸载的版本的序号就行了 。缓存
有时咱们须要不一样的ruby环境,并且不想更改系统自带的时候能够考虑使用rvm管理本地的ruby版本安全
\curl -sSL https://get.rvm.io | bash -s stable
查看可用的ruby版本ruby
rvm list known
使用某个制定版本做为默认的ruby版本
rvm use 2.6.3 --default
gem install bundler
而后在工程目录下建立Gemfile
source 'https://rubygems.org' gem "cocoapods", "1.7.5"
再次使用的时候就可使用如下方式进行包更新操做了,这样在咱们同一ruby环境下也可使用不一样版本的pod
bundle exec pod install
cocoapods支持咱们去查找想要使用的仓库 , 好比咱们想查找ReactoveObjC这个库
pod spec cat ReactiveObjC
咱们能够看到该仓库的配置信息。
{ "name": "ReactiveObjC", "version": "3.1.0", "summary": "The 2.x ReactiveCocoa Objective-C API: Streams of values over time", "description": "ReactiveObjC (formally ReactiveCocoa or RAC) is an Objective-C\nframework inspired by [Functional Reactive Programming](\nhttp://en.wikipedia.org/wiki/Functional_reactive_programming).\nIt provides APIs for composing and **transforming streams of values**.", "homepage": "https://reactivecocoa.io", "screenshots": "https://reactivecocoa.io/img/logo.png", "license": { "type": "MIT", "file": "LICENSE.md" }, "documentation_url": "https://github.com/ReactiveCocoa/ReactiveObjC/tree/master/Documentation#readme", "authors": "ReactiveCocoa", "social_media_url": "https://twitter.com/ReactiveCocoa", "platforms": { "ios": "8.0", "osx": "10.9", "watchos": "2.0", "tvos": "9.0" }, "source": { "git": "https://github.com/ReactiveCocoa/ReactiveObjC.git", "tag": "3.1.0" }, "source_files": [ "ReactiveObjC/*.{h,m,d}", "ReactiveObjC/extobjc/*.{h,m}" ], "private_header_files": [ "**/*Private.h", "**/*EXTRuntimeExtensions.h", "**/RACEmpty*.h" ], "ios": { "exclude_files": "ReactiveObjC/**/*{AppKit,NSControl,NSText,NSTable}*" }, "osx": { "exclude_files": "ReactiveObjC/**/*{UIActionSheet,UIAlertView,UIBarButtonItem,UIButton,UICollectionReusableView,UIControl,UIDatePicker,UIGestureRecognizer,UIImagePicker,UIRefreshControl,UISegmentedControl,UISlider,UIStepper,UISwitch,UITableViewCell,UITableViewHeaderFooterView,UIText,MK}*" }, "tvos": { "exclude_files": "ReactiveObjC/**/*{AppKit,NSControl,NSText,NSTable,UIActionSheet,UIAlertView,UIDatePicker,UIImagePicker,UIRefreshControl,UISlider,UIStepper,UISwitch,MK}*" }, "watchos": { "exclude_files": "ReactiveObjC/**/*{UIActionSheet,UIAlertView,UIBarButtonItem,UIButton,UICollectionReusableView,UIControl,UIDatePicker,UIGestureRecognizer,UIImagePicker,UIRefreshControl,UISegmentedControl,UISlider,UIStepper,UISwitch,UITableViewCell,UITableViewHeaderFooterView,UIText,MK,AppKit,NSControl,NSText,NSTable,NSURLConnection}*"
首先 , 新建一个singlgViewApp ,而后在命令行进入该project目录
pod init
咱们能够看到cocoapods为咱们生成了一个Podfile
platform表示这个工程安装的设备,后面是系统最低版本
如今咱们能够在里面添加一下刚才搜索的仓库
# Uncomment the next line to define a global platform for your project platform :ios, '9.0' target 'ocTest' do # Uncomment the next line if you're using Swift or would like to use dynamic frameworks # use_frameworks! pod 'ReactiveObjC' # Pods for ocTest end
这里咱们添加了一个仓库,接下来再命令行执行pod update来安装所须要的仓库,安装完毕后咱们能够看到当前路径下有两个工程文件
咱们在使用了cocoapods管理第三方库的时候,每次install或者update的时候就会生成*.xcworkspace这个文件咱们须要使用这个工程进行开发调试。
如今打开工程,定位到viewController.m中就能够import并使用ReactiveObjC啦 。
公司内部有本身搭建的gitlab服务时,有的公司搭建的gitlab服务为了安全并无提供外网接口,咱们只能在内网访问,这时候就要在podfile中添加私有源的地址
source 'http://xx.xxxx.com/ios/cocoapods-spec.git' source 'https://github.com/CocoaPods/Specs.git' # 官方库
上面那个就是咱们要添加的私有源地址,下面的是官方源地址,若是都不写的话那么默认就会使用官方源 。
pod lib create TDFCommonUtil
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: - https://guides.cocoapods.org/making/using-pod-lib-create.html ( hold cmd and click links to open in a browser. ) What platform do you want to use?? [ iOS / macOS ] > ios What language do you want to use?? [ Swift / ObjC ] > swift Would you like to include a demo application with your library? [ Yes / No ] > yes Which testing frameworks will you use? [ Quick / None ] > None Would you like to do view based testing? [ Yes / No ] > NO
如今咱们须要添加一些信息
cd进入刚才建立的目录,执行命令 ls
能够看到控制台有如下输出。
Example README.md TDFCommonUtil.podspec LICENSE TDFCommonUtil _Pods.xcodeproj
解释一下这里都是啥
咱们首先要为咱们的pod建立一个实际的git仓库 。
打开gitlab服务建立一个空的git仓库 而后先把pod目录提交到咱们的仓库 blalblabla
打开TDFCommonUtil.podspec文件
Pod::Spec.new do |s| s.name = 'TDFCommonUtil' s.version = '0.1.0' s.summary = 'A short description of TDFCommonUtil.' s.description = <<-DESC TODO: Add long description of the pod here. DESC s.homepage = 'https://github.com/xxxxx@yeah.net/TDFCommonUtil' s.license = { :type => 'MIT', :file => 'LICENSE' } s.author = { 'xxxxx' => 'xxxxx@2dfire.com' } s.source = { :git => 'https://github.com/xxxxx@yeah.net/TDFCommonUtil.git', :tag => s.version.to_s } s.ios.deployment_target = '8.0' s.source_files = 'TDFCommonUtil/Classes/**/*' # s.resource_bundles = { # 'TDFCommonUtil' => ['TDFCommonUtil/Assets/*.png'] # } # s.public_header_files = 'Pod/Classes/**/*.h' # s.frameworks = 'UIKit', 'MapKit' # s.dependency 'AFNetworking', '~> 2.3' end
这里的配置项就是生成pod须要配置的大部分选项了 ,简单介绍一下
如今把这些填好吧 。
有的时候咱们开发的pod仓库须要依赖其余仓库,好比咱们须要依赖ReactiveObjC
这里就能够在TDFCommonUtil.podspec下面添加这一行
s.dependency 'ReactiveObjC'
ps ,这里能够指向特定的版本也能够用 '~> 2.3' 的形式表示依赖此仓库至少大于2.3版本可是不会超过3.0 。
一切就绪后你能够在里面建立你的文件,添加代码了,别忘了再有文件的增长或删除后在运行一遍 ‘pod install’。
先随便建立几个文件,而后咱们使用cocoapods检测咱们的库
pod lib lint --sources='git@git.xxx.com:ios/cocoapods-spec.git' --use-libraries --allow-warnings --verbose --no-clean
这里的sources填写你所使用的私有gitlab服务,而后咱们就能够静静的看着命令行了。
最后若是出现
Test passed validation.
证实你的库是可运行的,若是没有出现passed就注意下输出中的error信息,搜索一下error看是什么致使的 。
lint经过后咱们就能够把本身的仓库信息推送到私有源了,注意不是「仓库」是「仓库信息」,也就是x.podspec 。
cocoapod能够自动帮咱们完成这件事情
pod repo push xxx-cocoapods-spec TDFOpenShopSDK.podspec --sources=git@git.xxx.com:ios/cocoapods-spec.git --allow-warnings --use-libraries --verbose
删除指定Pod的缓存
pod cache clean [NAME]
删除所有缓存
pod cache clean --all
pod deintegrate
pod env
能够看到Git,ruby,Xcode版本等信息
### Stack
CocoaPods : 1.7.5
Ruby : ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin18] RubyGems : 3.0.4 Host : Mac OS X 10.14.6 (18G87) Xcode : 10.3 (10G8) Git : git version 2.20.1 (Apple Git-117)
Ruby lib dir : /Users/felix/.rvm/rubies/ruby-2.6.3/lib
Repositories : 2dfire-cocoapods-spec - git@git.2dfire.net:ios/cocoapods-spec.git @ cfd5c16d38593af16fdaa4bf1bebcf47f14b801d
2dfire-cocoapods-spec-binary - git@git.2dfire.net:ios/cocoapods-spec-binary.git @ d07163e6b83c0fbfc71fe43a68dd2fce5f51d2b9 2dfire-ios-cocoapods-spec - http://git.2dfire.net/ios/cocoapods-spec @ cfd5c16d38593af16fdaa4bf1bebcf47f14b801d master - https://github.com/CocoaPods/Specs.git @ f21043d7a7fd59154e6cae2ef819d725de394cfa
### Installation Source
Executable Path: /Users/felix/.rvm/gems/ruby-2.6.3/bin/pod
### Plugins
cocoapods-deintegrate : 1.0.4
cocoapods-plugins : 1.0.0
cocoapods-search : 1.0.0
cocoapods-stats : 1.1.0
cocoapods-trunk : 1.3.1
cocoapods-try : 1.1.0
也就是预览模式,在有多个实例工程可用的时候会让用户选择预览哪个
pod try YYModel
须要用到 cocoapods-packager
gem install cocoapods-packager
pod package XXX.podspec --force --dynamic --no-mangle --spec-sources=https://github.com/CocoaPods/Specs.git
具体参数解释
Usage: $ pod package NAME [SOURCE] Package a podspec into a static library. Options: --force Overwrite existing files. --no-mangle Do not mangle symbols of depedendant Pods. --embedded Generate embedded frameworks. --library Generate static libraries. --dynamic Generate dynamic framework. --bundle-identifier Bundle identifier for dynamic framework --exclude-deps Exclude symbols from dependencies. --configuration Build the specified configuration (e.g. Debug). Defaults to Release --subspecs Only include the given subspecs --spec-sources=private,https://github.com/CocoaPods/Specs.git The sources to pull dependant pods from (defaults to https://github.com/CocoaPods/Specs.git)