美文网首页
CocoaPods安装和使用

CocoaPods安装和使用

作者: 倪大头 | 来源:发表于2018-03-26 21:09 被阅读20次

1.更换Ruby镜像

执行以下命令删除原来的ruby源:
gem sources --remove https://rubygems.org/

成功后会显示:
https://rubygems.org/ removed from sources

然后添加国内镜像源:
gem sources -a https://gems.ruby-china.com/

查看是否成功:
gem sources -l

输出:
*** CURRENT SOURCES ***
https://gems.ruby-china.org/

2.安装CocoaPods

升级ruby版本:
sudo gem update --system

成功后安装Pods:
sudo gem install -n /usr/local/bin cocoapods

执行pod setup:
pod setup

如果遇到如下报错:

[!] /usr/bin/git clone https://github.com/CocoaPods/Specs.git master --progress

Cloning into 'master'...
remote: Counting objects: 2016093, done.        
remote: Compressing objects: 100% (555/555), done.        
error: RPC failed; curl 56 LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54
fatal: The remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed

修改Git的传输字节限制即可:
git config --global http.postBuffer 1048576000

Setup completed表示setup完成

3.CocoaPods的使用

试一下AFN:
pod search AFNetworking

成功后显示:

-> 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
:

这时表示CocoaPods已经安装成功了,按q退出

在工程中创建一个Podfile文件:
cd /Users/nichenhao/Desktop/PodsTest/
touch Podfile

Podfile文件创建完毕,编辑你想导入的第三方库的名称及版本:
vim Podfile

按i编辑:

platform :ios, '7.0'
target 'PodsTest' do
pod 'AFNetworking', '~> 3.2.0'
end

'~> 3.2.0' 可以不加,默认最新版,esc,:wq保存退出

把该库下载到Xcode中:
pod install

Analyzing dependencies
Downloading dependencies
Installing AFNetworking (3.2.0)
Generating Pods project
Integrating client project

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

完成,导入一下试试吧:

#import "ViewController.h"
#import <AFNetworking.h>

@interface ViewController ()

@end

相关文章

网友评论

      本文标题:CocoaPods安装和使用

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