手把手教你創建Cocoapods的podspec

現在大部分iOS項目都是在用Cocoapods來管理項目了,但是當我們自己寫的一個公有或者私有項目的時候,想要支持Cocoapods,那應該怎么做呢?本教程手把手的教大家如何創建podsepc

步驟

  1. github上創建自己的共有項目


    2220EA18-9E12-4891-B409-EB99776CBA78.png
  2. 創建自己的Xcode工程,并提交到該repository


    0EF5DF0F-67C1-41D4-B3BA-5F8A8BB0BE78.png
  3. 創建podspec文件
    我們cd當我們項目的根目錄下。本項目中就是上圖的文件夾下。執行如下命令:

    pod spec create OceanFish git@github.com:FlyOceanFish/CreateOwnSpec.git
    
  4. 編輯我們的podspec文件

#
#  Be sure to run `pod spec lint OceanFish.podspec' to ensure this is a
#  valid spec and to remove all comments including this before submitting the spec.
#
#  To learn more about Podspec attributes see http://docs.cocoapods.org/specification.html
#  To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/
#

Pod::Spec.new do |s|

  # ―――  Spec Metadata  ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  These will help people to find your library, and whilst it
  #  can feel like a chore to fill in it's definitely to your advantage. The
  #  summary should be tweet-length, and the description more in depth.
  #

  s.name         = "OceanFish"
  s.version      = "0.0.1"
  s.summary      = "make a Spec"

  # This description is used to generate tags and improve search results.
  #   * Think: What does it do? Why did you write it? What is the focus?
  #   * Try to keep it short, snappy and to the point.
  #   * Write the description between the DESC delimiters below.
  #   * Finally, don't worry about the indent, CocoaPods strips it!
  s.description  = <<-DESC
                  手把手叫你如何創建自己的Spec
                   DESC

  s.homepage     = "https://github.com/FlyOceanFish"
  # s.screenshots  = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif"


  # ―――  Spec License  ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Licensing your code is important. See http://choosealicense.com for more info.
  #  CocoaPods will detect a license file if there is a named LICENSE*
  #  Popular ones are 'MIT', 'BSD' and 'Apache License, Version 2.0'.
  #

  s.license      = { :type => "MIT", :file => "LICENSE" }


  # ――― Author Metadata  ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Specify the authors of the library, with email addresses. Email addresses
  #  of the authors are extracted from the SCM log. E.g. $ git log. CocoaPods also
  #  accepts just a name if you'd rather not provide an email address.
  #
  #  Specify a social_media_url where others can refer to, for example a twitter
  #  profile URL.
  #

  s.author             = { "OceanFish" => "978456068@qq.com" }
  # Or just: s.author    = "frances"
  # s.authors            = { "frances" => "978456068@qq.com" }
  # s.social_media_url   = "http://twitter.com/frances"

  # ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  If this Pod runs only on iOS or OS X, then specify the platform and
  #  the deployment target. You can optionally include the target after the platform.
  #

  # s.platform     = :ios
   s.platform     = :ios, "10.3"

  #  When using multiple platforms
  # s.ios.deployment_target = "5.0"
  # s.osx.deployment_target = "10.7"
  # s.watchos.deployment_target = "2.0"
  # s.tvos.deployment_target = "9.0"


  # ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Specify the location from where the source should be retrieved.
  #  Supports git, hg, bzr, svn and HTTP.
  #

  s.source       = { :git => "https://github.com/FlyOceanFish/CreateOwnSpec.git", :tag => "#{s.version}" }


  # ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  CocoaPods is smart about how it includes source code. For source files
  #  giving a folder will include any swift, h, m, mm, c & cpp files.
  #  For header files it will include any header in the folder.
  #  Not including the public_header_files will make all headers public.
  #

  s.source_files  = "FlyOceanFish", "FlyOceanFish/**/*.{h,m}"
  #s.exclude_files = "Classes/Exclude"

  # s.public_header_files = "Classes/**/*.h"


  # ――― Resources ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  A list of resources included with the Pod. These are copied into the
  #  target bundle with a build phase script. Anything else will be cleaned.
  #  You can preserve files from being cleaned, please don't preserve
  #  non-essential files like tests, examples and documentation.
  #

  # s.resource  = "icon.png"
  # s.resources = "Resources/*.png"

  # s.preserve_paths = "FilesToSave", "MoreFilesToSave"


  # ――― Project Linking ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Link your library with frameworks, or libraries. Libraries do not include
  #  the lib prefix of their name.
  #

  # s.framework  = "SomeFramework"
  # s.frameworks = "SomeFramework", "AnotherFramework"

  # s.library   = "iconv"
  # s.libraries = "iconv", "xml2"


  # ――― Project Settings ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  If your library depends on compiler flags you can set them in the xcconfig hash
  #  where they will only apply to your library. If you depend on other Podspecs
  #  you can include multiple dependencies to ensure it works.

  s.requires_arc = true

  # s.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }
  # s.dependency "JSONKit", "~> 1.4"

end

  1. 執行如下命令進行校驗文件的正確性

    pod lib lint
    
AECADD88-FAD7-4B9C-BD73-701B7AC40282.png

如果見到如上圖,則代表驗證通過;如果沒有通過的話按照提示進行修改即可。
備注:這個命令是本地校驗的,還要pod spec lint 這個是本地和遠程校驗。如果此時我們用pod spec lint會報錯,因為我們還有發布一個版本,就是第6步還沒做呢。

  1. 在github上創建一個發布版本


    3EF1084F-7DF7-403E-8CE7-519AB42194E0.png

    此時我們運行pod spec lint則會見到如下圖


    EDBCCFB9-6125-4816-8CEE-DD7178C06CB0.png

    到這步整個制作過程已經完成接下來讓我們發布自己spec

發布到CocoaPods

CocoaPods 0.33中加入了 Trunk 服務,使用 Trunk 服務可以方便的發布自己的Pod。要想使用 Trunk 服務,首先需要使用如下命令注冊自己的電腦。這很簡單,只要你指明你的郵箱地址(spec文件中的)和名稱即可。CocoaPods 會給你填寫的郵箱發送驗證郵件,點擊郵件中的鏈接就可通過驗證。

  1. 注冊
    pod trunk register 978456068@qq.com "FlyOceanFish"

2、驗證是否注冊成功

pod trunk me
7117B254-7688-427F-A205-69968ABF3AE3.png

3、發布我們的podspec

pod trunk push
7FA508FC-B6CB-4ED1-B31E-1799F83BCE6F.png

此圖代表我們已經發布成功

不過此時我們pod search還搜不到我們的podspec。

可以通過一下兩步之后再嘗試搜索:

  • 運行pod setup更新本地的spec,再搜索一下試試看。
  • 刪除~/Library/Caches/CocoaPods目錄下的search_index.json文件

pod setup成功后會生成~/Library/Caches/CocoaPods/search_index.json文件。
終端輸入rm ~/Library/Caches/CocoaPods/search_index.json
刪除成功后再執行pod search

參考鏈接

CocoaPods
between 'pod spec lint' and 'pod lib lint'?

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容