CocoaPods 安装和使用

2017-01-12  本文已影响132人  伊尔今夏

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

CocoaPods 安装

$ sudo gem update --system
$ gem sources --remove https://rubygems.org/
$ gem sources -a https://ruby.taobao.org/
$ gem source -l 
*** CURRENT SOURCES ***

https://ruby.taobao.org/

如果显示上面的结果就比较替换成功了

$ sudo gem install cocoapods
$ pod setup

以上就是安装 CocoaPods 最重要的三个步骤,顺利的话就是安装成功。
但是在安装到最后一步,感觉命令窗口就卡住了。

Setting up CocoaPods master repo

在我以为是卡住的时候多次kill进程重来,发现结果是一样。Google一圈之后发现不是卡住了,pod setup第一次运行需要下载一个 master 文件。不是卡住是一直在下载程序,也可以用命令来查看下载进度.

$ cd ~/.cocoapods
$ du -sh *

最后文件大小为386M,下载完成后

Setting up CocoaPods master repo

CocoaPods 1.2.0.beta.3 is available.
To update use: `sudo gem install cocoapods --pre`
[!] This is a test version we'd love you to try.

For more information, see https://blog.cocoapods.org and the CHANGELOG for this version at https://github.com/CocoaPods/CocoaPods/releases/tag/1.2.0.beta.3

Setup completed

以上表示安装 CocoaPods 成功了。

CocoaPods 使用

$ vim Podfile
platform :ios, '8.0'
pod 'SDWebImage', '~> 3.8'

$ pod install

执行成功之后,输出如下:

Analyzing dependencies
Downloading dependencies
Installing SDWebImage (3.8.2)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `MKSwiftControls.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

pod installl 执行成功之后发现 ~/.CocoaPods/repos/master 文件的大小由 386M 变成 958M

CocoaPods的原理是将所有的依赖库都放到另一个名为Pods的项目中,然后让主项目依赖Pods项目,这样,源码管理工作都从主项目移到了Pods项目中。Pods项目最终会编译成一个名为libPods.a的文件,主项目只需要依赖这个.a文件即可。

基于以上的原理, master 文件会在第一次 pod install 之后会变大

以上是 CocoaPods 基本使用,如果都正确输出了,Happy Ending !!! 下面就不用看了!!!


在写好Podfile执行命令Pod install后,命令行输出错误

Analyzing dependencies
[!] The dependency `SDWebImage (~> 3.8)` is not used in any concrete target.

提示没有指定一个 target ,一再修改 Podfile 之后,最后 Podfile 版本

platform :ios, '8.0'

target 'MKSwiftControls' do
    use_frameworks! 
    pod 'SDWebImage', '~> 3.8'
end

以上可以执行成功,除了指定了一个 target ,还添加了 use_framworks!

use_framworks! 又是什么?

SDWebImage的GitHub主页,关于 CocoaPods 使用作者添加了一些说明

If you are using Swift, be sure to add use_frameworks! and set your target to iOS 8+:

platform :ios, '8.0'
use_frameworks!

先看 Pod install 执行之后,项目文件的变化

iOS 8 之前苹果只允许使用静态库,而 iOS 8 后就可以打包动态库了,虽然最后也是生成动态库。

使用 use_framworks! 就是选择使用动态库来封装。

项目 target 的配置选项变化

上面这些都是 CocoaPods 已经给配置好了,不需要自己额外的操作。但是对于还要兼容 iOS 7 及以下版本的项目是不能使用 use_framworks! 还是需要自己手动配置的!

http://blog.devtang.com/2014/05/25/use-cocoapod-to-manage-ios-lib-dependency/

https://segmentfault.com/a/1190000005041357

https://gold.xitu.io/post/58730a25a22b9d0058971144

上一篇 下一篇

猜你喜欢

热点阅读