美文网首页IOSiOS Developer程序员
使用CocoaPods管理库依赖

使用CocoaPods管理库依赖

作者: iOSerYJ | 来源:发表于2017-05-03 01:32 被阅读3309次

前戏

首先查看一下ruby和gem的版本,可能需要更新。我们可以使用RVM,但是官方建议尽量使用mac自带的ruby

CocoaPods is built with Ruby and it will be installable with the default Ruby available on macOS. You can use a Ruby Version manager, however we recommend that you use the standard Ruby available on macOS unless you know what you're doing.

  • 查看ruby版本
ruby -v
  • 查看gem版本
gem -v
  • 查看ruby源
gem source -l

如果ruby源是默认源或者淘宝源,那么更新源

  • 移除
gem source -r https://rubygems.org/ 或者 https://ruby.taobao.org/
  • 添加
gem source -a https://gems.ruby-china.org/
  • 更新gem
sudo gem update --system
  • 安装CocoaPods
sudo gem install -n /usr/local/bin cocoapods
  • 查看CocoaPods版本
pod --version
pod setup
  • 列出本地仓库
pod repo list
master
- Type: git (master)
- URL:  https://github.com/CocoaPods/Specs.git
- Path: /Users/YJ/.cocoapods/repos/master

1 repo

如果master仓库源是默认源,那么可以考虑更新源,但是这里不建议更新源,github源只是第一次执行pod setup命令花费大量时间,相比之下,国内镜像问题多多,大致是这样的

oschina源https://git.oschina.net/akuandev/Specs.git已经不存在了
gitcafe源https://gitcafe.com/akuandev/Specs.git2016.03.02迁移到coding
coding源https://git.coding.net/CocoaPods/Specs.git2017.03.16迁移到aliyun和gitclub
aliyun源https://code.aliyun.com/Magi/CocoaPods.git
gitclub源https://gitclub.cn/CocoaPods/Specs.git

  • 移除
pod repo remove 仓库名
  • 添加
pod repo add 仓库名 URL

添加命令可能报错

[!] To setup the master specs repo, please run `pod setup`.

直接克隆

git clone https://code.aliyun.com/Magi/CocoaPods.git 或者 https://gitclub.cn/CocoaPods/Specs.git ~/.cocoapods/repos/master
  • 更新本地仓库

通过省略仓库名,更新所有仓库

pod repo update

更新master仓库

pod repo update master

创建podfile

  • 创建项目

  • cd进入项目根目录

  • 通过pod init命令创建podfile文件
    CocoaPods提供pod init命令创建一个带有默认配置的podfile文件

8917A10B-DEA9-449D-8ABC-CEDD5DFA8101.png

Dependencies

  • pod

通过省略pod的版本,指定最新版本

pod 'AFNetworking'

通过数字,指定某个特定版本

pod 'AFNetworking', '2.5.0'

除此之外,还可以使用运算符

= 0.1  0.1的版本
> 0.1  任何大于0.1的版本
>= 0.1 任何大于等于0.1的版本
< 0.1  任何小于0.1的版本
<= 0.1 任何小于等于0.1的版本
~> 0.1 0.1的版本到0.2的版本,包括0.1,不包括0.2,总是匹配最新版本
  • target

podfile的target和Xcode的target是一一对应关系

# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'

target 'CocoaPodsDemoWithOC' do
  # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
  use_frameworks!

  # Pods for CocoaPodsDemoWithOC
  pod 'AFNetworking', '2.5.0'
  pod 'SDWebImage', '3.7.5'

end

target 'CocoaPodsDemoWithSwift' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for CocoaPodsDemoWithSwift
  pod 'AFNetworking', '2.5.0'
  pod 'SDWebImage', '3.7.5'

end

使用inherit!设置继承模式,:complete继承所有父类行为,:none不继承父类行为,:search_paths只继承父类的search paths

# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'

target 'CocoaPodsDemoWithOC' do
  # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
  use_frameworks!

  # Pods for CocoaPodsDemoWithOC
  pod 'AFNetworking', '2.5.0'
  pod 'SDWebImage', '3.7.5'

  target 'CocoaPodsDemoWithOCTests' do
    inherit! :search_paths
    # Pods for testing
    pod 'MJRefresh', '3.0.0'

  end

end

target 'CocoaPodsDemoWithSwift' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for CocoaPodsDemoWithSwift
  pod 'AFNetworking', '2.5.0'
  pod 'SDWebImage', '3.7.5'

  target 'CocoaPodsDemoWithSwiftTests' do
    inherit! :search_paths
    # Pods for testing
    pod 'MJRefresh', '3.0.0'

  end

end

使用abstract_target定义多个target继承依赖

# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'

abstract_target 'CocoaPodsDemo' do
  use_frameworks!

  pod 'AFNetworking', '2.5.0'
  pod 'SDWebImage', '3.7.5'

  target 'CocoaPodsDemoWithOC'
  target 'CocoaPodsDemoWithSwift'

end

Target configuration

  • platform
    指定工程Pods的Deployment Target,确保Deployment Target大于等于库要求的,否则报错
[!] Unable to satisfy the following requirements:

- `Alamofire (= 4.0.0)` required by `Podfile`

Specs satisfying the `Alamofire (= 4.0.0)` dependency were found, but they required a higher minimum deployment target.

平台的name,:osx for OS X,:ios for iOS,:tvos for tvOS,or :watchos for watchOS

如果没有指定平台,那么CocoaPods自动分配平台为ios,Deployment Target为9.0

[!] Automatically assigning platform ios with version 9.0 on target CocoaPodsDemoWithOC because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.

