1. 程式人生 > >SDK Framework建立 CocoaPods 提交

SDK Framework建立 CocoaPods 提交

前言: 公司常需要提供原始碼給客戶,但是又有部分原始碼是不想給的,所以,打包成靜態庫是一個不錯的選擇,至於是.a還是.framework,看個人選擇,如果標頭檔案比較多的,我還是喜歡.framework,簡潔,標頭檔案收藏得深,本文章只是記錄一下開發流程以供以後翻看,不足之處,請見諒,斧正。

說明:建立Framework的方法一般有兩種,一個是xcode建立,一個是cocoaPods建立,因為建立後,需要將它釋出到Pods上給人使用的,公司有部分程式碼需要隱私,所以我選擇了用xcode建立了Framework後build出需要版本,再發布Pods上,但是如果程式碼不需要保密,可以直接用cocoaPods建立,這樣子會省很多事情,利用需要用到cocoaPods管理的第三方框架等

1、建立Framework,新增一個簡單的列印類

建立Framework 建立Framework 新增列印類 新增列印類

2、編輯、打包SDK,千萬切記要General的Deployment Target選擇支援的系統版本,儘量選擇到最舊的8.0,這個沒有選擇的話,後面會影響很大

編譯出真機和虛擬機器使用的.framework,這裡可以選擇debug或者Release 編譯framework 合併出一個相容虛擬機器和真機使用的.framework,特別說明一下,framework和a庫有點區別,合併的檔案是.framework裡邊的紅色框住的這個檔案,這個很重要 framework 合併命令:lipo -create 真機編輯檔案路徑 虛擬機器編譯檔案路徑 -output 合併後存放檔案的路徑

合併後的framework

至此,已經Framework就算完成了,正如上面所說,我們需要的是提供包給別人用的,所以接下來是如何將SDK放到Pods上面去

3、將Framework提交到github上

說明:因為Pods預設是基於git的程式碼管理,所以要將SDK丟到Pods上去,首先需要先放到git上。以下操作都是預設你已經有了github的賬號了的

  • 首先,在git上建立一個空的新專案,特別注意,下面的初始化儘量、最好勾選上,還有那個License,也選擇上,後面提交會用到 建立專案

  • 然後將該專案克隆到本地,我為了方便查詢,直接放到了桌面

git clone https://github.com/p****/C**
*****FrameworkTest.git

本地專案

  • 將上面弄好的SDK放到該專案中,並提交 這裡寫圖片描述
cd 到.git倉庫的同級目錄
git add .
git commit -m "註釋說明comment log"
git push
git tag 1.0.1
git push --tags
  • 成功後會在終端見到一下內容,github的工作就差不多了,現在到Pods了
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/phyky/CocoaPodsFrameworkTest.git
 * [new tag]         1.0.1 -> 1.0.1
  • 註冊trunk(郵箱和使用者名稱),通過會收到【Please verify the session by clicking the link in the verification email that has been sent to 26******[email protected]
pod trunk register 26******90@qq.com phyky --verbose
  • 去郵箱驗證,將圖中的紅框連線拷到瀏覽器開啟,完成驗證 驗證

  • 檢視註冊資訊

pod trunk me
- Name:     phyky
- Email:    26******90@qq.com
- Since:    September 10th, 02:02
- Pods:     None
- Sessions:
- September 10th, 02:02 - January 16th, 2019 02:05. IP:
  183.62.48.226
- September 10th, 02:29 - January 16th, 2019 02:29. IP:
  183.62.48.226
  • 建立.spec檔案,該檔案主要是配置一些專案到Pods的引數,極為重要。
pod trunk push ./PHYFrameworkTest.podspec

這裡寫圖片描述

  • 配置.spec檔案,部分重要的引數我標了一下注釋,但是裡邊的很多引數我沒有使用,因為這個SDK是很簡單的,而且沒有涉及到png和xib,具體引數使用,可另行查閱
#
#  Be sure to run `pod spec lint PHYFrameworkTest.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         = "PHYFrameworkTest"
  s.version      = "1.0.1" //切記要和Git提交的版本號一致
  s.summary      = "Test for PHYFrameworkTest."

  # 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
  只是測試一下,信不信由你(just for test,no content)
                   DESC//這裡的描述必須比上面的summary長,否則報警告

  s.homepage     = "https://github.com/phyky/CocoaPodsFrameworkTest.git"//填寫地址
  # 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      = "MIT (example)"
  //特別重要,這就是上面提到的新建Git專案的時候,初始化需要選擇的LICENSE,沒有這個提交會報錯
  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             = { "" => "" }
  # Or just: s.author    = ""
  # s.authors            = { "" => "" }
  # s.social_media_url   = "http://twitter.com/"

  # ――― 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
  //平臺、系統版本必填,否則會報osx相關的警告,也是提交不了,前面打包Framework的時候提到的重要性
   s.platform     = :ios, "8.0"

  #  When using multiple platforms
  # s.ios.deployment_target = "8.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/phyky/CocoaPodsFrameworkTest.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.
  #
//特別重要,這裡邊的.h或者.m檔案不能只是配到.framework,否則找不到,報錯
  s.source_files  = "lib/PHYFrameworkTest.framework/Headers/*.{h}"
  #s.exclude_files = "Classes/Exclude"

  # s.public_header_files = "Classes/**/*.h"
//特別重要,這裡必須配置,否則上傳成功,找不到.framework,只有標頭檔案
  s.vendored_frameworks = 'lib/PHYFrameworkTest.framework'

  # ――― 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.source_files
  # 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 = "UIKit", "Foundation"

  # 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
  • 校驗spec檔案是否正確,這步可能會丟擲很多警告或者錯誤,對照著去修改就行
pod spec lint
  • 檢驗通過,提示以下
 -> PHYFrameworkTest (1.0.1)
Analyzed 1 podspec.
PHYFrameworkTest.podspec passed validation.
  • 提交到Pods,成功會有以下的提示
pod trunk push ./PHYFrameworkTest.podspec
Congrats
PHYFrameworkTest (1.0.1) successfully published
September 10th, 03:23
https://cocoapods.org/pods/PHYFrameworkTest
Tell your friends!

進行到這裡,SDK就算是完整建立並提交完了,迫不及待去【pod search PHYFrameworkTest】一下,發現並沒有找到,更新一下pod,刪除掉pod本地的搜尋快取【~/Library/Caches/CocoaPods】,就會搜尋到了。以下是最後的成果

Framework測試