iOS技术资料iOSiOS相关

(1)制作CocoaPod依赖库

2017-04-25  本文已影响346人  mkb2


1.在github上创建一个共有库

CocoaPod和github的关系

首先我们要将代码提交到github中,然后给项目配置相应的.podspec文件,然后才可以使用CocoaPod去下载代码,实际上前者是代码用来托管代码,后者是用来搜索项目的,当去下载的时候,其实还是到github去下载的

创建一个库 创建库的细节,那个是OC的,做个样子,我们本文讲的是swift库,名称叫做WaterFlowView

说明:

2.clone到本地,在该文件夹内创建一个新的项目

2.1 使用命令行clone库到本地

git clone https://github.com/wangxin20111/WaterFlowView.git
空项目是这个样子的

2.2 创建项目在这个文件夹中
创建项目有点说道,就是我们要起名,叫做XXXDemo或者是XXXExample,例如,我们的项目WaterFlowViewDemo

2.3 创建一个文件夹,用来盛放我们制作的插件
到项目的正式目录下,然后去创建文件夹

新增了文件夹

注意:
1.文件夹一定要和库名称相同(不相同也没毛病,但是很多事情不方便)
2.绿色的这两个要放在一列,相同的等级

2.4 向项目中添加我们要给用户的文件

然后将WaterFlowView(2这个位置)的拖到项目中

千万不要选中

因为你拖到了本地,如果是复制了一份,那么将来有可能你修改了那些文件,他们因为是copy过来的,所以你修改了,但是源文件没有修改,切记!!!
也许你会问:为什么要将我已经写好的文件复制到1 放到这里这个位置?
因为如果不这样的话,文件可能是从其他位置引入的,太远了,要放到咱们的项目中!!!

我已经写完了,直接拖过来的

为甚么给你们看这个图,是因为我用了一个展位图,但是这个图片的名称有可能起的很简单,导致了和系统的名称相同,有可能冲突了,所以要去避免冲突,方法是将图片放到bundle中
方法四部曲:

第一步.原始的照片和文件混放 第二步.将照片专门放到文件夹中(在finder中处理这一步,只有在finder中创建出的文件夹,才会真的创建) 第三步.将文件改成bundle 第四步.将修改后的东西导入项目,然后修改一下报错。 红色的是整个对外的库,蓝色的是bundel文件,和库名称相同
//过去给自己的库中设置照片是这样的:
 imageView.image = UIImage.init(named: "placeholder")

//使用了bundle之后,我们设置照片是这样的
 imageView.image = UIImage.init(named: "WaterFlowView.bundle/placeholder")

3.上传项目到github

git add .
git commit -m "修改的具体内容"
git push
最后的效果

4.创建或登录trunk账号

我们要让用户使用我的的库,就要上传到CocoaPod托管,我们要上传文件到CocoaPod,就要使用trunk
使用terminal创建或者登录

4.1 检测是否已经登录

pod trunk me

打印看看有没有数据,如果没有那么创建

4.2 创建pod trunk 用户

pod trunk register  邮箱 '用户名' --description='电脑描述'

//例如
pod trunk register  邮箱 'wangxin20111' --description='wangxin20111_mac'

4.3 查收邮件
如果是QQ邮箱,可能会被放到“垃圾箱”中,并不一定是“收件箱”,有的时候,经常出不来,有可能是你的邮件进入了垃圾邮箱,去看看注意一下

邮件来了,点击邮件的链接,验证一下就行了,会跳转到一个网络页面即可

4.4 检验用户
执行操作1

pod trunk me操作

5.添加podspec文件

已经将代码提交到github上,做了代码的托管,但是并没有上传到CocoaPod上,podspec文件就是帮助用户搜索到我们制作的库的文件

5.1 创建.podspec文件有两种方法

先来看看OC的

//XLForm库的内部       OC版本
Pod::Spec.new do |s|
  s.name     = 'XLForm'
  s.version  = '3.3.0'
  s.license  = { :type => 'MIT' }
  s.summary  = 'XLForm is the most flexible and powerful iOS library to create dynamic table-view forms.'
  s.description = <<-DESC
                    The goal of the library is to get the same power of hand-made forms but spending 1/10 of the time. XLForm provides a very powerful DSL used to create a form, validate & serialize the form data. It keeps track of this specification on runtime, updating the UI on the fly.
                  DESC
  s.homepage = 'https://github.com/xmartlabs/XLForm'
  s.authors  = { 'Martin Barreto' => 'martin@xmartlabs.com' }
  s.source   = { :git => 'https://github.com/xmartlabs/XLForm.git', :tag => s.version }
  s.source_files = 'XLForm/XL/**/*.{h,m}'
  s.requires_arc = true
  s.ios.deployment_target = '7.0'
  s.ios.frameworks = 'UIKit', 'Foundation', 'CoreGraphics'
  s.resource = 'XLForm/XLForm.bundle'
