美文网首页
CocoaPods 安装和使用

CocoaPods 安装和使用

作者: 伊尔今夏 | 来源:发表于2017-01-12 15:09 被阅读132次

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

CocoaPods 安装

  • 更新gem
$ sudo gem update --system
  • 替换 Ruby 源
$ gem sources --remove https://rubygems.org/
$ gem sources -a https://ruby.taobao.org/
$ gem source -l 
*** CURRENT SOURCES ***

https://ruby.taobao.org/

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

  • 安装 CocoaPods
    环境都准备好了,就可以安装,也使用命令
$ 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 使用

  • 在项目的根目录下创建一个文件 Podfile
$ vim Podfile
  • 在文件 Podfile 中要添加的想要的第三方库文件,比如:SDWebImage
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 执行之后,项目文件的变化

  • 修改了 project.pbxproj 文件的内容将原来的文件格式转化为XML文件格式

  • 添加了一个 工程名.xcworkspace 的工作空间

  • 将原来的工程和新的 Pods 工程放到了同一个工作空间

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

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

项目 target 的配置选项变化

  • RunPath Search Path 搜索路径设置为 @executable_path/Frameworks

  • C 和 C++ 编译器搜索路径设置是相同的

  • 添加对另一个工程 Pods_工程名.framework 的依赖,添加链接器选项为framework

  • Pods-工程名-frameworks.shPods-工程名-resources.sh ,这两个文件实际上就是将封装通用架构动态库文件和将动态库复制到 Bundle 指定目录下。

上面这些都是 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

相关文章

网友评论

      本文标题:CocoaPods 安装和使用

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