我们来看一个Podfile文件事例
platform :ios, '5.0'
inhibit_all_warnings!
pod 'SDWebImage','~>3.2'
pod 'JASidePanels','~>1.3.1'
pod 'BlocksKit','~>1.8'
pod 'TTTAttributedLabel','~>1.7'
pod 'MBProgressHUD','~>0.6'
pod 'RTLabel','~>1.0'
pod 'KNSemiModalViewController','~>0.3'
pod 'SFHFKeychainUtils','~>0.0.1'
pod 'MBMvc',:git => 'git@github.com:alibaba/MBMvc.git'
pod 'RSA',:path => 'libs/Sources/RSA'
可以看到依赖的库不单单可以依赖官方库,还可以直接依赖某个git上的库(git=>标示)或者本地的库(path=>标示) 不过自己依赖的库需要自己写podspec文件,这个下节会详述
如上的Podfile有一个比较坑爹的事情就是 你这样写的话,只有对项目中的第一个target的起效,如果你的项目中有多个target的话怎么把依赖关系直接应用到每个target上,如下在
target :XXX_91 do
pod 'Resources',:path => 'Resources'
end
target :XXX_Weiphone do
pod 'Resources',:path => 'Resources'
end
target :XXX_Release do
pod 'Resources',:path => 'Resources'
end
target :XXX_PreRelease do
pod 'Resources',:path => 'Resources'
end
为每个target都加上一个pod依赖,那么这个target会被Pod整体的加上依赖和配置,因为依赖是会被继承的,除非特别指定:exclusive => true
详情见 http://docs.cocoapods.org/podfile.html#target
当你写好了Podfile,就可以直接执行pod install,这个时候pod就是为您建立起一个Pods项目,并且简历好一个workspace来包含Pods项目和你自己的项目,并在你的项目中直接依赖Pods项目产出的lib包
如果以后Podfile文件有变动的话,需要更新项目配置就不是执行pod install了 而是执行’pod update’,他会更新整个依赖和Pods项目..具体的依赖关系 可以直接cat Podfile.lock 来查看
##怎么编写自己的PodSpec
PodSpec是对一个Pod项目的描述,pod可以根据PodSpec文件来指导需要拉那些文件下来编译以及设置编译的参数
详细的信息可以看http://docs.cocoapods.org/specification.html
Pod::Spec.new do |s|
s.name = "MBMvc"
s.version = "1.0.0"
s.summary = "MBMvc is a Message Based MVC framework."
s.homepage = "https://github.com/alibaba/MBMvc"
s.license = { :type => 'GPL2' , :text => <<-LICENSE
(C) 2007-2013 Alibaba Group Holding Limited
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
LICENSE
}
s.author = { "文通" => "wentong@taobao.com" }
s.source = { :git => "https://github.com/alibaba/MBMvc.git", :tag => "1.0.0" }
s.platform = :ios, '6.1'
s.ios.deployment_target = '4.3'
s.source_files = 'MBMvc/**/*.{h,m}'
s.public_header_files = 'MBMvc/**/*.h'
s.requires_arc = true
s.prefix_header_contents = <<-EOS
#ifdef DEBUG
#define TBMB_DEBUG
#endif
EOS
end
如上可以看到一个很简单的描述文件..他执行了需要编译的源文件和头文件 以及是否支持ARC(这个很帅,不需要再自己去设置-fno-obj-arc了)..
但是很多时候我们依赖的不是源码而是framework甚至带有Resource,这里给出一个支持直接依赖framework和Resource的例子:
Pod::Spec.new do |s|
s.name = "Huoyan"
s.version = "1.0.0"
s.summary = "Huoyan"
s.source_files = '**/*.h'
s.preserve_paths = 'huoyan.framework','TBScanLib.framework'
s.requires_arc = true
s.frameworks = 'huoyan','TBScanLib'
s.resource = "huoyan.bundle"
s.xcconfig = { 'FRAMEWORK_SEARCH_PATHS' => '"$(SRCROOT)/libs/Frameworks/Huoyan"' }
end
通过preserve_paths 留下framework文件,然后通过xcconfig来设置framework扫描路径来依赖framework
而资源文件的拷贝直接用s.resource = "huoyan.bundle"的方式 依赖
##模块化
通过PodSpec的subspec 可以使一个项目能模块化输出功能 ,一个例子:
Pod::Spec.new do |s|
s.name = "iOS_Util"
s.version = "0.10.0"
s.summary = "Some iOS Util"
s.license = 'MIT'
s.author = { "文通" => "wentong@taobao.com" }
s.platform = :ios, '6.1'
s.ios.deployment_target = '4.3'
s.subspec 'Common' do |cos|
cos.source_files = 'iOS_Util/Common/*.{h,m}'
cos.public_header_files = 'iOS_Util/Common/*.h'
end
s.subspec 'Core' do |cs|
cs.source_files = 'iOS_Util/Core/*.{h,m}'
cs.public_header_files = 'iOS_Util/Core/*.h'
cs.dependency 'libextobjc', '0.2.5'
end
s.subspec 'Json' do |js|
js.source_files = 'iOS_Util/Json/*.{h,m}'
js.public_header_files = 'iOS_Util/Json/*.h'
js.dependency 'iOS_Util/Core'
end
s.subspec 'Bean' do |bs|
bs.source_files = 'iOS_Util/Bean/*.{h,m}'
bs.public_header_files = 'iOS_Util/Bean/*.h'
bs.dependency 'iOS_Util/Core'
end
s.subspec 'DB' do |ds|
ds.source_files = 'iOS_Util/DB/*.{h,m}'
ds.public_header_files = 'iOS_Util/DB/*.h'
ds.dependency 'FMDB/standard' ,'~> 2.1'
ds.dependency 'iOS_Util/Common'
ds.dependency 'iOS_Util/Core'
end
s.subspec 'WebP' do |ws|
ws.source_files = 'iOS_Util/WebP/*.{h,m}'
ws.public_header_files = 'iOS_Util/WebP/*.h'
ws.dependency 'libwebp' ,'~> 0.3.0-rc7'
ws.frameworks = 'CoreGraphics'
end
s.subspec 'Location' do |ls|
ls.source_files = 'iOS_Util/Location/*.{h,m}'
ls.public_header_files = 'iOS_Util/Location/*.h'
ls.dependency 'iOS_Util/Common'
ls.dependency 'iOS_Util/DB'
ls.frameworks = 'CoreLocation' ,'MapKit'
ls.resource = 'iOS_Util/Location/chinaDivision.sqlite'
end
s.subspec 'AMR' do |as|
as.source_files = 'iOS_Util/AMR/**/*.{h,m,mm}'
as.public_header_files = 'iOS_Util/AMR/**/*.h'
as.preserve_paths = "iOS_Util/AMR/**"
as.library = 'opencore-amrnb','opencore-amrwb'
as.xcconfig = { 'LIBRARY_SEARCH_PATHS' => '"$(PODS_ROOT)/iOS_Util/iOS_Util/AMR/lib"' }
end
s.subspec 'Cache' do |cas|
cas.source_files = 'iOS_Util/Cache/*.{h,m,mm}'
cas.public_header_files = 'iOS_Util/Cache/*.h'
cas.dependency 'iOS_Util/Common'
end
s.subspec 'Preference' do |ps|
ps.source_files = 'iOS_Util/Preference/*.{h,m,mm}'
ps.public_header_files = 'iOS_Util/Preference/*.h'
ps.dependency 'iOS_Util/Json'
end
s.requires_arc = true
end
可以看到通过subspec可以区分出不同的模块,而且模块间也能依赖
而在其他项目要用到的时候在Podfile里面可以
pod 'iOS_Util/Json',:git => 'git@gitlab.alibaba-inc.com:tbw/ios_util.git'
这样的方式来直接依赖其中一个模块的代码,而其他模块的代码不会被打包进来
参考
cocoapods添加github上自制类库_2_添加subspec
使用CocoaPods进行Xcode的项目依赖管理