美文网首页iOS开发iOS开发技能集锦
iOS - 封装的工具类上传到cocoapods

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

作者: BlackStar暗星 | 来源:发表于2019-01-08 19:16 被阅读63次

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

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

    • 新建项目,提交到GitHub或者其他代码托管网站,有下载链接就行,需要以tag或者commit 为目标代码
      • 我查看过一些其他人写的,会把要上传的文件提到项目根目录,或者复制一份,但是我觉得比较麻烦,还是按照正常的项目处理的,也是可以的,木有问题
    • 准备 podspec 文件
      • 打开终端,cd到项目当前跟目录,执行pod spec create xxxname
      • 具体内容
    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文件中的每行代码的意思差不多翻译就可以了

    • 划重点
      1. 注意示例汇中 s.description 的格式(当然,这个注释掉也可以)
      2. s.license 必须要有,我是到MIT官网找的,也可以是 .md & .txt格式的,或者其他协议 ,协议内容如下(只需要改变第一行版权所有人信息即可,其他不用动)
    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

    • podspec 文件准备好后,检查文件合法性pod spec lint xxxx.podspec --allow-warnings 需要加 --allow-warnings,要不然基本过不去
      可能报错 第三方引入有问题,让你用#import<>的方式引入,引入的时候主要要带有 文件路径引入,如引入AFNetworking需要这么写#import <AFNetworking/AFNetworking.h> 不能写#import <AFNetworking.h> 或者 #import "AFNetworking.h"
      如果改完代码 还是报这个错误,需要清理缓存 :
    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
    
    • 发布成功后,pod search xxxx 试试可不可以搜索出来,如果确定发布成功了,但是搜不出来,需要重置 pod的searchindex
    cd ~/Library/Caches/CocoaPods/ 
    //然后执行
    rm -rf search_index.json
    

    重新 pod search 就可以了

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

    相关文章

      网友评论

        本文标题:iOS - 封装的工具类上传到cocoapods

        本文链接:https://www.haomeiwen.com/subject/iepbrqtx.html