Cocoapods基础使用

Cocoapods基础使用

基础使用

创建一个Cocoapods管理的Xcode项目

  1. 创建一个Xcode项目

  2. 打开终端,cd到工程目录下,使用pod init命令,会初始化一个Podfile的文件

  3. 打开Podfile文件, 第一行指定我们使用的平台以及版本,例如:platform :ios, '9.0',指定支持iOS9之后

  4. 为了使cocoapods关联我们指定的项目,我们需要添加target '$TARGET_NAME' do end,在end前pod要添加的库名即可

    target 'MyApp' do
        pod 'ObjectiveSugar'
    end
    
  5. 保存修改的Podfile文件,执行pod install命令即可

当需要像一个已经存在的xcworkspace中集成cocoapod功能时,只需要在目标块外部添加一行来指定workspace即可:workspace 'MyWorkspace'

cocoapod做了哪些事情

  1. cocoapod创建了一个workspace
  2. 将我们的工程添加到该workspace,
  3. 添加需要的静态库到该workspace
  4. pod库之后都会打包成libPods.a,将该文件link到工程
  5. pod库的编译依赖参数等变为库的.xcconfig文件,根据该文件向项目中添加Xcode configuration设置在编译时的依赖参数
  6. 改变项目 target configurations基于Cocoapods
  7. 添加工程执行script为Pods-resources.sh的bash脚本,然后在每次编译时将pod库中的资源文件复制

pod install && pod update

pod install

pod install只在我们每次修改了podfile文件,增加、删除、修改了依赖库时,才需要执行该命令来检索指定库
当执行pod install后,会将我们使用的库的版本号写入podfile.lock文件中,此文件用来追踪我们使用的pod库的版本号
执行pod install,会下载Podfile.lock文件中已存在的pod库的指定版本,或者不在podfile.lock中的pod库的合适版本

pod outdated

此命令会根据podfile.lock中的版本,列出所有有较新版本的pod库,这就是我们执行pod update或者 pod update PODNAME会更新的版本内容(这些写版本也是会受限于我们的podfile中设置的pod库版本号 pod 'MyPod', '~>x.y')

pod outdated

会检索比podfile.lock中版本更新的库,并更新(只要该版本符合podfile中版本的限制)
pod update PODNAME 更新一个
pod update 更新所有

因为podfile.lock是记录我们pod库版本的,因此我们需要向远程仓库中commit、push该文件

Podfile

一个比较复杂的podfile文件如下

source 'https://github.com/CocoaPods/Specs.git'
source 'https://github.com/Artsy/Specs.git'

platform :ios, '9.0'
inhibit_all_warnings!

target 'MyApp' do
  pod 'GoogleAnalytics', '~> 3.1'

  # Has its own copy of OCMock
  # and has access to GoogleAnalytics via the app
  # that hosts the test target

  target 'MyAppTests' do
    inherit! :search_paths
    pod 'OCMock', '~> 2.0.1'
  end
end

post_install do |installer|
  installer.pods_project.targets.each do |target|
    puts target.name
  end
end

具体用法可以查看cocoapod官网,下面为部分用法解析:

多个Target共享pod库

使用abstract_target来使多个目标共享pod
abstract_target是抽象对象,只是为了让方便继承,并不存在实际该target

# 并没有名为'Shows'的目标工程
abstract_target 'Shows' do
  pod 'ShowsKit'
  pod 'Fabric'

  # 'ShowsiOS’工程包含'ShowsKit'和'Fabric'pod库的拷贝
  target 'ShowsiOS' do
    pod 'ShowWebAuth'
  end

  # 'ShowsTV’工程包含'ShowsKit'和'Fabric'pod库的拷贝
  target 'ShowsTV' do
    pod 'ShowTVAuth'
  end
end

可以简写为 和我们上面复杂的的Podfile 例子一样了

pod 'ShowsKit'
pod 'Fabric'

target 'ShowsiOS' do
  pod 'ShowWebAuth'
end

target 'ShowsTV' do
  pod 'ShowTVAuth'
end

指定版本

  • 指定特别的版本

    • pod 'SSZipArchive' 用最新的pod库版本
    • pod 'Objection', '0.9' 指定使用的为特定的pod库版本
    • pod 'Objection', '>0.1' 指定任何大于0.1的版本
    • pod 'Objection', '>=0.1' 指定任何大于或等于0.1的版本
    • pod 'Objection', '<0.1' 指定任何小于0.1的版本
    • pod 'Objection', '<=0.1' 指定任何小于或等于0.1的版本
    • pod 'Objection', '~>0.1.2' 指定版本在0.1.2到0.2之间 不包括0.2
    • pod 'Objection', '~>0.1' 指定版本在0.1.2到1.0之间 不包括1.0

指定pod的来源

指定pod来源为本地文件

常用于pod发布之前的本地调试

# 
pod 'Alamofire', :path => '~/Documents/Alamofire'
#Pod文件的podspec应该位于指定的文件夹中

自定义pod的git分支或版本

有时候对于我们自己创建的pod库,我们可能需要pod特定版本或者指定分支
此时,在该库的根目录中应该包含podspec文件

# pod git 上master分支的版本
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git'

# pod dev分支的版本库
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :branch => 'dev'

# pod 指定tag的库
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :tag => '3.1.1'

# pod 指定commit号版本
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :commit => '0f506b1c45'

指定特定的源

通常我们会在全局范围搜索指定的库,当指定了该项后,我们对此pod库将只搜索指定的源

pod 'PonyDebugger', :source => 'https://github.com/CocoaPods/Specs.git'

安装子pod库

我们通常在安装pod时 会安装定义在其中的所有的Subspecs
我们可以通过此方法安装其中子库

pod 'QueryKit/Attribute'

pod 'QueryKit', :subspecs => ['Attribute', 'QuerySet']

对于没有podspec的库,从spec集合库外其他获取

假如可以从其它来源,例如http请求,获取到podspec

pod 'JSONKit', :podspec => 'https://example.com/JSONKit.podspec'

install!

生命安装期间使用的安装方法或选项, 一般情况下,不需要特殊指定,使用默认的就行

abstract!

指定当前目标是抽象的,因此不会链接到xcode目标

inherit!

设置子target继承父target的继承模式, :none表示不继承任何 :search_path之继承父的searchPath

target 'App' do
  target 'AppTests' do
    inherit! :search_paths
  end
end

工程配置

inhibit_all_warnings!

禁止所有cocoapods的warning,可以被子target继承或重定义

# 禁用特定库的warning  
pod 'SSZipArchive', :inhibit_warnings => true

use_frameworks!

使用framework代替静态库 常用与swift项目

Source

Source是用来 从给定的source列表检索指定pod库的specs

我们可以指定specs位置

# 这是自定义源 源的顺序与pod库顺序相关 pod库将使用第一个找到该库的源的版本
source 'https://github.com/artsy/Specs.git'
# 这是官方源 其实隐含包括的 当使用自定义source时 就需要显示指定官方源
source 'https://github.com/CocoaPods/Specs.git'

Hooks

Hooks 包括 plugin pre_install post_install 等 会在安装pod库的进程中调用进行一些处理,详情

你可能感兴趣的:(Cocoapods基础使用)