iOS創(chuàng)建CocoaPods私有庫

前言

iOS開發(fā)者取试,會經(jīng)常使用CocoaPods管理第三方庫膳灶。但是,無論是組件化開發(fā)還是需要創(chuàng)建公司內(nèi)部的類庫球订,我們都可以制作自己私有的CocoaPods庫。本文就是詳細說明私有庫創(chuàng)建流程瑰钮。

1.準備工作

  • 創(chuàng)建兩個私有遠程倉庫 LWSpec 和 LWLib
    • WYSpec倉庫用來存儲本地spec
    • WYLib用來存儲項目工程文件

2.創(chuàng)建pod私有庫的項目工程

  • cd到合適目錄下用命令行創(chuàng)建工程

  • 執(zhí)行pod lib create WYLib

    MacBook-Pro ~ % cd desktop
    MacBook-Pro desktop % pod lib create LWLib
    
  • 回車之后終端會進行靈魂冒滩,按照自己的需求回答。

    #選擇平臺
    What platform do you want to use?? [ iOS / macOS ]
     > iOS
    #選擇編程語言
    What language do you want to use?? [ Swift / ObjC ]
     > Objc
    #在你的項目中是否創(chuàng)建一個demo工程浪谴,為了方便測試开睡,我選擇了Yes
    Would you like to include a demo application with your library? [ Yes / No ]
     > Yes
    #測試框架選擇哪一個
    Which testing frameworks will you use? [ Specta / Kiwi / None ]
     > None
    #要不要做視圖測試
    Would you like to do view based testing? [ Yes / No ]
     > Yes
    # 類前綴名
    What is your class prefix?
     > LW
    security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
    security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
    security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
    security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
    security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
    security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
    
    Running pod install on your new library.
    
    Analyzing dependencies
    Fetching podspec for `YOURPODNAME` from `../`
    Downloading dependencies
    Installing YOURPODNAME (0.1.0)
    Generating Pods project
    Integrating client project
    
    [!] Please close any current Xcode sessions and use `LWLib.xcworkspace` for this project from now on.
    Sending stats
    Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
    
    Ace! you're ready to go!
    We will start you off by opening your project in Xcode
    open 'LWLib/Example/LWLib.xcworkspace'
    
    To learn more about the template see `https://github.com/CocoaPods/pod-template.git`.
    To learn more about creating a new pod, see   `https://guides.cocoapods.org/making/making-a-cocoapod`.
    
  • 創(chuàng)建完成后自動打開

  • 然后進入LWLib文件夾中找到Classes中的ReplaceMe.m文件刪除,然后將自己所需要的文件放到這里目錄下

  • cdExample路徑下苟耻,執(zhí)行pod install更新Example中的pod

  • 打開Example中的.workspace文件篇恒,打開工程。

    找到. podspec

  • 修改. podspec

    Pod::Spec.new do |s|
    #項目名稱
    s.name             = 'LWLib'
    #版本號
    s.version          = '0.1.0'
    #項目摘要
    s.summary          = 'LWLib.'
    # 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
    LWLib的項目描述婚度,這是自己創(chuàng)建的一個項目
                         DESC
                         
    #主頁,這里要填寫可以訪問的地址官卡,不然驗證不通過
    #個人遠程倉庫的地址
    s.homepage         = 'https://gitee.com/ZhaoLaoWu/LWLib'
    # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
    s.license          = { :type => 'MIT', :file => 'LICENSE' }
    #作者信息
    s.author           = { '趙桂安' => '623916490@qq.com' }
    #項目地址蝗茁,這里不建議使用ssh地址,會有警告寻咒;建議使用HTTP或者HTTPS哮翘,最好使用HTTPS。
    #更改成自己的遠程倉庫地址
    s.source           = { :git => 'https://gitee.com/ZhaoLaoWu/LWLib.git', :tag => s.version.to_s }
    # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
    #最低支持的iOS版本
    s.ios.deployment_target = '8.0'
    #代碼源文件地址
    s.source_files = 'LWLib/Classes/**/*'
    
    # s.resource_bundles = {
    #   'LWLib' => ['LWLib/Assets/*.png']
    # }
    
    # s.public_header_files = 'Pod/Classes/**/*.h'
    
    #根據(jù)自己是否有依賴庫來決定是否添加
    #依賴的frameworks
    # s.frameworks = 'UIKit', 'MapKit'
    #依賴的公共庫或者私有庫
    # s.dependency 'AFNetworking', '~> 2.3'
    end
    
    
  • 修改完成后,cdLWLib文件

    執(zhí)行pod lib lint

    出一下表示成功

    MacBook-Pro LWLib % pod lib lint
    
     -> LWLib (0.1.0)
      #注意如果這有警告毛秘,最好把問題解決了饭寺,我這個是已經(jīng)解決的。
      - NOTE  | xcodebuild:  note: Using new build system
      - NOTE  | xcodebuild:  note: Building targets in parallel
      - NOTE  | [iOS] xcodebuild:  note: Planning build
      - NOTE  | [iOS] xcodebuild:  note: Constructing build description
      - NOTE  | [iOS] xcodebuild:  warning: Skipping code signing because the target does not have an Info.plist file and one is not being generated automatically. (in target 'App' from project 'App')
      - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'LWLib' from project 'Pods')
      - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'Pods-App' from project 'Pods')
      - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'App' from project 'App')
    
      LWLib passed validation.
      #已經(jīng)完成通過
    

