CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. It has over ten thousand libraries and can help you scale your projects elegantly. - 摘录自CocoaPods.orghtml
在CocoaPods出现以前,iOS项目中引用第三方库的方式是很是原始的,要么是把源代码拷贝到主工程中,要么是经过静态库引入.a文件,而后还要修改一系列的build settings。后续的第三方库的升级也是个枯燥乏味的事情,总之若是你的iOS项目目前仍是这样管理第三方库,那么大家还处在石器时代。java
CocoaPods经过集中式的管理,能够很是有效的管理第三方库,甚至能够用于大型项目的模块化管理,很是优雅高效的解决iOS项目中的依赖管理。ios
CocoaPods是一个Ruby Gem,由于直接访问RubyGem速度很是慢,建议先替换成淘宝镜像git
$ gem sources --remove https://rubygems.org/ $ gem sources -a https://ruby.taobao.org/
安装CocoaPodsgithub
$ sudo gem install cocoapods
在项目根目录下建立Podfile,下面是一个Podfile的例子 (详情能够参考http://guides.cocoapods.org/syntax/podfile.html#podfile):ruby
platform :ios, '9.0' target "MyApp" do pod 'ObjectiveSugar', '~> 0.5' target "MyAppTests" do pod 'OCMock', '~> 2.0.1' end end
platform: 能够指定平台的信息和deployment target的版本bash
target: 能够根据不一样的target来引入不一样的pod微信
pod: 引入依赖库架构
pod 'SSZipArchive' -- 引入最新版本ide
pod 'Objection', '0.9' -- 引入特定的版本
pod 'Objection', '>0.9'> -- 任何大于0.9的版本
pod 'Objection', '>=0.9'> -- 任何大于等于0.9的版本
pod 'Objection', '<0.9'> -- 任何小于0.9的版本
pod 'Objection', '<=0.9'> -- 任何小于等于0.9的版本
pod 'Objection', '~>0.9'> -- 任何介于0.9到1.0的最新版本,不包含1.0
pod 'AFNetworking', :path => '~/Documents/AFNetworking' -- 使用本地路径引入
pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :tag => '0.7.0' -- 使用git库引入
pod 'JSONKit', :podspec => 'https://example.com/JSONKit.podspec' -- 使用外部的podspec来引入
安装pods
$ pod install
更新pods
$ pod update
install和update的区别:假如使用 pod 'SVProgressHUD',没有指定版本。使用pod install,若是Pods中存在SVProgressHUD,则直接使用。使用pod update,则会保证更新SVProgressHUD到最新版本。
install或update速度一般很慢,由于每次执行的时候都须要同步一下CocoaPods Specs,这个有几百兆的大小,同步一次很是耗时。因此若是你使用的第三方库并非常常更新,则不用常常更新那个Specs库。可使用如下命令:
$ pod install --verbose --no-repo-update $ pod update --verbose --no-repo-update
执行完install或者update命令后,就可使用.xcworkspace打开项目。
随着iOS APP愈来愈复杂,功能愈来愈多,对于iOS项目的工程化要求也愈来愈高了,对于复杂的APP通常都须要对项目进行模块化管理。
模块化有几个方式:
1. 目录结构管理:这是最原始的方式,仅仅经过目录结构实现代码层次的清晰化。但本质上并无解决代码之间的依赖混乱的状况,模块化划分也很是不清晰。
2. 子工程:经过子工程能够实现代码依赖管理和模块化,可是须要引入复杂的设置,不利于管理。
3. 静态库:将依赖代码打包成为静态库.a,不过因为不能看到源码,调试不方便。
自从有了CocoaPods,可使用它来管理私有库,从而实现了代码模块化管理。例以下图所示:
例如在github上面建立一个空的git库:https://github.com/xxx/MySpecs
将这个git库加入到CocoaPods库的列表中:
$ pod repo add MySpecs git@github.com:xxx/MySpecs.git
此时能够检查下本地的pod repo
$ pod repo list
MySpecs
- Type: git (master)
- URL: git@github.com:xxx/MySpecs.git
- Path: /Users/xxx/.cocoapods/repos/mySpecs
master
- Type: git (master)
- URL: git@github.com:CocoaPods/Specs.git
- Path: /Users/xxx/.cocoapods/repos/master
肯定私有库的Specs已经加到本地pod repo中。
在私有库项目中的根目录,建立对应的podspec文件,里面会描述这个库的基本信息。
PodSpec规范能够查看:https://guides.cocoapods.org/syntax/podspec.html
# # Be sure to run `pod spec lint PodName.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| s.name = "PodName" s.version = "0.0.1" s.summary = "A short description of PodName." s.homepage = "http://github.com/xxx/PodName" s.license = { :type => "MIT", :text => <<-LICENSE Copyright © 2016年 xxx. All rights reserved. LICENSE } s.author = { "" => "" } s.source = { :git => "git@github.com:xxx/PodName.git", :tag => "0.0.1" } s.source_files = "**/*.{h,m,mm,c}" s.frameworks = "Foundation", "QuartzCore", "UIKit", "WebKit" s.libraries = "z" s.dependency 'AFNetworking' s.ios.deployment_target = '6.0' end
resource: 能够指定资源文件,建议使用bundle以免资源文件产生冲突。
frameworks: 指定这个pod依赖的系统framework
libraries: 指定这个pod依赖的系统动态库。注意使用的名字:好比须要引用"libz.dylib", 那么这里只须要写"z"
不管原始项目的目录结构或者group结构,默认的pod里面的代码都会平铺在根目录里面
若是须要增长目录层次结构,则须要使用subspec,详细使用规范:https://guides.cocoapods.org/syntax/podspec.html#subspec
注意:SubSpecs之间不能存在相互依赖关系,只能单向依赖
$ pod lib lint --sources='git@github.com:xxx/MySpecs.git' --verbose --use-libraries --allow-warnings
sources参数能够指定私有库的Pod Specs库的地址。若是可以经过,说明代码编译没有问题。
$ git tag -m "first release" "0.0.1" $ git push --tags #推送tag到远端仓库
$ pod repo push MySpecs PodName.podspec --sources='git@github.com:xxx/MySpecs.git' --use-libraries --allow-warnings
这样就完成了一个CocoaPods的私有库的提交了,别人就能够在Podfile里面使用这个私有库了。
你们若是还有关于CocoaPods的用法,能够一块儿交流。你们玩的开心~
有兴趣同窗能够关注微信公众号奶爸码农,不按期分享投资理财、IT相关内容: