1、概要javascript
iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods)能够用来方便的统一管理这些第三方库。html
2、安装java
因为网上的教程基本都大同小异,但细节之处还不是很完善,因此借机会在这里补充下:ios
注:要使用CocoaPods,那就要下载安装它,而下载安装CocoaPods须要Ruby环境git
一、Ruby环境搭建github
当前安装环境为Mac mini 10.8.5。Mac OS自己自带Ruby,但仍是更新一下保险,由于我第一次安装在没有更新Ruby的状况下就失败了。vim
a 查看下当前ruby版本:打开终端输入 ruby -v(确实安装了,不过用这个版本接下来工做失败了,因此更新下ruby)xcode
终端输入以下命令(把Ruby镜像指向taobao,避免被墙,你懂得)ruby
gem sources --remove https://rubygems.org/
gem sources -a https://ruby.taobao.org/
gem sources -l (用来检查使用替换镜像位置成功)bash
二、下载安装CocoaPods
终端输入:sudo gem install cocoapods
这样就下载安装好了CocoaPods
三、使用CocoaPods
a 新建一个项目,名字PodTest
b 终端中,cd到项目总目录(注意:包含PodTest文件夹、PodTest.xcodeproj、PodTestTest的那个总目录)
接着上一步,终端输入 vim Podfile
键盘输入 i,进入编辑模式,输入
platform :ios, '7.0'
pod 'MBProgressHUD', '~> 0.8'
而后按Esc,而且输入“ :”号进入vim命令模式,而后在冒号后边输入wq
注意:键盘输入 :后,才能输入wq。回车后发现PodTest项目总目录中多一个Podfile文件
激动人心的时刻到了:肯定终端cd到项目总目录,而后输入 pod install,等待一会,大约3分钟。
查看项目根目录:
注意:如今打开项目不是点击 PodTest.xodeproj了,而是点击 PodTest.xcworkspace
打开项目后看到项目结构而且测试一下:
运行结果:
补充:
一、CocoaPods的基本安装及使用都详细的说明了,但还有一些补充,当须要同时导入多个第三方时候怎么办 ?
这就须要修改Podfile了,就是用vim编辑的那个保存在项目根目录中的文件,修改完了Podfile文件,须要从新执行一次pod install命令。
例如:
platform :ios
pod 'JSONKit', '~> 1.4'
pod 'AFNetworking', '~> 2.0'
二、CocoaPods能够查找你想要的第三方库
终端输入命令:pod search UI
疯了了,我怎么查找这么大众的关键字,好多库~~
而后从新编辑Podfile文件,按照以前的步骤,把更多的库都导入项目!
本博客盗窃自阿福的博客,地址:http://blog.csdn.net/lizhongfu2013/article/details/26384029
============================我是分割线==================================
CocoaPods更新了一下,而后:出现下面的警告
ex:
The dependency `` is not used in any concrete target The dependency `AFNetworking ` is not used in any concrete target
CocoaPods更新了一下,而后:
sudo gem install cocoapods --pre
安装cocoapods的预览版本,就会更新下来新的1.0.0.beta.2版本,以下所示:
Successfully installed cocoapods-1.0.0.beta.2 Parsing documentation for cocoapods-1.0.0.beta.2
很高兴啊,更新了新的版本,然而pod install就出错了,悲了个剧!出错以下:
Updating local specs repositories
Analyzing dependencies
[!] The dependency `FMDB (~> 2.3)` is not used in any concrete target. The dependency `SDWebImage (~> 3.6)` is not used in any concrete target. The dependency `AFNetworking (~> 2.3.0)` is not used in any concrete target. The dependency `DACircularProgress (~> 2.2.0)` is not used in any concrete target. The dependency `MBProgressHUD (~> 0.8)` is not used in any concrete target. The dependency `PSTCollectionView (~> 1.2.1)` is not used in any concrete target. The dependency `HPGrowingTextView (~> 1.1)` is not used in any concrete target. The dependency `ProtocolBuffers (= 1.9.3)` is not used in any concrete target. The dependency `leveldb-library (~> 1.18.1)` is not used in any concrete target. The dependency `SCLAlertView-Objective-C (~> 0.7.5)` is not used in any concrete target. The dependency `MWPhotoBrowser (~> 1.4.1)` is not used in any concrete target. The dependency `MMMarkdown (~> 0.5)` is not used in any concrete target. The dependency `MJExtension (~> 2.5.16)` is not used in any concrete target. The dependency `MJRefresh (~> 2.4.12)` is not used in any concrete target. The dependency `Masonry (~> 0.6.3)` is not used in any concrete target.
我用的三方库比较多,挺长的,出这个错是告诉咱们咱们所用的库没有指定target,它不知道用在哪里,因此就给报错了,而后我去了cocoapods的官网看了下,cocoapods官网地址
官网是这样给推荐的:
在建立Podfile的时候,用这种格式使用,
platform :ios, '8.0' use_frameworks! target 'MyApp' do pod 'AFNetworking', '~> 2.6' pod 'ORStackView', '~> 3.0' pod 'SwiftyJSON', '~> 2.3' end
里面的 MyApp 记得替换为本身攻城里面的target。这样就基本OK了,执行pod install / pod update 就均可以了。