原文连接html
CocoaPods是OS X和iOS下的一个第三类库管理工具,经过CocoaPods工具咱们能够为项目添加被称为“Pods”的依赖库(这些类库必须是CocoaPods自己所支持的),而且能够轻松管理其版本。react
使用CocoaPods有如下几点好处:ios
sudo gem install cocoapods
复制代码
pod --version
复制代码
在开发中安装使用cocoapods要注意版本,由于通常开发过程当中要你们一块儿使用同一个工程,通常为了指定版本咱们会在工程下建立Gemfile来指定使用cocoapods的版本。git
除了指定Gemfile之外 , 咱们还能够安装指定版本的podsgithub
sudo gem install cocoapods -v 1.3.1
复制代码
再查看一下pod版本咱们就会发现已经安装了1.3.1swift
当咱们本地同时存在多个版本的pod的时候能够把多余的卸载掉xcode
sudo gem uninstall cocoapods
复制代码
会提示咱们选择卸载的版本安全
Select gem to uninstall:
1. cocoapods-1.2.1
2. cocoapods-1.3.1
3. All versions
>
复制代码
咱们选择想要卸载的版本的序号就行了 。ruby
cocoapods支持咱们去查找想要使用的仓库 , 好比咱们想查找ReactoveObjC这个库bash
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
复制代码
Cocoapods极大方便了咱们管理外部SDK和内部模块化解耦,用好这个管理工具无疑会为咱们的工做效率带来巨大提高。