如果没有指定Deployment Target,那么对于不同平台,CocoaPods提供不同默认值,4.3 for iOS, 10.6 for OS X, 9.0 for tvOS and 2.0 for watchOS

  • inhibit_all_warnings!
    关于禁止警告,可以看我的上一篇文章Xcode消除编译器警告

  • use_frameworks!
    使用framework代替library,Swift项目必须使用use_frameworks!

安装更新依赖

  • 通过pod install或者pod update命令安装或者更新

  • pod install
    可选选项 : --repo-update
    执行pod install之前强制执行pod repo update

  • pod update
    可选选项 : --no-repo-update
    执行pod update之前跳过pod repo update

pod install还是pod update

当第一次使用CocoaPods部署项目时或者当添加删除更新一个pod时,可以使用pod install

  • 每次执行pod install,对于每个pod已经安装的版本,它会写在Podfile.lock文件,这个文件记录每个pod已经安装的版本并且锁定那些版本,Podfile.lock文件大概长这样的
PODS:
  - AFNetworking (2.5.0):
    - AFNetworking/NSURLConnection (= 2.5.0)
    - AFNetworking/NSURLSession (= 2.5.0)
    - AFNetworking/Reachability (= 2.5.0)
    - AFNetworking/Security (= 2.5.0)
    - AFNetworking/Serialization (= 2.5.0)
    - AFNetworking/UIKit (= 2.5.0)
  - AFNetworking/NSURLConnection (2.5.0):
    - AFNetworking/Reachability
    - AFNetworking/Security
    - AFNetworking/Serialization
  - AFNetworking/NSURLSession (2.5.0):
    - AFNetworking/Reachability
    - AFNetworking/Security
    - AFNetworking/Serialization
  - AFNetworking/Reachability (2.5.0)
  - AFNetworking/Security (2.5.0)
  - AFNetworking/Serialization (2.5.0)
  - AFNetworking/UIKit (2.5.0):
    - AFNetworking/NSURLConnection
    - AFNetworking/NSURLSession
  - MJRefresh (3.0.0)
  - SDWebImage (3.7.5):
    - SDWebImage/Core (= 3.7.5)
  - SDWebImage/Core (3.7.5)

DEPENDENCIES:
  - AFNetworking (= 2.5.0)
  - MJRefresh (= 3.0.0)
  - SDWebImage (= 3.7.5)

SPEC CHECKSUMS:
  AFNetworking: 96ac9bf3eda33582701cb1fcc5b896aa1e20311e
  MJRefresh: 72e41ae6c0e9331da3e19cf7e9286a2b1b62ff5f
  SDWebImage: 69c6303e3348fba97e03f65d65d4fbc26740f461

PODFILE CHECKSUM: d86820d289392f6ad6293e733113a16c4751d05b

COCOAPODS: 1.2.1
  • 对于Podfile.lock文件列出的pods,它会下载Podfile.lock文件列出的版本,不会尝试检查一个新的可用版本。对于Podfile.lock文件没有列出的pods。它会寻找一个版本匹配podfile文件的描述

当执行pod update时,它会更新每个pod到最新版本(只要匹配podfile文件的描述就行),不考虑Podfile.lock文件

例如,podfile文件的描述

pod 'AFNetworking'

执行pod install,由于此时没有Podfile.lock文件,安装当时最新的2.5.0版本并且记录在Podfile.lock文件。一段时间之后,再次执行pod install,由于有Podfile.lock文件,版本还是2.5.0,如果执行pod update,那么更新此时最新版本3.0.0

总结Podfile.lock文件,pod install,pod update

  • 对于Podfile.lock文件官方建议加入版本控制
  • 对于podfile文件指定某个特定版本,这样pod installpod update几乎没区别
  • 对于podfile文件没有指定某个特定版本,知道什么时候pod install什么时候pod update

References :
Podfile Syntax Reference
Command-line Reference

相关文章

  • 自定义Cocoapods库

    做Ios开发会使用Cocoapods来管理第三方依赖库,非常好用,指定依赖,自动下载依赖。关于Cocoapods的...

  • 自定义cocoapods库

    经常使用cocoapods来管理第三方依赖库,非常好用 关于cocoapods的安装可以参考CocoaPods安装...

  • 使用CocoaPods管理库依赖

    前戏 首先查看一下ruby和gem的版本,可能需要更新。我们可以使用RVM,但是官方建议尽量使用mac自带的rub...

  • 使用Cocoapods管理依赖库

    做Android开发的时候,有Gradle来帮我们管理依赖,非常方便,不需要自己去找依赖的库去挨个下载,只需要添加...

  • 使用CocoaPods管理依赖库

    CocoaPods是什么 在iOS开发中势必会用到一些第三方依赖库,比如大家都熟悉的ASIHttpRequest、...

  • 创建CocoaPods的制作过程

    使用CocoaPods来管理第三方库实在是方便,在学会了使用CocoaPods后,开始尝试创建一个自己的版本依赖库...

  • Cocoapods使用心得

    Cocoapods CocoaPods管理Xcode项目的依赖库。项目的依赖库在名为 Podfile 的文本文件中...

  • 开源库 - 包管理(依赖库管理)

    Swift依赖库管理 Swift依赖库管理有三种方式: CocoaPods (中心化的依赖管理器,CocoaPod...

  • CocoaPods 安装和使用

    CocoaPods 是一个管理第三方库并解决库之间依赖关系的工具。本文主要介绍 CocoaPods 安装和简单使用...

  • 老项目使用 CocoaPods 遇到的 install 问题

    Android 目前开发中常使用 Gradle 管理依赖,久闻 iOS 使用 CocoaPods 管理依赖。 使用...

网友评论

本文标题:使用CocoaPods管理库依赖

本文链接:https://www.haomeiwen.com/subject/ydopzttx.html