3.將本地文件上傳到遠程倉庫

MacBook-Pro LWLib % git remote add origin https://gitee.com/ZhaoLaoWu/LWLib.git
MacBook-Pro LWLib % git add .
MacBook-Pro LWLib % git commit -m "Initial commit"
MacBook-Pro LWLib % git push -u origin master 
#To https://gitee.com/ZhaoLaoWu/LWLib.git
# ! [rejected]        master -> master (fetch first)
#error: failed to push some refs to 'https://gitee.com/ZhaoLaoWu/LWLib.git'
#hint: Updates were rejected because the remote contains work that you do
#hint: not have locally. This is usually caused by another repository pushing
#hint: to the same ref. You may want to first integrate the remote changes
#hint: (e.g., 'git pull ...') before pushing again.
#hint: See the 'Note about fast-forwards' in 'git push --help' for details.
#注意:當(dāng)執(zhí)行推送遠端失敗時叫挟,先執(zhí)行以下操作
MacBook-Pro LWLib % git pull origin master
#From https://gitee.com/ZhaoLaoWu/LWLib
# * branch            master     -> FETCH_HEAD
#fatal: refusing to merge unrelated histories
#注意:如果出現(xiàn)上面所示錯誤艰匙,需添加--allow-unrelated-histories即可
MacBook-Pro LWLib % git pull origin master --allow-unrelated-histories
#From https://gitee.com/ZhaoLaoWu/LWLib
# * branch            master     -> FETCH_HEAD
#CONFLICT (add/add): Merge conflict in README.md
#Auto-merging README.md
#Automatic merge failed; fix conflicts and then commit the result.
# 合并README.md的沖突 然后 commit
MacBook-Pro LWLib % git add .
MacBook-Pro LWLib % git commit -m '解決合并沖突' 
# 推送更新到遠程倉庫
#注意:第一次推送master分支時,加上了-u參數(shù)抹恳,Git不但會把本地的master分支內(nèi)容推送的遠程新的master分支员凝,還會把本地的master分支和遠程的master分支關(guān)聯(lián)起來,后續(xù)推送不需要再使用-u參數(shù)
MacBook-Pro LWLib % git push -u origin master
#Enumerating objects: 116, done.
#Counting objects: 100% (116/116), done.
#Delta compression using up to 4 threads
#Compressing objects: 100% (104/104), done.
#Writing objects: 100% (114/114), 53.17 KiB | 3.54 MiB/s, done.
#Total 114 (delta 25), reused 0 (delta 0)
#remote: Resolving deltas: 100% (25/25), completed with 1 local object.
#remote: Powered by GITEE.COM [GNK-5.0]
#To https://gitee.com/ZhaoLaoWu/LWLib.git
#   82f9eaf..b38a835  master -> master
#Branch 'master' set up to track remote branch 'master' from 'origin'.
#tag 值要和podspec中的version一致
MacBook-Pro LWLib % git tag 0.1.0
#推送tag到服務(wù)器上
MacBook-Pro LWLib % git push --tags
#Total 0 (delta 0), reused 0 (delta 0)
#remote: Powered by GITEE.COM [GNK-5.0]
#To https://gitee.com/ZhaoLaoWu/LWLib.git
# * [new tag]         0.1.0 -> 0.1.0

這是查看遠程倉庫已經(jīng)有代碼了

4.校驗Spec

  • 執(zhí)行pod spec lint

    zhaoguian@zhaoguiandeMacBook-Pro LWLib % pod spec lint
    
     -> LWLib (0.1.0)
      - NOTE  | xcodebuild:  note: Using new build system
      - NOTE  | xcodebuild:  note: Building targets in parallel
      - NOTE  | [iOS] xcodebuild:  note: Planning build
      - NOTE  | [iOS] xcodebuild:  note: Constructing build description
      - NOTE  | [iOS] xcodebuild:  warning: Skipping code signing because the target does not have an Info.plist file and one is not being generated automatically. (in target 'App' from project 'App')
      - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'LWLib' from project 'Pods')
      - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'Pods-App' from project 'Pods')
      - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'App' from project 'App')
    
      Analyzed 1 podspec.
    
      LWLib.podspec passed validation.
      
    

    出現(xiàn)上述描述表示成功奋献。

