美文网首页
Cocoapods发布属于自己的框架!

Cocoapods发布属于自己的框架!

作者: 沈正方 | 来源:发表于2018-04-18 14:50 被阅读27次

    (一)创建框架

    1. 在本地新建文件夹TabPageView
    2. 进入TabPageView,初始化git仓库(git init)
    3. 创建文件夹Sources(可以修改,在podspec文件中默认为Classes),该文件夹用来存储框架的源文件

    上述操作如下图一所示:

    图一

    (二)创建框架的描述信息

    1. 通过命令pod spec create TabPageView创建框架的描述信息文件
    2. 修改一些必要的描述信息,可以参考github上流行框架的.podspec文件(官方podspec介绍

    如下是Alamofire的.podspec文件

    Pod::Spec.new do |s|
      # 框架名称
      s.name = 'Alamofire'
      # 框架版本
      s.version = '4.7.2'
      # 框架协议
      s.license = 'MIT'
      # 框架简介
      s.summary = 'Elegant HTTP Networking in Swift'
      # 框架的github主页
      s.homepage = 'https://github.com/Alamofire/Alamofire'
      # 框架开发者主页
      s.social_media_url = 'http://twitter.com/AlamofireSF'
      # 框架作者及作者的联系邮箱
      s.authors = { 'Alamofire Software Foundation' => 'info@alamofire.org' }
      # 框架的下载地址和发布地址
      s.source = { :git => 'https://github.com/Alamofire/Alamofire.git', :tag => s.version }
      
      # 框架最低支持的iOS、Mac OS、TVOS & Watch OS Version
      s.ios.deployment_target = '8.0'
      s.osx.deployment_target = '10.10'
      s.tvos.deployment_target = '9.0'
      s.watchos.deployment_target = '2.0'
    
      # 框架的源文件
      s.source_files = 'Sources/*.swift'
    end
    

    注意:

    • s.description的字数一定要比s-summary长,否则会看到如下警告
    - WARN  | description: The description is shorter than the summary.
    

    TabPageView的spec

    Pod::Spec.new do |s|
      # 框架名称
      s.name = 'TabPageView'
      # 框架版本
      s.version = '0.0.1'
      # 框架协议
      s.license = 'MIT'
      # 框架简介
      s.summary = "The swift version of TabPageView in iOS"
      # 框架详细介绍
      s.description = "该框架实现了Tab标题和Page内容联动滚动的效果"
      # 框架的github主页
      s.homepage = 'https://github.com/brianbryant/TabPageView'
      # 框架开发者主页
      s.social_media_url = 'https://github.com/brianbryant'
      # 框架作者及作者的联系邮箱
      s.authors = { 'brianbryant' => 'brianbryant@126.com' }
      # 框架的下载地址和发布地址
      s.source = { :git => 'https://github.com/brianbryant/TabPageView.git', :tag => s.version }
    
      # 框架的源文件
      # **:匹配Sources下所有的目录
      # *.{h,m,swift}:Sources下所有子目录中的所有以.h,.m,.swift为后缀名的文件
      s.source_files  = "Sources", "Sources/**/*.{h,m,swift}"
      s.exclude_files = "Sources/Exclude"
    end
    

    (三)创建远程代码仓库

    在github上新建一个仓库TabPageView(https://github.com/brianbryant/TabPageView
    命令行进入本地TabPageView文件夹执行命令:git remote add origin master https://github.com/brianbryant/TabPageView.git
    添加本地框架源文件到本地git代码仓库暂缓区:git add .
    提交暂缓区的文件到本地master分支:git commit -m "初始化框架"
    上传到远程git代码仓库中:git push origin master
    创建发布版本:git tag 0.0.1
    上传全部发布版本:git push --tag
    上传指定发布版本:git push origin 0.0.1

    (四)上传spec

    验证.podspec文件有没有错误:pod lib lint

    执行命令pod trunk register brianbryant@126.com 'brianbryant' --verbose注册cocoapods账户
    执行命令pod trunk push TabPageView.podspec发布框架TabPageView的spec

    注意:

    • 因为该框架为运行在iOS上的swift语言版本的框架,我们需要指定swift语言版本。
      • 在.podspec同级目录下创建文件.swift-version。并打开.swift-version,输入内容
      echo "4.0" >> .swift-version
      
    • 如果不指定swift语言版本,会报如下警告
    - WARN  | swift: The validator used Swift 3.2 by default because no Swift version was specified. 
    To specify a Swift version during validation, add the `swift_version` attribute in your podspec. 
    Note that usage of the `--swift-version` parameter or a `.swift-version` file is now deprecated.
    
    • 项目必须包含一个证书。如果不包含会报如下警告
    - WARN  | [iOS] license: Unable to find a license file
    

    文中TabPageView框架源码点我


    如果有幸被你看到这里,我想先说一句:谢谢。接下来我会不断把自己空闲时间学习的笔记、工作中遇到的问题,思考总结后写成文章分享出来。下面是我的个人公众号,如果你觉得我写的东西给了你带来了一些启发,可以关注一下我的公众号bryanshen,接下来我会分享更多更优质的内容。谢谢!

    相关文章

      网友评论

          本文标题:Cocoapods发布属于自己的框架!

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