CocoaPods使用详解

来源:互联网 发布:linux net snmp使用 编辑:程序博客网 时间:2024/06/07 07:23

1.CocoaPods介绍:

cocoapods是iOS最常用最有名的类库管理工具

iOS开发过程中不可避免的要使用第三方框架,​CocoaPods的出现能够让我们更好的管理第三方框架,使项目中使用的第三方框架更新更加方便快捷

2.​CocoaPods的安装

安装之前需要明确的是​CocoaPods是在国外的服务器,需要利用淘宝的Ruby镜像访问CocoaPods

下面是设置淘宝的Ruby镜像以及安装CocoaPods

1.终端中查看​源  $ gem sources -l

可以看到有两个服务器,一个是rubygems.org的,一个是ruby.taobao.org

$ gem sources -l

*** CURRENT SOURCES ***

https://rubygems.org/

https://ruby.taobao.org

2.删除rubygems.org     $sudo gem sources -r https://rubygems.org/

​ 输入命令之后需要输入密码

$ sudo gem sources -r https://rubygems.org/

https://rubygems.org/ removed from sources

3.终端中查看​源  $ gem sources -l

可以看到只剩下ruby.taobao.org

$ gem sources -l

*** CURRENT SOURCES ***

https://ruby.taobao.org

4.设置源:$sudo gem sources -a https://ruby.taobao.org/

​$ sudo gem sources -a https://ruby.taobao.org/

https://ruby.taobao.org/ added to sources

5.安装:$sudo gem install cocoapods

6.验证安装:当cocoapods安装完成之后就可以使用pod命令,终端输入pod,回车​就可以看到pod的命令了

gem 常用命令​总结:

# 查看gem源$ gem sources –l

# gem自身升级$ sudo gem update --system

# 查看版本$ gem --version

# 清除过期的gem$ sudo gem cleanup

# 安装包$ sudo gem install cocoapods

# 删除包$ gem uninstall cocoapods

# 更新包$ sudo gem update

# 列出本地安装的包$ gem list​​​​​​​

​3.OC中利用终端操作cocoapods加载第三方框架

1.终端中cd 项目根目录(根目录:将项目所在的文件件直接拖入终端即可)​  $ cd 项目根目录

2.创建Podfile     $ vim podfile

3.进入编辑模式,编辑​​podfile   $ i 

4.输入需要的具体框架 格式  pod '框架名称'

5.退出编辑模式 按ESC  之后输入  :wq ​

6.下载/更新第三方框架  pod update --no-repo-update

7.成功之后会提示:

请关闭当前的Xcode会话,并打开    xxxx.xcworkspace文件

[!] Please close any current Xcode sessions and use `xxxxx.xcworkspace` for this project from now on.Sending statsPod installation complete! There is 1 dependency from the Podfile and 1 total podinstalled.apple:text apple$

​4.Swift项目使用终端操作CocoaPods管理第三方框架

1.cd 项目文件夹

2.创建默认的 Podfile     $ pod init

此时在项目所在的文件夹下可以看到生成了一个Podfile文件,用Xcode打开这个文件

3.查看Podfile文件​

# Uncomment this line to define a global platform for your project

# platform :ios, '8.0'//指定下载的第三方框架,支持的最低iOS版本

# Uncomment this line if you're using Swift//如果想要支持Swift打开下面这句话

# use_frameworks!

//删掉下面语句

target 'text'

doend

4.搜索想要使用的框架(以AFNetworking为例)

    回到终端,输入 $pod search afn

    此处不区分大小写也不要求写完整

找到AFNetworking框架

-> AFNetworking (3.1.0) A delightful iOS and OS X networking framework. pod 'AFNetworking', '~> 3.1.0' - Homepage: https://github.com/AFNetworking/AFNetworking - Source: https://github.com/AFNetworking/AFNetworking.git

5.拷贝上面红色的内容,到Podfile文件,并关闭此文件
​# Uncomment this line to define a global platform for your project

# platform :ios, '8.0'

# Uncomment this line if you're using Swift

use_frameworks! //打开这句话表示支持Swift

pod 'AFNetworking'​

6.下载/更新第三方框架​

$ pod install --no-repo-update

6.成功之后会提示:

请关闭当前的Xcode会话,并打开    xxxx.xcworkspace文件

[!] Please close any current Xcode sessions and use `xxxxx.xcworkspace` for this project from now on.Sending statsPod installation complete! There is 1 dependency from the Podfile and 1 total podinstalled.apple:text apple$

5.删除不想使用的第三方框架

Podfiles文件中删除不需要的框架

然后终端输入命令$ pod update --no-repo-update

填写图片摘要(选填)

6.利用可视化界面,加载第三方框架

1.安装可视化界面,形成一个可视化的窗口,在窗口中安装这些第三方框架,xcode一个插件Alcatraz

  • github 地址:https://github.com/supermarin/Alcatraz

安装Alcatraz之后点击Window ->Package Manager

填写图片摘要(选填)

之后弹框:搜索cocoapods

填写图片摘要(选填)

利用可视化界面,通过CocoaPods加载第三方框架

完全退出xcode,新建一个项目,然后product->CocoaPods->Create/Edit Podfile

填写图片摘要(选填)

打开这个文件之后,这个文件保存的就是第三方框架的名字,删除其余内容,修改pod后面为需要的第三方框架的名字,此处以AFN框架举填写图片摘要(选填)

填写图片摘要(选填)

之后查看,Install Pods就能够点击了,点击

填写图片摘要(选填)

之后控制台打印出以下代码,等待一会如果没有反应

填写图片摘要(选填)

​则打开终端

cd  拖入项目所在的文件夹 回车

然后输入

pod install --verbose --no-repo-update //忽略本地库,直接从网上下载最新的

总结:

pod 命令汇总​

# 第一次使用安装框架   $ pod install

今后升级、添加、删除框架,或者框架不好用    $ pod update

# 搜索框架    $ pod search XXX

# 在项目文件夹下创建默认的 Podfile      $ pod init

Pod file 格式说明

# 最低支持的 iOS 版本 platform :ios, '8.0' 

# Swift 项目需要将框架转换为 frameworks 才能使用 use_frameworks! 

# 框架列表 pod 'AFNetworking'

0 0
原创粉丝点击