5.創(chuàng)建spec repo

  • 進入文件夾~/.cocoapods/repos可以查看本地spec repo

  • 指定管理 lib repospecs repourl
    pod repo add LWLib https://gitee.com/ZhaoLaoWu/LWSpec.git
    注意 此處地址是spec倉庫地址

  • 推送 podspecspecs repo
    pod repo push LWLib LWLib.podspec --sources=https://gitee.com/ZhaoLaoWu/LWSpec.git

    MacBook-Pro LWLib % pod repo push LWLib LWLib.podspec --sources=https://gitee.com/ZhaoLaoWu/LWSpec.git
    
    Validating spec
     -> LWLib (0.1.0)
        - NOTE  | xcodebuild:  note: Using new build system
        - NOTE  | xcodebuild:  note: Building targets in parallel
        - NOTE  | [iOS] xcodebuild:  note: Planning build
        - NOTE  | [iOS] xcodebuild:  note: Constructing build description
        - NOTE  | [iOS] xcodebuild:  warning: Skipping code signing because the target does not have an Info.plist file and one is not being generated automatically. (in target 'App' from project 'App')
        - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'LWLib' from project 'Pods')
        - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'Pods-App' from project 'Pods')
        - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'App' from project 'App')
    
    Updating the `LWLib' repo
    
    
    Adding the spec to the `LWLib' repo
    
    - [Add] LWLib (0.1.0)
    
    Pushing the `LWLib' repo
    

    出現(xiàn)上述表示已經(jīng)成功

6.驗證

  • 創(chuàng)建一個新項目健霹,在Podfile中創(chuàng)建并使用
source ‘https://gitee.com/ZhaoLaoWu/LWSpec.git’
platform:ios, '10.0'
target 'CSLWLib' do
 use_frameworks!
 pod 'LWLib'
end
  • cd項目中 執(zhí)行pod install
MacBook-Pro %  ~ % cd /Users/zhaoguian/Desktop/CSLWLib 
MacBook-Pro CSLWLib % pod install

Analyzing dependencies
Downloading dependencies
Installing LWLib (0.1.0)
Generating Pods project
Integrating client project
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

[!] Smart quotes were detected and ignored in your Podfile. To avoid issues in the future, you should not use TextEdit for editing it. If you are not using TextEdit, you should turn off smart quotes in your editor of choice.
zhaoguian@localhost CSLWLib % 

完成

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市瓶蚂,隨后出現(xiàn)的幾起案子糖埋,更是在濱河造成了極大的恐慌,老刑警劉巖窃这,帶你破解...
    沈念sama閱讀 211,348評論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件瞳别,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機祟敛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,122評論 2 385
  • 文/潘曉璐 我一進店門倍奢,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人垒棋,你說我怎么就攤上這事卒煞。” “怎么了叼架?”我有些...
    開封第一講書人閱讀 156,936評論 0 347
  • 文/不壞的土叔 我叫張陵畔裕,是天一觀的道長。 經(jīng)常有香客問我乖订,道長扮饶,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,427評論 1 283
  • 正文 為了忘掉前任乍构,我火速辦了婚禮甜无,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘哥遮。我一直安慰自己岂丘,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 65,467評論 6 385
  • 文/花漫 我一把揭開白布眠饮。 她就那樣靜靜地躺著奥帘,像睡著了一般。 火紅的嫁衣襯著肌膚如雪仪召。 梳的紋絲不亂的頭發(fā)上寨蹋,一...
    開封第一講書人閱讀 49,785評論 1 290
  • 那天,我揣著相機與錄音扔茅,去河邊找鬼已旧。 笑死,一個胖子當(dāng)著我的面吹牛召娜,可吹牛的內(nèi)容都是我干的运褪。 我是一名探鬼主播,決...
    沈念sama閱讀 38,931評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼萤晴,長吁一口氣:“原來是場噩夢啊……” “哼吐句!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起店读,我...
    開封第一講書人閱讀 37,696評論 0 266
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎攀芯,沒想到半個月后屯断,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,141評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,483評論 2 327
  • 正文 我和宋清朗相戀三年殖演,在試婚紗的時候發(fā)現(xiàn)自己被綠了氧秘。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,625評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡趴久,死狀恐怖丸相,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情彼棍,我是刑警寧澤灭忠,帶...
    沈念sama閱讀 34,291評論 4 329
  • 正文 年R本政府宣布,位于F島的核電站座硕,受9級特大地震影響弛作,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜华匾,卻給世界環(huán)境...
    茶點故事閱讀 39,892評論 3 312
  • 文/蒙蒙 一映琳、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧蜘拉,春花似錦萨西、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,741評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至您机,卻和暖如春穿肄,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背际看。 一陣腳步聲響...
    開封第一講書人閱讀 31,977評論 1 265
  • 我被黑心中介騙來泰國打工咸产, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人仲闽。 一個月前我還...
    沈念sama閱讀 46,324評論 2 360
  • 正文 我出身青樓脑溢,卻偏偏與公主長得像,于是被迫代替她去往敵國和親赖欣。 傳聞我的和親對象是個殘疾皇子屑彻,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,492評論 2 348