CocoaPods可以方便地通过Mac自带的RubyGems安装。
打开Mac电脑自带的终端,然后按照以下操作即可:
1.更换Ruby镜像
首先查看ruby当前源 $ gem sources -l
$ gem sources -l
*** CURRENT SOURCES ***
https://rubygems.org/
移除ruby当前源 $ gem sources --remove https://rubygems.org/
$ gem sources --remove https://rubygems.org/
替换国内最新镜像源(淘宝的Ruby镜像已经不更新了)
$ gem sources -a https://gems.ruby-china.org/
再次查看
$ gem sources -l
*** CURRENT SOURCES ***
https://gems.ruby-china.org
2.升级ruby环境
如果使用 $ gem update --system
会报错误
ERROR: While executing gem ... (Gem::FilePermissionError) You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory
这时使用
$ sudo gem update --system
升级成功后会提示
RubyGems system software updated
3.安装cocoapods
如果使用 $ sudo gem install cocoapods
报错误
ERROR: While executing gem ... (Gem::FilePermissionError) You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory
那就使用
$ sudo gem install -n /usr/local/bin cocoapods
然后执行pod setup
(ps:这个执行很慢,耐心等待...... pod setup在执行时,会输出Setting up CocoaPods master repo,然后会卡一会儿。这步其实是 Cocoapods 将它的信息下载到 ~/.cocoapods目录下,你可以command+n新建一个终端窗口,执行cd ~/.cocoapods/进入到该文件夹下,然后du -sh *查看文件大小,这个目录最终大小是900多M。出现Setup completed 的时候说明已经完成了
$ pod setup
4.cocoapods使用
①首先搜索需要的三方库 AFNetworking,会出现符合条件的搜索结果
$ pod search AFNetworking
②在工程中创建Podfile文件,输入终端命令进入到工程目录下,然后创建Podfile文件
$ cd /Users/Desktop/YourAPPName
$ vim Podfile
③进入界面后,输入
platform :ios, '8.0'
target 'YourApp' do
pod 'AFNetworking', '~> 3.1.0'
end
④把三方库下载到工程中
$ pod install
注:如果是在swift的工程中引入三方库,pod install 可能出现下面错误,处理办法
[!] The `GWeather [Debug]` target overrides the `ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES` build setting defined in `Pods/Target Support Files/Pods-GWeather/Pods-GWeather.debug.xcconfig'. This can lead to problems with the CocoaPods installation
- Use the `$(inherited)` flag, or
- Remove the build settings from the target.
找到【Project/Target】-->【Your Project Name】-->【Buid Settings】
搜索‘ ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES’,选择“other”,输入 $(inherited)
1.png
网友评论