cocoaPods是啥?
简单的说就是个管理第三方开源库的玩意,比如MJRefresh之类的。想要啥,只要在podfile文件中加一行就行,然后pod install一下,第三方库就加到工程中了,很简单,现在来安装吧。
安装安装前先检查下Ruby有没有安装这样表示安装好了。
在终端输入以下命令:
$ sudo gem install -n /usr/local/bin cocoapods如果没有反应,则输入:$ gem sources --remove https://rubygems.org/然后再输入:$ gem sources -a http://gems.ruby-china.org/然后再次在终端运行:$ sudo gem install -n /usr/local/bin cocoapods
等会就会安装好滴。
报错问题://如果发生以下问题
$ sudo gem install cocoapodsERROR: While executing gem ... (Gem::FilePermissionError)You don't have write permissions for the /usr/bin directory.$ pod --version/Library/Ruby/Site/2.3.0/rubygems.rb:271:in
'//解决方案://我这里显示已经更新了$ sudo gem update --systemLatest version currently installed. Aborting.$ mkdir -p $HOME/Software/ruby$ export GEM_HOME=$HOME/Software/ruby$ gem install cocoapods显示:Fetching: xcodeproj-1.5.6.gem (100%)Successfully installed xcodeproj-1.5.6Fetching: cocoapods-1.4.0.gem (100%)Successfully installed cocoapods-1.4.0Parsing documentation for xcodeproj-1.5.6Installing ri documentation for xcodeproj-1.5.6Parsing documentation for cocoapods-1.4.0Installing ri documentation for cocoapods-1.4.0Done installing documentation for xcodeproj, cocoapods after 7 seconds2 gems installedfind_spec_for_exe': can't find gem cocoapods (>= 0.a) (Gem::GemNotFoundException) from /Library/Ruby/Site/2.3.0/rubygems.rb:299:in
activate_bin_path'from /usr/local/bin/pod:23:in `$ export PATH=$PATH:$HOME/Software/ruby/bin//这个时候查看下pod的版本,不报错了$ pod --version显示1.4.0
使用就拿MJRefresh举例,先用终端搜索下$ pod search AFNetworking然后咱创建个Demo项目,在这个项目目录下创建一个叫“Podfile”的文件,这里用终端来创建,我们先到这个项目的目录下,输入$ vim Podfile然后在vim中输入i进入编辑模式source 'https://github.com/CocoaPods/Specs.git'target :RuntimeTest doplatform:ios,'9.0'pod 'MJRefresh', '~> 3.1.15.3'end按esc退出编辑模式,再输入:wq 这样就保存退出了最后运行下面命令$ pod installAnalyzing dependenciesDownloading dependenciesInstalling MJRefresh (3.1.15.3)Generating Pods projectIntegrating client project[!] Please close any current Xcode sessions and use RuntimeTest.xcworkspace
for this project from now on.Sending statsPod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.运行成功了然后打开RuntimeTest.xcworkspace这个文件就完事了