end
*********************************************************************************************
//MJExtension
Pod::Spec.new do |s|
  s.name         = "MJExtension"
  s.version      = "3.0.13"
  s.ios.deployment_target = '6.0'
  s.osx.deployment_target = '10.8'
  s.summary      = "A fast and convenient conversion between JSON and model"
  s.homepage     = "https://github.com/CoderMJLee/MJExtension"
  s.license      = "MIT"
  s.author             = { "MJLee" => "199109106@qq.com" }
  s.social_media_url   = "http://weibo.com/exceptions"
  s.source       = { :git => "https://github.com/CoderMJLee/MJExtension.git", :tag => s.version }
  s.source_files  = "MJExtension"
  s.requires_arc = true
end

因为我的是Swift版本,所以复制了一个Swift的库

Pod::Spec.new do |s|
  s.name         = "SwiftTheme"
  s.version      = "0.3.2"
  s.summary      = "Powerful theme/skin manager for iOS 7+ 主题/换肤, 夜间模式"
  s.homepage     = "https://github.com/jiecao-fm/SwiftTheme"

  s.license      = 'MIT'
  s.author       = { "GeSen" => "i@gesen.me" }
  s.source       = { :git => "https://github.com/jiecao-fm/SwiftTheme.git", :tag => s.version.to_s }

  s.platform     = :ios, '8.0'
  s.requires_arc = true

  s.source_files = 'Source'
end

需要注意的是s.source_files = 'Source',这句话才是精髓,就是你给别人使用的库在那个文件,你就说就好了,我的是WaterFlowView, (详情参考图:四部曲-第四步)

搞定了podsepc文件效果图

6.上传到github,添加tag标记

git add .
git commit -m "添加了 .podspec文件"
git push

//这句话就是给我们的库文件打tag,这个tag一定要和.podspec文件内容一一对应,否则有问题
//将来每次更新我们自己的库文件的时候,要去升级库的版本号,要做两步
//1.修改.podspec文件中的数字      2.给我们的库打tags,如下所示
git tag 0.0.1
git push --tags

7.验证podspec文件

在.podspec的上一层文件去执行验证命令

我们去验证自己的库文件的时候,这样写

pod spec lint
或者
pod spec lint waterFlowView.podspec

但是有的时候会有一些warning,然后就验证失败,所以我们此时可以

pod spec lint waterFlowView.podspec --allow-warnings

PS:这里遇到的问题

- ERROR | [iOS] unknown: Encountered an unknown error (Simulator iPhone 4s is not available.) during validation.

原因是好多个,最后我是通过查看自己的pod版本,升级了就好了
升级代码:sudo gem install -n /usr/local/bin cocoapods --pre

[!] The spec did not pass validation, due to 2 errors and 1 warning.
[!] The validator for Swift projects uses Swift 3.0 by default, if you are using a different version of swift you can use a `.swift-version` file to set the version for your Pod. For example to use Swift 2.3, run:
    `echo "2.3" > .swift-version`.
wangxin:WaterFlowView wangxin$

直接在terminal中输入echo "3.0" > .swift-version 就行了

wangxin:WaterFlowView wangxin$ pod spec lint

 -> WaterFlowView (0.0.1)
    - ERROR | xcodebuild: Returned an unsuccessful exit code. You can use `--verbose` for more information.
    - ERROR | [iOS] xcodebuild:  WaterFlowView/WaterFlowView/WaterFlowCell.swift:19:13: error: value of type 'UIImageView' has no member 'sd_setImage'
    - WARN  | [iOS] xcodebuild:  WaterFlowView/WaterFlowView/WaterFlowView.swift:89:16: warning: variable 'h' was written to, but never read

Analyzed 1 podspec.

在项目中,我以为是sd影响了我的项目,然后就去移除,注释,terminal中清除pod的缓存,重启机,均失败。

解决办法(清除缓存)二者都行
pod cache clean --all
pod cache list

最后果断删除github上的项目,然后清除本地的,重新按照上边的步骤来了一遍,好了

8.上传到Cocoapod中

如果刚才验证,没有发现警告,那么可以直接这个样上传

pod trunk push waterFlowView.podspec

如果有警告,那么我们要去修改警告或者忽略警告

pod trunk push waterFlowView.podspec --allow-warnings

在上传的时候,要等几分钟才行

//上传成功的提示
🎉  Congrats
🚀  WaterFlowView (0.0.1) successfully published
📅  April 25th, 00:08 
🌎  https://cocoapods.org/pods/WaterFlowView 
👍  Tell your friends!

9.验证自己的库是否上传成功(大约半小时左右,时间不一定)

我们要去搜索一下自己的库是否成功了,打算在命令行中搜索一下
但是一般直接pod search WaterFlowView是不行的

因为CocoaPod本身一直在集成很多开发者的库,他的master分支一直是在更新的,但是你本地的库相对于刚才上传WaterFlowView的时间节点你本地的库是老的
所以我们要去更新一下本地的库

pod setup //初始化
pod repo update //更新仓库
pod search WaterFlowView //查找

一般情况下就能查看到了

** PS:这一步所遇问题 **

我的过了一下午也没有search到,然后我去网上查了一下,可能就是慢;
用两点验证自己是否上传成功:

只能漫长的等等,然后update一下

10.如何升级自己的库

其实刚才已经说了
1.修改项目
2.修改.podspece内部的版本号
3.上传github
4.打tag
5.上传tags即可

上一篇 下一篇

猜你喜欢

热点阅读