cocoapods简介
CocoaPods是一个负责管理iOS项目中第三方开源库的工具。CocoaPods的项目源码在Github上管理。该项目开始于2011年8月12日,它持续保持活跃更新。开发iOS项目不可避免地要使用第三方开源库,CocoaPods的出现使得我们可以节省设置和更新第三方开源库的时间
在我们有了CocoaPods这个工具之后,只需要将用到的第三方开源库放到一个名为Podfile的文件中,然后在命令行执行$ pod install命令。CocoaPods就会自动将这些第三方开源库的源码下载下来,并且为我的工程设置好相应的系统依赖和编译参数。
cocoapods安装
1、首先升级ruby库
命令 `➜ ~ gem update --system`
如果提示以下信息,说明没有没有写入权限 安装2方法安装
`You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.`
2、使用下面命令
安装命令 `sudo gem update --system`
如果在Mac OS 10.11之后会有以下问题 安装3方法安装
`Operation not permitted - /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/gem`
3、使用👇命令安装
安装命令 ` ➜ ~ sudo gem install -n /usr/local/bin cocoapods`
下面替换镜像源使用国内镜像快速一点
1、查看当前镜像源
命令 `gem sources -l`
显示结果 `https://rubygems.org/`
2、首先移除原有镜像源 使用👇命令:
命令 `gem sources --remove https://rubygems.org/`
执行结果 `https://rubygems.org/ removed from sources`
3、添加新的镜像源 使用👇命令:
命令`gem sources -a https://gems.ruby-china.org/`
执行结果 `https://gems.ruby-china.org/ added to sources`
4、👇查看当前镜像源
命令:`gem sources -l`
执行结果 `https://gems.ruby-china.org/`
设置下载cocoapods使用命令
下载cocopods 本地库 `$ pod setup`
完成之后显示 `Setup completed`
搜索第三方库命令 例如:`➜ ~ pod search afn`
第一次如果出现下面命令
`[!] Unable to find a pod with name, author, summary, or description matching afn`
🐸删除之前缓存文件命令 `rm ~/Library/Caches/CocoaPods/search_index.json`
然后再次执行搜索命令 `➜ ~ pod search afn`
出现如下信息 `Creating search index for spec repo 'master'.. Done!`
🐒等待一会如果会出现下面信息 说明cocoapods 安装完成 可以正常使用了。✅
-> AFNetworking (3.2.0)
A delightful iOS and OS X networking framework.
pod 'AFNetworking', '~> 3.2.0'
- Homepage: https://github.com/AFNetworking/AFNetworking
- Source: https://github.com/AFNetworking/AFNetworking.git
- Versions: 3.2.0, 3.1.0, 3.0.4, 3.0.3, 3.0.2, 3.0.1, 3.0.0, 3.0.0-beta.3, 3.0.0-beta.2, 3.0.0-beta.1, 2.6.3, 2.6.2, 2.6.1, 2.6.0, 2.5.4, 2.5.3, 2.5.2, 2.5.1, 2.5.0, 2.4.1,
2.4.0, 2.3.1, 2.3.0, 2.2.4, 2.2.3, 2.2.2, 2.2.1, 2.2.0, 2.1.0, 2.0.3, 2.0.2, 2.0.1, 2.0.0, 2.0.0-RC3, 2.0.0-RC2, 2.0.0-RC1, 1.3.4, 1.3.3, 1.3.2, 1.3.1, 1.3.0, 1.2.1, 1.2.0,
1.1.0, 1.0.1, 1.0, 1.0RC3, 1.0RC2, 1.0RC1, 0.10.1, 0.10.0, 0.9.2, 0.9.1, 0.9.0, 0.7.0, 0.5.1 [master repo]
- Subspecs:
- AFNetworking/Serialization (3.2.0)
- AFNetworking/Security (3.2.0)
- AFNetworking/Reachability (3.2.0)
- AFNetworking/NSURLSession (3.2.0)
- AFNetworking/UIKit (3.2.0)
-> AFNetworking+AutoRetry (0.0.5)
Auto Retries for AFNetworking requests
pod 'AFNetworking+AutoRetry', '~> 0.0.5'
- Homepage: https://github.com/shaioz/AFNetworking-AutoRetry
- Source: https://github.com/shaioz/AFNetworking-AutoRetry.git
- Versions: 0.0.5, 0.0.4, 0.0.3, 0.0.2, 0.0.1 [master repo]
cocoapods使用 集成AFNetworking为例
1、Xcode创建一个工程PodTest
2、在PodTest根目录下创建一个Podfile
文件 目录结构如下:
➜ PodTest git:(master) ✗ tree -L 1
.
├── PodTest
├── PodTest.xcodeproj
├── Podfile
└── Pods
3、编辑Podfile
文件 文件内容如下:
platform :ios, '8.0'
target "PodTest" do
pod 'AFNetworking', '~> 3.1.0'
end
4、集成第三方库命令 pod install
5、打开工程目录看到.xcworkspace
说明安装成功
6、使用.xcworkspace
文件进行项目开发
7、在工程中引入头文件 #import "AFNetworking.h"
⚠️下面是pod install
安装时的两个问题
Q1:提示如下信息 说明你的网络被强了 要挂VPN 才可以链接到github安装第三发库
[!] Error installing AFNetworking
[!] /usr/bin/git clone https://github.com/AFNetworking/AFNetworking.git /var/folders/0r/cmv1rgg93kn034h4hp2x68bh0000gn/T/d20180409-5557-17d5r6r --template= --single-branch --depth 1 --branch 3.2.0
Cloning into '/var/folders/0r/cmv1rgg93kn034h4hp2x68bh0000gn/T/d20180409-5557-17d5r6r'...
fatal: unable to access 'https://github.com/AFNetworking/AFNetworking.git/': Server aborted the SSL handshake
Q2:提示下面信息说明Xcode的路径不正确,要重新设置Xcode路径
Analyzing dependencies
[!] Could not automatically select an Xcode project. Specify one in your Podfile like so:
xcodeproj ‘path/to/Project.xcodeproj
解决命令:xcode-select -s /Applications/Xcode.app/Contents/Developer
Q3:pod install
成功之后打开工程添加第三方库找不到第三方库头文件
解决办法:在target -> BuildSettings -> search Paths 下的 User Header Search Paths,添加`$(SRCROOT)`或者`$(PODS_ROOT)`然后要设置为`recursive`。