iOS开发iOS开发技能集锦

iOS - 封装的工具类上传到cocoapods

2019-01-08  本文已影响63人  BlackStar暗星

记录一次上传文件到 pod 的初级体验

iOS常用第三方管理工具cocoapods,很多人都在用,也确实方便。那么如何将自己的代码上传到cocoapods呢,作为萌新就baidu、google呗。
当然,作为萌新,尝试的时候肯定都是坑啊,要不然咋是萌新呢。
ps :诀窍--尝试的时候尽可能精简,这样容易过,一次一次增加难度,这样出现问题的时候就不会太多,解决起来也不会有砸电脑的冲动^_^

Pod::Spec.new do |s|

  s.name         = "BSAFNetWorking"
  s.version      = "0.1.0"
  s.summary      = "AFNetWorking changed BSAFNetWorking"
  s.description  = <<-DESC
                        封装AFNetWorking 提供提供body体请求(setHTTPBody方式),提供表单格式方式请求(application/x-www-form-urlencoded)
                        提供上传、下载、普通网络请求
                      DESC

  s.homepage        = "https://github.com/BlackStarLang/BSAFNetWorking.git"
  s.author          = { "BlackStar" => "blackstar_lang@163.com" }
  s.platform        = :ios, "8.0"
  s.source          = { :git => "https://github.com/BlackStarLang/BSAFNetWorking.git", :tag => s.version }
  s.source_files    = "BSAFNetWorking/SQBaseApi/BSAFNetwroking.h"
  s.public_header_files    = "BSAFNetWorking/SQBaseApi/BSAFNetwroking.h"
  s.framework       = "UIKit"
  s.dependency "AFNetworking", "~> 3.0"
  s.license= { :type => "MIT", :file => "LICENSE" }

  s.subspec 'BSApi' do |ss|
    ss.source_files = "BSAFNetWorking/SQBaseApi/BSApi/*"
    ss.framework    = "UIKit"
end

通过命令行创建的文件是带有很多注释的,不过很多我都用不上,为了看的清除,我把没用的都删了。如果想看所有的,可以看看具体的项,以便于增加或修改spec项

spec文件中的每行代码的意思差不多翻译就可以了

Copyright (c) 2018 BlackStar <blackstar_lang@163.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
  1. s.dependency 是你项目需要依赖的第三方,比如说我的这个是封装的AFNetworking ,肯定需要依赖AFNetworking的,所以,依赖一个写一个,依赖多个写多个,如下:
  s.dependency "AFNetworking", "~> 3.0"
  s.dependency 'SDWebImage','~> 4.1.0'
  1. s.framework 是依赖的系统框架,我这里只用了 UIKit。
    如果依赖多个系统框架,可以一行搞定,但是 frameworks 是带有s的哦,这个和依赖单个框架是不一样的。
s.frameworks       = "UIKit","UIFoundation"

5.特别注意 s.source_files 坑很多,一定要搞准你要上传文件的目录位置,要不然一遍一遍过不去
搞不准目录写的对不对点击这里看demo
我们的pod文件可能是有多个文件夹的,如果不特殊处理,上传后的文件是没有分目录的,都在一个文件夹里。想要带有目录结构需要将每一个文件夹设置成子模块,具体写法如下

Pod::Spec.new do |s|

  s.name         = "BSAFNetWorking"
  s.version      = "0.1.0"
  s.summary      = "AFNetWorking changed BSAFNetWorking"
  s.description  = <<-DESC
                        封装AFNetWorking 提供提供body体请求(setHTTPBody方式),提供表单格式方式请求(application/x-www-form-urlencoded)
                        提供上传、下载、普通网络请求
                      DESC

  s.homepage        = "https://github.com/BlackStarLang/BSAFNetWorking.git"
  s.author          = { "BlackStar" => "blackstar_lang@163.com" }
  s.platform        = :ios, "8.0"
  s.source          = { :git => "https://github.com/BlackStarLang/BSAFNetWorking.git", :tag => s.version, :submodules => true}
  s.source_files    = "BSAFNetWorking/SQBaseApi/BSAFNetwroking.h"
  s.public_header_files    = "BSAFNetWorking/SQBaseApi/BSAFNetwroking.h"
  s.framework       = "UIKit"
  s.dependency "AFNetworking", "~> 3.0"
  s.license= { :type => "MIT", :file => "LICENSE" }

  s.subspec 'BSApi' do |ss|
    ss.source_files = "BSAFNetWorking/SQBaseApi/BSApi/*"
    ss.framework    = "UIKit"
end
end

看好几个end哦。
参考demo

pod cache clean --all
//然后在
pod spec lint xxx.podspec
//终端执行
pod trunk me //检查是否有账户
//没有则注册,邮箱地址需要真实有效,需要接收邮件验证,好像需要翻墙,不大记得了
pod trunk register 邮箱地址 "昵称" 
//例子:
pod trunk register blackstar_lang@163.com "BlackStar"
pod trunk push xxxx.podspec --allow-warnings
cd ~/Library/Caches/CocoaPods/ 
//然后执行
rm -rf search_index.json

重新 pod search 就可以了

这就是此次发布pod的全过程了,如果你也遇到了问题,可以留言共同研究哦

上一篇 下一篇

猜你喜欢

热点阅读