iOS CocoaPods 入门学习指南

文章目录

一、关于CocoaPods
     1、什么是CocoaPods
     2、安装使用CocoaPods
     3、pod install 和 pod update 区别
二、制作CocoaPods准备工作
     1、注册Trunk账号
     2、注册github账号
     3、安装git
     4、配置git
     5、配置ssh key
 三、让你的库支持CocoaPods
    1、创建项目: repository 或 start a project
    2、填写项目信息
    3、clone项目到本地
    4、x-code创建项目并保存到克隆的文件夹下面
    5、commit 并push 到github
    6、制作podspec,填写规范
    7、验证podspec正确性
    8、提交我们的开源项目到cocoapods
    9、编辑README文件
    10、常见问题总结
 四、常用的git命令行
   1、仓库
   2、配置
   3、增加、删除文件
   4、代码提交
   5、分支
   6、标签
   7、查看信息
   8、远程同步
   9、撤销
五、git常见的GUI工具
   1、主要介绍sourceTree(对于git命令的封装而已)
   2、git推荐教程
   3、git官网 https://git-scm.com/

一、关于CocoaPods

1、什么是CocoaPods
CocoaPods管理X-code项目的库依赖项项目的依赖项在名为Podfile的单个文本文件中指定。CocoaPods将解析库之间的依赖关系,获取生成的源代码,然后在Xcode工作区中将它们链接在一起以构建项目。最终目标是通过创建更加集中的生态系统来提高第三方开源库的可发现性和参与度。

2、安装使用CocoaPods
CocoaPods是用Ruby构建的,它可以使用macOS上的默认Ruby进行安装。您可以使用Ruby版本管理器(例如:rvm)。使用macOS上提供的标准Ruby即可。
具体使用教程戳这里

3、pod install 和 pod update 区别
当你需要向向你的项目中安装新的pod库时使用pod install。即使之前你已经有一个Podfile并且执行了pod install,即使你是在向一个已经使用了CocoaPods的项目中添加或移除pod库。
只有当你想要更新pod库的版本时才使用pod update

pod install
这是在您第一次要检索项目的pod时使用,但每次编辑Podfile以添加,更新或删除Pod时都会使用。每次pod install命令运行的时候,pod install会为每一个它安装的pod库在Podfile.lock文件中写入其版本号。Podfile.lock文件追踪每一个安装的pod库的版本号,并锁定这些版本号。对于在Podfile.lock文件中的pod库,pod install会只下载Podfile.lock文件中指定的版本,而不会去检查这个库是否有更新的版本。 对于不在Podfile.lock文件中的pod库,pod install会搜索这个pod库在Podfile文件中指定的版本。

pod update 或 pod update [podName]
当你运行了pod update podName命令,CocoaPods会在不考虑Podfile.lock中版本的情况下试着去查找podName的最新版本。pod update podName命令会将相应的pod更新到最新的版本。

二、制作CocoaPos

1、注册Trunk账号

pod  trunk register 邮箱  '用户名' 
例如:pod trunk register 12345678@qq.com 'zhansan'

注册成功后到邮箱进行确认

确认完成后,查看个人信息

pod trunk me 

2、注册github账号
https://github.com/

3、安装git

查看是否安装过git

git

安装过则输出

usage: git [--version] [--help] [-C <path>] [-c name=value]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone      Clone a repository into a new directory
   init       Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
   add        Add file contents to the index
   mv         Move or rename a file, a directory, or a symlink
   reset      Reset current HEAD to the specified state
   rm         Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
   bisect     Use binary search to find the commit that introduced a bug
   grep       Print lines matching a pattern
   log        Show commit logs
   show       Show various types of objects
   status     Show the working tree status

grow, mark and tweak your common history
   branch     List, create, or delete branches
   checkout   Switch branches or restore working tree files
   commit     Record changes to the repository
   diff       Show changes between commits, commit and working tree, etc
   merge      Join two or more development histories together
   rebase     Reapply commits on top of another base tip
   tag        Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
   fetch      Download objects and refs from another repository
   pull       Fetch from and integrate with another repository or a local branch
   push       Update remote refs along with associated objects

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.

通过homebrew安装git,先安装homebrew

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

安装git

brew install git

4、配置git

查看是否配置了

git config user.name
git config user.email

配置用户名和邮箱

git config --global user.name "用户名"
git config --global user.email "邮箱"

注意: 用户名和邮箱是git提交代码时用来显示你身份和联系方式的,并不是github用户名和邮箱

5、配置ssh key

克隆的两种方式 : https/ssh (ssh局限于项目创建者或参与者)

git clone  https://xxx.git
git clone git@github.com:xxx.git

