您的位置:首页 > 移动开发 > IOS开发

iOS cocoapods

2015-09-14 17:42 591 查看
  cocoapods 听起来很高大上,其实就是一个用来管理第三方库的一个家伙。

  怎么使用呢?在我集合了两张博文和自己的实践后,总结出来:两篇链接如下(谢谢博主): http://blog.csdn.net/sanjunsheng/article/details/28398455http://www.360doc.com/content/14/0309/10/11029609_358970353.shtml 有了这个工具,它就能帮我们好好地管理第三方库了。

它解决了库与库之间的依赖关系,同时通过创建一个Xcode的workspace来将这些第三方库和我们的工程连接起来,供我们开发使用。

使用CocoaPods的目的是让我们能自动化的、集中的、直观的管理第三方开源库。

1.安装:

  CocoaPods是使用Ruby实现的,要想使用它需要有Ruby的环境,幸运的是OS X系统默认的已经可以运行Ruby了,因此我们只需要直接下载运行即可

  1.1 如果直接安装,肯定是不可行的。首先我们必须先把原来的RubyGems移除掉,原因很简单:因为我们链接不上;万能的马云替我们完成了这一伟大的工程。

原理:先移除掉系统默认的,改为淘宝的就可以了。代码如下:

gem sources --remove https://rubygems.org/


  提示如下:

/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/universal-darwin14/rbconfig.rb:213: warning: Insecure world writable dir /usr/local in PATH, mode 040777
source https://rubygems.org/ not present in cache


  这个是因为我之前把它给删掉了一次;

  1.2 添加淘宝的:

gem sources -a https://ruby.taobao.org/


  提示如下:

/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/universal-darwin14/rbconfig.rb:213: warning: Insecure world writable dir /usr/local in PATH, mode 040777 https://ruby.taobao.org/ added to sources


  最后一行,代表着淘宝的镜像已经安装上了。如果不是这个结果,请百度之~

  1.3 查看目前所有的镜像文件:

gem sources -l


  提示如下:

/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/universal-darwin14/rbconfig.rb:213: warning: Insecure world writable dir /usr/local in PATH, mode 040777
*** CURRENT SOURCES ***
 https://ruby.taobao.org/


  最后一行,代表所有的镜像文件只有淘宝。

  1.4 镜像文件已经搭建好了,那么就让我们安装CocoaPods吧:

gem install cocoapods


  提示如下:

/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/universal-darwin14/rbconfig.rb:213: warning: Insecure world writable dir /usr/local in PATH, mode 040777
Fetching: i18n-0.7.0.gem (100%)
ERROR:  While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.


  它告诉我:我没有这个权限。。(如果有其他报错,比如: Could not find a valid gem 'coocapods' (>= 0) in any repository,也是没有权限的原因)百度之之后,这么解决:

sudo chmod 777 /Library/Ruby/Gems/2.0.0/


  需要输入密码。输完密码后,继续安装。

  1.5 重新安装

sudo gem install cocoapods


  提示如下:(一部分)

/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/universal-darwin14/rbconfig.rb:213: warning: Insecure world writable dir /usr/local in PATH, mode 040777
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


  。。。。还有很多,代表着正在安装。。

等到结束后,我们会看到:

Installing ri documentation for cocoapods-0.38.2
20 gems installed


  这代表着就结束了。。

  1.6 设置cocoapods

pod setup


  提示如下:

/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/universal-darwin14/rbconfig.rb:213: warning: Insecure world writable dir /usr/local in PATH, mode 040777
Setting up CocoaPods master repo

CocoaPods 0.39.0.beta.4 is available.
To update use: `gem install cocoapods --pre`
[!] This is a test version we'd love you to try.

For more information see http://blog.cocoapods.org and the CHANGELOG for this version http://git.io/BaH8pQ. 
Setup completed


  代表着设置结束。

  1.7 去设置CocoaPods的项目文件,进入我们的项目,cd (项目目录) eg“cd /Users/KuaiYong/Desktop/exercises/cocoapodsTest”我项目的文件夹是:cocoapodsTest

  1.8 进入目录后,点击ls,就得能你的.xcodeproj才行,否则就是目录不对,得去调整;

  1.9 建立Podfile文件,我对它的理解是:在这里面说明要引入哪些文件。

touch Podfile


  这样,在目录下就会有这个文件。

  1.10 编辑此Podfile文件,因为我们已经进入了项目文件,所以可以直接编辑:(也可以不使用vi,直接用记事本或者编辑器进行编辑保存)

vi Podfile


  输入i就是开始编辑了,输入的内容如下:

platform :ios, '6.0'
pod 'JSONKit'
pod "AFNetworking'


  按ESC键,再按住shift加冒号,再在Terminal里面输入wq就写入加退出了。

  1.11 引入

pod install


  提示如下:

/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/universal-darwin14/rbconfig.rb:213: warning: Insecure world writable dir /usr/local in PATH, mode 040777
Updating local specs repositories

CocoaPods 0.39.0.beta.4 is available.
To update use: `gem install cocoapods --pre`
[!] This is a test version we'd love you to try.

For more information see http://blog.cocoapods.org and the CHANGELOG for this version http://git.io/BaH8pQ. 
Analyzing dependencies
Downloading dependencies
Installing AFNetworking (0.10.1)
Installing JSONKit (1.4)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `cocoapodsTest.xcworkspace` for this project from now on.
Sending stats


  这样就完成了。将XCode退出,再打开后来生成的.xcworkspace文件就可以看到整个项目了。

  1.12bug出现: direct access to Objective-C's isa is deprecated in favor of object_getClass()十有八九会出现这个,反正我是出现了。解决办法:

将cocoapods自动生成的Pods项目文件的Build Settings 从项目中搜索 Direct usage of 'isa' 将 YES(treat as error) 改为NO就可以了。原本项目本身的不用管。

  这样,再在原来的项目里引入第三方库就可以了。和本身导入文件是一样的。

OVER。 QQ群:461093715

  11月3日 更新。经博友留言才知晓,一试果然不能用了。而且淘宝镜像的首页已经贴出了通知。

12.9日更新:

1.如何移除CocoaPods:

1.1 which pod

会告诉我们pod安装在哪个路径

1.2 sudo rm -rf path

提示我们输入密码,然后这个cocoapods就被移除了,在搜索时,使用pod就没有反应了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: