1. 程式人生 > >Pod 建立私有庫之引用三方Framework和libs的配置方法

Pod 建立私有庫之引用三方Framework和libs的配置方法

使用pod管理庫已經用了3年,當然也做過私有庫,但在做私有庫的時候有些東西並沒有弄得非常清楚,最近專案需要用到將自己的工程打成Framework並將這個Framework做成pod來供其他人去引用,鼓搗了大概幾個小時弄了一個例子,下面和大家分享一下過程和結果。這個Pod工程我以網易雲信的SDK作為匯入三方包為例,目的是為了演示如果做pod庫引用三方frameworks以及libraries。

1、首先我們需要把自己需要打出來的framework包匯出來,這個過程非常簡單而且網上很多例子我就不細說了,我也曾寫過一篇文章來說明這個過程,IOS打包動態庫以及使用的過程和注意事項 如果有需要可以點開學習一下

2、第二步在github上新新增一個庫這個相信大家都操作過我就不細說,然後將建立好的庫clone下來,然後我們可以開心的進行下一步了。

3、下一步就是在clone下來的工程目錄建立Pod工程,說是工程其實是做Pod引用相關,首先建立podspec檔案和將需要的三方frameworks和libraries拷貝到podspec檔案同級目錄(當然也可以放到其新建的工程裡其他目錄下但配置的時候需要找相對podspec檔案的相對路徑),建立方法如下

pod spec create 元件名稱
當執行完以上命令後在你當前目錄會出現副檔名為podspec的配置檔案,我使用的名稱為PrivatePod2018,所以生成的檔名稱是PrivatePod.podspec。 生成的PrivatePod.podspec中其實就是原始的配置選項,如下:
#
#  Be sure to run `pod spec lint PrivatePod2018.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         = "PrivatePod2018"
  s.version      = "0.0.1"
  s.summary      = "A short description of PrivatePod2018."

  # 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
                   DESC

  s.homepage     = "http://EXAMPLE/PrivatePod2018"
  # 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)"
  # s.license      = { :type => "MIT", :file => "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             = { "mingmingsuper" => "
[email protected]
" } # Or just: s.author = "mingmingsuper" # s.authors = { "mingmingsuper" => "[email protected]" } # s.social_media_url = "http://twitter.com/mingmingsuper" # ――― 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, "5.0" # 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 => "http://EXAMPLE/PrivatePod2018.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 = "Classes", "Classes/**/*.{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
生成這個檔案後你會發現檔案中都是預設配置,然後我們需要詳細配置這個檔案,主要需要配置的內容如下: - s.name 指這個庫的模組名稱 - s.version 庫的版本號,剛開始建立為0.0.1預設可以不用改 - s.summary 簡要描述,用於簡述這個庫的主要功能 - s.description 專案的詳細描述 - s.platform 這個是配置平臺 - s.homepage 專案主頁 - s.license 這個相信大家都知道我就不解釋了一般MIT就行 - s.author 這個是配置作者的資訊,可以使姓名加郵箱,也可以只姓名或只郵箱 - s.source 這個一定得正確配置,一般都是放到github上用github上專案的地址,我選用的地址是ssh方式如[email protected]:mingmingsuper/PrivatePod.git,你可能會問為啥你定義的名是PrivatePod2018為啥這裡是PrivatePod呢,那是因為PrivatePod已經被人佔用了
注意私有庫的模組名稱最後以podspec的name配置為準和專案建立的目錄名稱關係不大
  • s.source_file 這個是配置pod工程中的原始檔,如果是Objective-C寫的專案可以配置成類似"Classes//*.{h,m}"這樣的,如果是swift寫的可以配置成"Classes//*.swift",再如果是混編可以配置成"Classes//*.{h,m}","Classes//*.swift"這樣的,一次類推如果還有c++混編大家自己去試吧和以上說的寫法一樣,以此類推。
  • s.public_header_files 需要暴露出去的標頭檔案配置

針對s.frameworks和s.libraries以及s.vendored_libraries和s.vendored_frameworks這裡先不進行粗略說明因為這篇文章本意是重點介紹這幾個配置。


重要配置如下


由於我們做的這個私有pod需要引用外部的framework以及libraries所以我們需要配置vendored_libraries和vendored_frameworks
1)我們先簡單說明一下framewordks,這個配置選項是我們配置的依賴的系統庫,例如我們需要引用MobileCoreServices等系統庫那麼配置如下
s.frameworks = "MobileCoreServices", "SystemConfiguration", 'AVFoundation', 'CoreTelephony', 'CoreMedia', 'AudioToolbox', 'VideoToolbox'
2) s.libraries道理一樣我們是配置系統提供的一些庫,比如我們要引用libz.tbd等,我們需要如下配置寫法
s.libraries = "c++", "sqlite3.0", "z"
你可能要問為什麼libz.tbd寫成z,很簡單這是一個規則要去除lib和後邊的.tbd,如果遇到配置系統library就需要這麼簡寫,記住就行了。
以上是配置系統庫引用的寫法不做過多解釋也沒有太多注意的地方,很好理解。
3) s.vendored_libraries 由於網易雲信的庫引用了很多的.a檔案所以我們需要通過這個配置將這些檔案引用到我們pod工程中,寫法有兩種第一種是寫成以逗號分隔的形式例如:'libaacplus.a','libcrypto.a',... 還有一種是簡寫為通用形式以 .a匹配所有.a檔案,所以我們採用.a這種方式,這樣不容易拼錯也節省時間。 4)s.vendored_frameworks和s.vendored_libraries類似寫法也很類似,我們同樣採用簡寫形式
注意以上3和4是配置引用三方庫的寫法,這兩點可以著重看一下。需要注意的是我把所有的framework和library都放到了專案的根目錄所以沒有加目錄直接寫檔名稱,如果您將檔案放到了固定其他目錄必須制定完整路徑才可以用,路徑指相對於podspec檔案的路徑

4、以上就介紹完podspec檔案裡的配置了,下面我們來說庫的提交到最後提交到cocoapods上。 - 第一步先提交到github上,然後打tag
git add .
git commit -m "add files"
git push
git tag -a 0.0.1 -m "new version"
git push --tags

  • 第二部需要把工程推到cocoapods的trunk上,首先需要註冊
pod trunk register 'your email address' 'your name'
接下來就可以把工程推到trunk上了
pod trunk push PrivatePod.podspec
執行這個命令需要一點時間耐性等著就行,如果成功會有像以下的輸出
[!] Found podspec `PrivatePod2018.podspec`
Updating spec repo `master`

CocoaPods 1.4.0 is available.
To update use: `sudo gem install cocoapods`

For more information, see https://blog.cocoapods.org and the CHANGELOG for this version at https://github.com/CocoaPods/CocoaPods/releases/tag/1.4.0

Validating podspec
 -> PrivatePod2018 (0.0.3)

Updating spec repo `master`

CocoaPods 1.4.0 is available.
To update use: `sudo gem install cocoapods`

For more information, see https://blog.cocoapods.org and the CHANGELOG for this version at https://github.com/CocoaPods/CocoaPods/releases/tag/1.4.0


--------------------------------------------------------------------------------