任何工程都可以使用https去clone,但是提交权限只有当你是项目的所有者或参与者,你才具备提交权限。每次push代码到远端仓库需要校验userName和password,配置ssh后,就可以直接提交不校验

检查电脑是否配置过ssh,以下两个命令就是检查是否已经存在 id_rsa.pub (公钥)或 id_dsa.pub (私匙)文件

cd ~/.ssh
ls

生成秘钥对

ssh-keygen -t rsa -C '邮箱'   

连续回车,输入2次密码继续回车即可
在这里插入图片描述
可以在用户主目录里找到.ssh目录,里面有id_rsa和id_rsa.pub两个文件,这两个就是SSH Key的秘钥对,id_rsa是私钥,不能泄露出去,id_rsa.pub是公钥

打开github,登录个人账号,点击settting,点击ssh,如下图
在这里插入图片描述

打开公钥文件id_rsa.pub(直接拖进x-code),全部复制
在这里插入图片描述

测试git是否与github绑定成功

ssh git@github.com

输入密码后回车

Hi  'github账号的用户名! ' You've successfully authenticated, 
but GitHub does not provide shell access.

至此git 与github绑定成功。

三、让你的库支持CocoaPos

1、创建项目: repository 或 start a project
在这里插入图片描述

2、填写项目信息
在这里插入图片描述

3、clone项目到本地
找到克隆路径
在这里插入图片描述
打开终端,cd到桌面,执行

git clone  路径
例如 : git clone https://github.com/AFNetworking/AFNetworking.git

在这里插入图片描述

克隆完成桌面就有创建的工程
在这里插入图片描述

4、x-code创建项目并保存到克隆的文件夹下面
在这里插入图片描述

5、commit 并push 到github

#进入到工程路径
cd   项目拖进终端
# 文件添加到仓库(. 代表添加所有文件)
git  add .
#文件添加到仓库
git commit -m "提交信息"
#推送到远程仓库
git push

在这里插入图片描述
到此,你的项目就已经上传到github上。
在这里插入图片描述

下面的步骤开始,是将你封装的库开源到github,让别人pod你的封装库
6、制作podspec文件,填写规范

#进入到项目根目录
cd 拖进终端
#新建一个后缀为.podspec文件
pod spec create xxx (注意:xxx一般都是项目名) 

xxx.podspec可以直接拖进x-code打开编辑

Pod::Spec.new do |s|

#项目名
  s.name         = "videoEdit"
#版本号
  s.version      = "0.0.1"
#版本号
  s.summary      = "A short description of videoEdit."
#项目描述
  s.description  = <<-DESC
#填写项目描述
                   DESC
#github项目主页
  s.homepage     = "http://EXAMPLE/videoEdit"
#效果图,多张用逗号分隔开
  s.screenshots  = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif"
#开源许可
  s.license      = "MIT (example)"
#作者信息(可以以下4中格式)
  s.author             = { "Cherish" => "39xxx1825@qq.com" }
  # Or just: s.author    = "Cherish"
  # s.authors            = { "Cherish" => "39xxx1825@qq.com" }
  # s.social_media_url   = "http://twitter.com/Cherish"
#支持平台兼容的最新版本(可以以下2种格式)
  # s.platform     = :ios
  # s.platform     = :ios, "5.0"
#项目源码
  s.source       = { :git => "http://EXAMPLE/videoEdit.git", :tag => "#{s.version}" }
#项目源码目录(即被pod封装库)
  s.source_files  = "Classes", "Classes/**/*.{h,m}"
  s.exclude_files = "Classes/Exclude"
#公开头文件地址
  # s.public_header_files = "Classes/**/*.h"
#项目依赖的库,多个库用逗号分隔
  # s.framework  = "SomeFramework"
  # s.library   = "iconv"
  # s.libraries = "iconv", "xml2"
#要求ARC环境
  # s.requires_arc = true
#xcode里的config文件
  # s.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }
#依赖的其他仓库,如果有多个需要填写多个s.dependency
  # s.dependency "JSONKit", "~> 1.4"

end

例如,项目videoEdit的Lib实体文件夹下的文件你已经封装成库,准备开源
在这里插入图片描述

根目录下的videoEdit.podspec可以简写如下

Pod::Spec.new do |s|
  s.name         = "videoEdit"
  s.version      = "1.0.0"
  s.summary      = "视频编辑demo"
  s.homepage     = "https://github.com/CoderYangFeiLong/videoEdit"
  s.license      = "MIT"
  s.author       = { "Cherish" => "390151825@qq.com" }
  s.platform     = :ios
  s.source       = { :git => "https://github.com/CoderYangFeiLong/videoEdit.git", :tag => "1.0.0" } #这边如果打了tag,远程仓也需要打tag
  s.source_files  = "videoEdit/videoEdit/Lib/*.{h,m}"
  s.requires_arc = true
end

保存后,将代码commit并push

git commit -m Release 1.0.0
git tag 1.0.0
git push --tags  
git push origin master

7、验证podspec正确性

pod lib lint
或者 
pod lib lint --allow-warnings 

没什么大问题就会显示通过了videoEdit passed validation.
在这里插入图片描述

8、提交我们的开源项目到cocoapods

 pod trunk push xxx.podspec
 例如: pod trunk push videoEdit.podspec

显示通过Validating podspec
在这里插入图片描述

正常的pod search试试pod search xxx
在这里插入图片描述

我们来测试下,X-code随意创建个project,TestDemo, 创建Podfile文件,执行pod install
在这里插入图片描述
动手试试吧!

9、编辑README文件

github上README.md的编写,在大多数情况下与csdn的MarkDown编辑器一样的,但是它回车不一样,在README.md上用
代替回车。在不需要上传图片等资源时,基本上只需要注意回车即可。

关于标题

# 一级标题
## 二级标题
### 三级标题
注意:#和文字之间有个空格

关于粗体、斜体

**粗体**
*斜体*
***粗体加斜体***

关于代码块(esc下面那个按键连续3下,成对出现)
在这里插入图片描述

关于表

|列名1|列名2|
|:---|:---|
|列1的内容1|列2的内容1|
|列1的内容2|列2的内容2|

效果如下

列名1列名2
列1的内容1列2的内容1
列1的内容2列2的内容2

关于超链接

[描述文字](链接地址)
例如 :[百度](https://www.baidu.com/)

关于效果图

我一般是将效果图放工程里,推到远程仓库,再调用图片链接。
格式

![image](图片地址)

如何获取图片链接:右键复制图片地址

如何修改效果图样式

# 将图片制成 200 *200 
<img src="图片地址" width="200" height="200" alt="描述语言,写什么都不会显示出来"/>
# 200*200,居中显示
<div align=center><img width="200" height="200" src="图片地址"/></div>

10、常见问题总结

(1) pod search过程中失败, 或出现这类字样

[!] Unable to find a pod with name, author, summary, or descriptionmatching ...

解决方案 : 可以通过重新创建索引来解决,再次搜索时,会自动重新创建索引。

rm ~/Library/Caches/CocoaPods/search_index.json

(2) 在验证podspecpod lib lint文件发生的错误

 - ERROR | [iOS] file patterns: The `source_files` pattern did not match any file.

这个是在指定共享的类库时, 文件路径不对, 也就是设置s.source_files 字段时, 发生了错误。修改对了即可。

其他问题后续补充吧!

四、常用的git命令行

1、仓库

# 在当前目录新建一个Git代码库
$ git init

# 新建一个目录,将其初始化为Git代码库
$ git init [project-name]

# 下载一个项目和它的整个代码历史
$ git clone [url]

2、配置

# 显示当前的Git配置
$ git config --list

# 编辑Git配置文件
$ git config -e [--global]

# 设置提交代码时的用户信息
$ git config [--global] user.name "[name]"
$ git config [--global] user.email "[email address]"

3、增加、删除文件

# 添加指定文件到暂存区
$ git add [file1] [file2] ...

# 添加指定目录到暂存区,包括子目录
$ git add [dir]

# 添加当前目录的所有文件到暂存区
$ git add .

# 添加每个变化前,都会要求确认
# 对于同一个文件的多处变化,可以实现分次提交
$ git add -p

# 删除工作区文件,并且将这次删除放入暂存区
$ git rm [file1] [file2] ...

# 停止追踪指定文件,但该文件会保留在工作区
$ git rm --cached [file]

# 改名文件,并且将这个改名放入暂存区
$ git mv [file-original] [file-renamed]

4、代码提交

# 提交暂存区到仓库区
$ git commit -m [message]

# 提交暂存区的指定文件到仓库区
$ git commit [file1] [file2] ... -m [message]

# 提交工作区自上次commit之后的变化,直接到仓库区
$ git commit -a

# 提交时显示所有diff信息
$ git commit -v

# 使用一次新的commit,替代上一次提交
# 如果代码没有任何新变化,则用来改写上一次commit的提交信息
$ git commit --amend -m [message]

# 重做上一次commit,并包括指定文件的新变化
$ git commit --amend [file1] [file2] ...

5、分支

# 列出所有本地分支
$ git branch

# 列出所有远程分支
$ git branch -r

# 列出所有本地分支和远程分支
$ git branch -a

# 新建一个分支,但依然停留在当前分支
$ git branch [branch-name]

# 新建一个分支,并切换到该分支
$ git checkout -b [branch]

# 新建一个分支,指向指定commit
$ git branch [branch] [commit]

# 新建一个分支,与指定的远程分支建立追踪关系
$ git branch --track [branch] [remote-branch]

# 切换到指定分支,并更新工作区
$ git checkout [branch-name]

# 切换到上一个分支
$ git checkout -

# 建立追踪关系,在现有分支与指定的远程分支之间
$ git branch --set-upstream [branch] [remote-branch]

# 合并指定分支到当前分支
$ git merge [branch]

# 选择一个commit,合并进当前分支
$ git cherry-pick [commit]

# 删除分支
$ git branch -d [branch-name]

# 删除远程分支
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]

6、标签

# 列出所有tag
$ git tag

# 新建一个tag在当前commit
$ git tag [tag]

# 新建一个tag在指定commit
$ git tag [tag] [commit]

# 删除本地tag
$ git tag -d [tag]

# 删除远程tag
$ git push origin :refs/tags/[tagName]

# 查看tag信息
$ git show [tag]

# 提交指定tag
$ git push [remote] [tag]

# 提交所有tag
$ git push [remote] --tags

# 新建一个分支,指向某个tag
$ git checkout -b [branch] [tag]

7、查看信息

# 显示有变更的文件
$ git status

# 显示当前分支的版本历史
$ git log

# 显示commit历史,以及每次commit发生变更的文件
$ git log --stat

# 搜索提交历史,根据关键词
$ git log -S [keyword]

# 显示某个commit之后的所有变动,每个commit占据一行
$ git log [tag] HEAD --pretty=format:%s

# 显示某个commit之后的所有变动,其"提交说明"必须符合搜索条件
$ git log [tag] HEAD --grep feature

# 显示某个文件的版本历史,包括文件改名
$ git log --follow [file]
$ git whatchanged [file]

# 显示指定文件相关的每一次diff
$ git log -p [file]

# 显示过去5次提交
$ git log -5 --pretty --oneline

# 显示所有提交过的用户,按提交次数排序
$ git shortlog -sn

# 显示指定文件是什么人在什么时间修改过
$ git blame [file]

# 显示暂存区和工作区的差异
$ git diff

# 显示暂存区和上一个commit的差异
$ git diff --cached [file]

# 显示工作区与当前分支最新commit之间的差异
$ git diff HEAD

# 显示两次提交之间的差异
$ git diff [first-branch]...[second-branch]

# 显示今天你写了多少行代码
$ git diff --shortstat "@{0 day ago}"

# 显示某次提交的元数据和内容变化
$ git show [commit]

# 显示某次提交发生变化的文件
$ git show --name-only [commit]

# 显示某次提交时,某个文件的内容
$ git show [commit]:[filename]

# 显示当前分支的最近几次提交
$ git reflog

8、远程同步

# 下载远程仓库的所有变动
$ git fetch [remote]

# 显示所有远程仓库
$ git remote -v

# 显示某个远程仓库的信息
$ git remote show [remote]

# 增加一个新的远程仓库,并命名
$ git remote add [shortname] [url]

# 取回远程仓库的变化,并与本地分支合并
$ git pull [remote] [branch]

# 上传本地指定分支到远程仓库
$ git push [remote] [branch]

# 强行推送当前分支到远程仓库,即使有冲突
$ git push [remote] --force

# 推送所有分支到远程仓库
$ git push [remote] --all

9、撤销

# 恢复暂存区的指定文件到工作区
$ git checkout [file]

# 恢复某个commit的指定文件到暂存区和工作区
$ git checkout [commit] [file]

# 恢复暂存区的所有文件到工作区
$ git checkout .

# 重置暂存区的指定文件,与上一次commit保持一致,但工作区不变
$ git reset [file]

# 重置暂存区与工作区,与上一次commit保持一致
$ git reset --hard

# 重置当前分支的指针为指定commit,同时重置暂存区,但工作区不变
$ git reset [commit]

# 重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致
$ git reset --hard [commit]

# 重置当前HEAD为指定commit,但保持暂存区和工作区不变
$ git reset --keep [commit]

# 新建一个commit,用来撤销指定commit
# 后者的所有变化都将被前者抵消,并且应用到当前分支
$ git revert [commit]

暂时将未提交的变化移除,稍后再移入
$ git stash
$ git stash pop

五、git常见的GUI工具

1、主要介绍sourceTree(对于git命令的封装而已)

还没想好怎么写…

2、 廖雪峰 | git推荐教程

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值