天天看點

iOS遠端私有庫的建立iOS遠端私有庫的建立

iOS遠端私有庫的建立

這幾天在研究遠端私有庫的使用,下面就記錄一下自己研究的過程,帶你一步步的建立自己的私有庫。

工具和技能

  • 第三方代碼倉庫,支援私有倉庫(必須是免費的,土豪略過)
  • CocoaPods的常用操作
  • git的常用操作

操作步驟

1、建立CocoaPods庫的模闆
pod lib create DemoLib
2、将核心代碼放入Classes目錄,如果有資源檔案放入Assets
3、測試代碼
4、git 關聯遠端私有庫
git remote add 私有庫位址
5、上傳代碼到git倉庫
 git add .
 git commit -m '更新資訊'
 git push  或者 git push origin master
 git tag 0.1.0
 git tag push --tags
6、建立一個私有庫索引并添加到本地
pod repo add localSpec 私有庫索引倉庫
pod repo 檢視是否添加成功
7、利用pod驗證代碼庫
#本地驗證
pod lib lint
#遠端驗證
pod spec lint
#遠端私有庫驗證
pod spec lint --private
#推送到本地搜尋庫
pod repo push localSpec MyLib.podspec
           

建立私有庫

1.首先在第三方倉庫中建立一個私有倉庫,我選用的是"碼雲",你當然也可以選擇其他的。

2.利用CocoaPods建立一個庫的模闆檔案

iOS遠端私有庫的建立iOS遠端私有庫的建立

3.将編寫的庫代碼拷貝到Classes檔案中,我這裡隻是随意拷貝了一份代碼

iOS遠端私有庫的建立iOS遠端私有庫的建立

4.進入Example檔案夾,在指令行利用pod install安裝自己的庫,測試是否正确,這一步略過

5.利用git将代碼上傳到私有倉庫中,并打相應的tag

iOS遠端私有庫的建立iOS遠端私有庫的建立
iOS遠端私有庫的建立iOS遠端私有庫的建立

建立私有庫的索引

1.同樣在第三方倉庫中創一個私有項目存儲私有庫的索引檔案

2.在本地添加私有庫索引檔案,利用pod repo 檢視

iOS遠端私有庫的建立iOS遠端私有庫的建立

3.編輯之前建立的Mylib庫的MyLib.podspec檔案

注意:s.version的值一定要和之前的tag對應,否則後面的驗證會不通過

iOS遠端私有庫的建立iOS遠端私有庫的建立

4.驗證MyLib.podspec檔案,由于是私有庫,注意檢視報錯資訊

iOS遠端私有庫的建立iOS遠端私有庫的建立

5.将MyLib.podspec檔案添加到本地索引檔案localSpec中,此時和自動同步到遠端私有索引庫中

iOS遠端私有庫的建立iOS遠端私有庫的建立

6.檢視是否添加成功

iOS遠端私有庫的建立iOS遠端私有庫的建立
iOS遠端私有庫的建立iOS遠端私有庫的建立

7.利用pod search 搜尋自己的私有庫

iOS遠端私有庫的建立iOS遠端私有庫的建立

8.檢視遠端私有庫的檔案

iOS遠端私有庫的建立iOS遠端私有庫的建立
iOS遠端私有庫的建立iOS遠端私有庫的建立

建立一個項目安裝自己的私有庫來驗證

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

source 'https://git.oschina.net/TianTianKaiFa/MySpecs.git'
#source 'https://github.com/CocoaPods/Specs.git'  有其他第三方庫,加上這句

target 'TestDemo' do
  use_frameworks!
  pod "MyLib"
end

           
iOS遠端私有庫的建立iOS遠端私有庫的建立
  • 自動化腳本

一鍵将修改的代碼上傳到git倉庫,并更新到私有庫索引庫

腳本Github位址

#使用方法 ./upload.sh 更新資訊 tag值 podspec檔案名

message=${1}
tag=${2}
podspecName=${3}

method="使用方法:./upload.sh 更新資訊 tag值 podspec檔案名"

if [ ${#podspecName} -eq 0 ]
then
    echo "參數不合法 - $method" & exit
fi

git add .

git commit -m "${message}"

git push origin master

git tag $tag 2> error.sh
error=$(cat ./error.sh)
if [ ${#error} -ne 0 ]  #不等于零說明有錯誤
then
    echo $error
    #删除本地tag
    git tag -d $tag
    #删除遠端tag
    git push origin :refs/tags/$tag
fi

git tag $tag
git push --tags

#本地驗證庫資訊  --verbose
pod lib lint --allow-warnings

#驗證遠端資訊
pod spec lint --private

#推送到私有庫
pod repo push localSpec MyLib.podspec
           
  • CocoaPods.podspec編輯備份
#
# Be sure to run `pod lib lint MyLib.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
  s.name             = 'MyLib'
  s.version          = '0.2.1'
  s.summary          = 'MyLib.'

# 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         = 'https://gitee.com/TianTianKaiFa/DemoLib'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { '[email protected]' => '[email protected]' }
  s.source           = { :git => 'https://gitee.com/TianTianKaiFa/DemoLib.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

    s.ios.deployment_target = '9.0'

    s.subspec 'Categorys' do |ss|
        ss.source_files = 'MyLib/Classes/Categorys/*'
    end
    
    s.subspec 'Components' do |ss|
        ss.source_files = 'MyLib/Classes/Components/**/*'
    end
    
    s.subspec 'Config' do |ss|
        ss.source_files = 'MyLib/Classes/Config/*'
    end

    #s.source_files = 'MyLib/Classes/**/*'
    #framework的寫法
	#s.source_files = 'MyLib/Classes/**/*.h'
	#s.vendored_frameworks= 'MyLib/Classes/Products/MyLib.framework'
	  # s.resource_bundles = {
	  #   'MyLib' => ['MyLib/Assets/*.png']
	  # }

  # s.public_header_files = 'Pod/Classes/**/*.h'
  # s.frameworks = 'UIKit', 'MapKit'
  # s.dependency 'AFNetworking', '~> 2.3'
end