原文链接CocoaPods是OS X和iOS下的一个第三类库管理工具,通过CocoaPods工具我们可以为项目添加被称为“Pods”的依赖库(这些类库必须是CocoaPods本身所支持的),并且可以轻松管理其版本。使用CocoaPods有以下几点好处:在引入第三方库时它可以自动为我们完成各种各样的配置,包括配置编译阶段、连接器选项、甚至是ARC环境下的一些配置等。使用CocoaPods可以很方便地管理的第三方SDK,大部分稳定好用的SDK都支持cocoapods导入。在项目模块化的过程中方便我们模块间解耦。Installsudo gem install cocoapods查看版本pod –version在开发中安装使用cocoapods要注意版本,因为一般开发过程中要大家一起使用同一个工程,一般为了指定版本我们会在工程下创建Gemfile来指定使用cocoapods的版本。指定使用Cocoapods的版本除了指定Gemfile以外 , 我们还可以安装指定版本的podssudo gem install cocoapods -v 1.3.1再查看一下pod版本我们就会发现已经安装了1.3.1卸载掉不需要的版本当我们本地同时存在多个版本的pod的时候可以把多余的卸载掉sudo gem uninstall cocoapods会提示我们选择卸载的版本Select gem to uninstall: 1. cocoapods-1.2.1 2. cocoapods-1.3.1 3. All versions>我们选择想要卸载的版本的序号就好了 。如何查看某个SDK的详细信息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.\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为我们生成了一个Podfileplatform表示这个工程安装的设备,后面是系统最低版本现在我们可以在里面添加一下刚才搜索的仓库# Uncomment the next line to define a global platform for your projectplatform :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 ocTestend这里我们添加了一个仓库,接下来再命令行执行pod update来安装所需要的仓库,安装完毕后我们可以看到当前路径下有两个工程文件Test.xcodeprojTest.xcworkspace我们在使用了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' # 官方库上面那个就是我们要添加的私有源地址,下面的是官方源地址,如果都不写的话那么默认就会使用官方源 。如何创建并发布仓库到私有repo首先执行命令,后面替换为你要发布SDK的名字pod lib create TDFCommonUtilpod会给出一些选项让我们选择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 ] >iosWhat language do you want to use?? [ Swift / ObjC ] >swiftWould you like to include a demo application with your library? [ Yes / No ] >yesWhich testing frameworks will you use? [ Quick / None ] > NoneWould you like to do view based testing? [ Yes / No ] > NO现在我们需要添加一些信息cd进入刚才创建的目录,执行命令 ls 可以看到控制台有以下输出。Example README.md TDFCommonUtil.podspecLICENSE TDFCommonUtil _Pods.xcodeproj解释一下这里都是啥Example 这里面就是示例工程README.md 这里面是我们对改pod功能和使用等的介绍 , 使用markdown语法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 = <<-DESCTODO: 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需要配置的大部分选项了 ,简单介绍一下name pod名称version 版本号 ,特别注意下在更新时需要对git仓库打tagsummary 简介description 描述homepage 主页license 使用协议author 作者deployment_target 需要系统版本source_files 源文件路径resource_bundles 资源文件路径 注意这里会把资源文件打包成bundle , 调用的时候要注意下public_header_files 公开的头文件 (有些头文件我们不想要外部看见可以在这里去掉)frameworks 需要依赖的framework库dependency 需要依赖的其他pod现在把这些填好吧 。如何添加依赖有的时候我们开发的pod仓库需要依赖其他仓库,比如我们需要依赖ReactiveObjC这里就可以在TDFCommonUtil.podspec下面添加这一行s.dependency ‘ReactiveObjC’ps ,这里可以指向特定的版本也可以用 ‘> 2.3’ 的形式表示依赖此仓库至少大于2.3版本但是不会超过3.0 。使用lint命令检测我们的仓库是否还有问题一切就绪后你可以在里面创建你的文件,添加代码了,别忘了再有文件的增加或删除后在运行一遍 ‘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和内部模块化解耦,用好这个管理工具无疑会为我们的工作效率带来巨大提升。
...