尝试使用CocoaPad

来源:互联网 发布:贵州大数据管理局地址 编辑:程序博客网 时间:2024/06/10 21:06

甭管什么原因吧,反正现在要用CocoaPod了,刚开始不知道这是什么玩意儿,后来看了一些介绍明白了,其实就跟vim的插件管理工具Vundle一个意思,CocoaPod就是用来管理第3方库的。自己创建一个项目,你能保证你用的所有代码都是自己从系统接口开始撸出来的吗?不能吧,就算你愿意你老板也不愿意啊,所以这时就需要使用开源的第3方库来帮你完成部分功能,这时问题来了,什么都怕多,如果我用了很多的第3方库,以至于整个项目看起来几乎都是东拼西凑的了,那这么多的库咋管理?如果有个别的库有更新了呢?而且有些库在编译的时候需要添加一些编译变量,比如-Ddebug之类的,这些编译变量在使用Makefile时经常会遇到,就是那个意思。这时难道我要一个一个的去手动修改吗?答案肯定是否定的,这时CocoaPod就可以出场管理这些第3方库了,此时他就跟Vundle的功能一样,什么?你不知道Vundle是什么,那就当我没说吧。

首先是安装。最初我跟着网上的教程稀里糊涂的装了,但是不知道装哪里去了。

 ~/Work/src/ios/audio-stream/ pod setupSetting up CocoaPods master repo  $ /usr/bin/git clone https://github.com/CocoaPods/Specs.git master --progress  Cloning into 'master'...  remote: Counting objects: 1275860, done.  remote: Compressing objects: 100% (2182/2182), done.  remote: Total 1275860 (delta 1166), reused 37 (delta 37), pack-reused 1273503  Receiving objects: 100% (1275860/1275860), 398.79 MiB | 1.57 MiB/s, done.  Resolving deltas: 100% (628639/628639), done.  Checking connectivity... done.  Checking out files: 100% (156551/156551), done.Setup completed ~/Work/src/ios/audio-stream/ ls

执行上面的命令之后,仔细研究了一下,原来是在~/.cocoapods下面创建了一个索引文件。接下来就需要使用pod来下载安装第3方库了。这时我又一次疑惑了,该在哪个目录下载呢?突然醒悟了,当然是在哪里使用下载到哪个目录了,于是用xcode临时创建了一个project,使用命令行进入到该路径下,在根据教程操作。
比如说我刚才用xcode创建的project叫testpod,

 ~/Work/src/ios/testpod/ lstestpod           testpod.xcodeproj testpodTests      testpodUITests

执行

vi Podfile

最开始,按照教程的例子Podfile的内容是下面这个样子的:

platform :iospod 'JSONKit', '~> 1.4'pod 'Reachability', '~> 3.0.0'pod 'ASIHTTPRequest'pod 'RegexKitLite'

于是在安装那些个库的时候遇到下面的提示:

 ~/Work/src/ios/testpod/ pod installAnalyzing dependencies[!] The dependency `JSONKit (~> 1.4)` is not used in any concrete target.The dependency `Reachability (~> 3.0.0)` is not used in any concrete target.The dependency `ASIHTTPRequest` is not used in any concrete target.The dependency `RegexKitLite` is not used in any concrete target.

去查了一下,这是因为没有指定谁来用这些库的原因,即需要指明谁来用它们,于是改成下面这样:

platform :iostarget 'testpod' do        pod 'JSONKit', '~> 1.4'        pod 'Reachability', '~> 3.0.0'        pod 'ASIHTTPRequest'        pod 'RegexKitLite'end

testpod就是刚才创建的project名字。再次执行

 ~/Work/src/ios/testpod/ pod installAnalyzing dependenciesDownloading dependenciesInstalling ASIHTTPRequest (1.8.2)Installing JSONKit (1.4)Installing Reachability (3.0.0)Installing RegexKitLite (4.0)Generating Pods projectIntegrating client project[!] Please close any current Xcode sessions and use `testpod.xcworkspace` for this project from now on.Sending statsPod installation complete! There are 4 dependencies from the Podfile and 4 total pods installed. ~/Work/src/ios/testpod/

这时就已经下载安装好指定的第3方库了,同时需要注意的是,以后再打开工程是需要打开testpod.xcworkspace这个文件,而不是原来的那个xcodeproj文件了。

原创粉丝点击