Cocoapod是一个强大的第三方的开源框架的管理工具,其中的各种开源类库,很多都是持续更新;那么我们在使用的时候就可以使用到最新的类库;由于我们在使用开源框架的时候,也难免会遇到 第三方类库可能会出现的Bug,或者由于苹果的SDK更新,各种框架也需要更新,使用Cocoapods的话就可以一键更新到最新的开源框架了!
首先就是要安装Cocoapos了,安装它需要ruby环境,但是较新的Mac 系统已经为我们安装好了ruby,所以我们现在可以直接安装Cocoapods啦!
安装Cocoapods步骤:
1.打开终端,设置root密码
执行如下命令来设置root密码(如果你没有设置root密码的话)
sudo passwd root
New password: Retype new password:
输入密码并且确认即可
2.安装Cocoapods
(1)执行命令:
su
输入密码切换到root用户
(2)执行:
gem sources --remove https://rubygems.org/
待有反应(输出
https://rubygems.org/ removed from sources
)
(3)然后执行:
gem sources -a http://ruby.taobao.org/
(输出是
http://ruby.taobao.org/ added to sources
)
(4)查看是否关联到镜像,执行
gem sources -l
(输出
*** CURRENT SOURCES ***
http://ruby.taobao.org/
,代表执行之前的命令是成功的)
(5)安装Cocoapods,执行
sudo gem install cocoapods
可能需要等待几分钟,Cocoapods库就安装好了,输出如下:
Fetching: i18n-0.7.0.gem (100%) Successfully installed i18n-0.7.0 Fetching: thread_safe-0.3.5.gem (100%) Successfully installed thread_safe-0.3.5 Fetching: tzinfo-1.2.2.gem (100%) Successfully installed tzinfo-1.2.2 Fetching: minitest-5.5.1.gem (100%) Successfully installed minitest-5.5.1 Fetching: activesupport-4.2.1.gem (100%) Successfully installed activesupport-4.2.1 Fetching: nap-0.8.0.gem (100%) Successfully installed nap-0.8.0 Fetching: fuzzy_match-2.0.4.gem (100%) Successfully installed fuzzy_match-2.0.4 Fetching: cocoapods-core-0.36.0.gem (100%) Successfully installed cocoapods-core-0.36.0 Fetching: claide-0.8.1.gem (100%) Successfully installed claide-0.8.1 Fetching: colored-1.2.gem (100%) Successfully installed colored-1.2 Fetching: xcodeproj-0.23.0.gem (100%) Successfully installed xcodeproj-0.23.0 Fetching: cocoapods-downloader-0.8.1.gem (100%) Successfully installed cocoapods-downloader-0.8.1 Fetching: cocoapods-plugins-0.4.1.gem (100%) Successfully installed cocoapods-plugins-0.4.1 Fetching: cocoapods-try-0.4.3.gem (100%) Successfully installed cocoapods-try-0.4.3 Fetching: netrc-0.7.8.gem (100%) Successfully installed netrc-0.7.8 Fetching: cocoapods-trunk-0.6.0.gem (100%) Successfully installed cocoapods-trunk-0.6.0 Fetching: molinillo-0.2.1.gem (100%) Successfully installed molinillo-0.2.1 Fetching: escape-0.0.4.gem (100%) Successfully installed escape-0.0.4 Fetching: open4-1.3.4.gem (100%) Successfully installed open4-1.3.4 Fetching: cocoapods-0.36.0.gem (100%) Successfully installed cocoapods-0.36.0 Parsing documentation for i18n-0.7.0 Installing ri documentation for i18n-0.7.0 Parsing documentation for thread_safe-0.3.5 Installing ri documentation for thread_safe-0.3.5 Parsing documentation for tzinfo-1.2.2 Installing ri documentation for tzinfo-1.2.2 Parsing documentation for minitest-5.5.1 Installing ri documentation for minitest-5.5.1 Parsing documentation for activesupport-4.2.1 unable to convert "\x84" from ASCII-8BIT to UTF-8 for lib/active_support/values/unicode_tables.dat, skipping Installing ri documentation for activesupport-4.2.1 Parsing documentation for nap-0.8.0 Installing ri documentation for nap-0.8.0 Parsing documentation for fuzzy_match-2.0.4 Installing ri documentation for fuzzy_match-2.0.4 Parsing documentation for cocoapods-core-0.36.0 Installing ri documentation for cocoapods-core-0.36.0 Parsing documentation for claide-0.8.1 Installing ri documentation for claide-0.8.1 Parsing documentation for colored-1.2 Installing ri documentation for colored-1.2 Parsing documentation for xcodeproj-0.23.0 Installing ri documentation for xcodeproj-0.23.0 Parsing documentation for cocoapods-downloader-0.8.1 Installing ri documentation for cocoapods-downloader-0.8.1 Parsing documentation for cocoapods-plugins-0.4.1 Installing ri documentation for cocoapods-plugins-0.4.1 Parsing documentation for cocoapods-try-0.4.3 Installing ri documentation for cocoapods-try-0.4.3 Parsing documentation for netrc-0.7.8 Installing ri documentation for netrc-0.7.8 Parsing documentation for cocoapods-trunk-0.6.0 Installing ri documentation for cocoapods-trunk-0.6.0 Parsing documentation for molinillo-0.2.1 Installing ri documentation for molinillo-0.2.1 Parsing documentation for escape-0.0.4 Installing ri documentation for escape-0.0.4 Parsing documentation for open4-1.3.4 Installing ri documentation for open4-1.3.4 Parsing documentation for cocoapods-0.36.0 Installing ri documentation for cocoapods-0.36.0 20 gems installed
(6)测试一下pod命令,在终端 执行
exit
退出root用户,因为Cocoapods默认不能在root上正常的使用
pod
会输出:
sh-3.2# exit exit macios:~ mac$ pod Usage: $ pod COMMAND CocoaPods, the Cocoa library package manager. Commands: + init Generate a Podfile for the current directory. + install Install project dependencies to Podfile.lock versions + ipc Inter-process communication + lib Develop pods + list List pods + outdated Show outdated project dependencies + plugins Show available CocoaPods plugins + repo Manage spec-repositories + search Searches for pods + setup Setup the CocoaPods environment + spec Manage pod specs + trunk Interact with the CocoaPods API (e.g. publishing new specs) + try Try a Pod! + update Update outdated project dependencies and create new Podfile.lock Options: --silent Show nothing --version Show the version of the tool --verbose Show more debugging information --no-ansi Show output without ANSI codes --help Show help banner of specified command macios:~ mac$
输出如上,代码Cocoapods安装成功!
3.使用Cocoapods
首先新建一个ios工程如FirstApp,然后关闭工程[此点很重要,否则会无法正常使用Cocoapods安装类库],切换到工程的根目录(拖拽工程文件夹可以快速的切换)
(1)执行
cd /Users/mac/Desktop/FirstApp(你的工程根目录路径)
(2)执行
pod init
执行完成之后,会在根目录下创建一个文件:Podfile
(3)搜索你需要的类库,只需输入部分字符串即可,自动查找
比如我们来搜索一下AFNetworking类库,执行:
pod search afnet
如果是第一次使用Cocoapods搜索的话会满一些,稍等一下,即可看到很多的AF相关的内容出来了,其中有我们想要的AFNetworking框架
部分输出如下:
-> AFNetworkActivityLumberjackLogger (2.0.3) AFNetworking 2.0 Extension for Network Request Logging with Support of CocoaLumberjack forked from AFNetworkActivityLogger by Matt Thompson pod 'AFNetworkActivityLumberjackLogger', '~> 2.0.3' - Homepage: https://github.com/sohayb/AFNetworkActivityLogger - Source: https://github.com/sohayb/AFNetworkActivityLogger.git - Versions: 2.0.3 [master repo] -> AFNetworking (2.5.1) A delightful iOS and OS X networking framework. pod 'AFNetworking', '~> 2.5.1' - Homepage: https://github.com/AFNetworking/AFNetworking - Source: https://github.com/AFNetworking/AFNetworking.git - Versions: 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]
拷贝出其中我们需要使用的命令:
pod 'AFNetworking', '~> 2.5.1'
这个命令代表了安装AF的版本,是2.5.1
AF安装到工程中的方法:
打开工程根目录下的Podfile文件,把上面的安装AF的命令写入其中即可,如果有多个框架分行写入到其中即可
# Uncomment this line to define a global platform for your project # platform :ios, '6.0' target 'FirstApp' do pod 'AFNetworking', '~> 2.5.1' end target 'FirstAppTests' do end
如上,把pod ‘AFNetworking‘ , ‘~>2.5.1‘放在do 之后,即可
如果还有其他的框架,则放在
pod ‘AFNetworking‘ , ‘~>2.5.1‘的下一行,即可
执行命令来安装AF或者其他的你在Podfile中写入的框架命令:
pod install
稍等一两分钟即可安装成功,
输出如下:
macios:FirstApp mac$ pod install Analyzing dependencies Downloading dependencies Installing AFNetworking (2.5.1) Generating Pods project Integrating client project [!] Please close any current Xcode sessions and use `FirstApp.xcworkspace` for this project from now on.
那么我们就安装成功了,按照他的输出的提示,下次使用时打开 FirstApp.xcworkspace即可()
{请勿打开原来的FirstApp.xcodeproj文件,会报错}
4.使用框架,需要使用<>尖括号引入
要使用AF导入如下框架即可!
#import <AFNetworking.h>
原文地址:http://blog.csdn.net/yangbingbinga
下一节:如何解决使用Cocoapods带来的问题