CCRogerWang / blog

A blog about iOS, Python,Machine Learning
1 stars 1 forks source link

How to create a private pods #4

Open CCRogerWang opened 7 years ago

CCRogerWang commented 7 years ago

事前準備

請安裝CocoaPods, 並且更新到最新版本。

sudo gem install cocoapods

開始 - Create Development pod

使用 pod lib create [Library name]

pod lib create MyLib

輸入完後稍等一下會出現幾個問題需要填寫,如圖示。 1

等pod跑完後會自動開啟Xcode。 來看一下目錄結構: 2

來做一個Lib

首先新增一個Swift檔案在classes的資料夾,命名為foo.swfit。
這是一個class包含一個class func fooPrint。

注意:swift的檔案需要被宣告為public。  


public class foo{
public class func fooPrint() {
    print("foo")
}

}


修改完後請使用pod install or pod update去更新這個Lib,這樣xcode才會認識剛剛新增的foo。  

接下來到Example for MyLib的ViewController.swift,修改code
```swift
import MyLib
override func viewDidLoad() {
    super.viewDidLoad()
    foo.fooPrint()
}

Done!現在跑一下程式,出現foo。

如何更新  

來新增一個function,打開foo.swift,加上以下的程式碼。

    public class func fooShow() {
        print("fooShow")
    }

再回到ViewController.swift,修改一下。

    override func viewDidLoad() {
        super.viewDidLoad()
        foo.fooPrint()
        foo.fooShow() // 使用剛剛新增的function   
    }

看一下Xcode,會報錯是正常的。  

注意:每次新增檔案到你的Pods,必須要做一次pod install or pod update。

做完pod install後,build & run,會看到
foo
fooShow
就成功了,到目前為止是Create Development Pods的教學。  

把MyLib丟到Github    

略~

在Github產生一個新的spec (存放podspecs)

在github產生一個demo-roger-specs

mkdir demo-roger-specs
cd demo-roger-specs
echo "# demo-roger-specs" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:demo-roger-specs.git
git push -u origin master

上傳到github,這個資料夾就可以砍了

在CocoaPods新增自己的spec

pod repo add demo-roger-specs git@github.com:demo-roger-specs.git

CocoaPodsp會把這個spec複製一份到 "~/.cocoapods/repos" 裡面

編輯podspec

回到MyLib.podspec

Pod::Spec.new do |s|
  s.name             = 'MyLib'
  s.version          = '0.1.0'
  s.summary          = 'A short description of 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
TODO: Add long description of the pod here.
                       DESC

  s.homepage         = 'https://github.com/roger77622@gmail.com/MyLib'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'roger77622@gmail.com' }
  s.source           = { :git => 'https://github.com/roger77622@gmail.com/MyLib.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '8.0'

  s.source_files = 'MyLib/Classes/**/*'

  # s.resource_bundles = {
  #   'MyLib' => ['MyLib/Assets/*.png']
  # }

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

s.summary,s.description盡量寫,description一定要比summary長。
必要修改的欄位:
s.homepage = 'https://github.com/demo-MyLib'
s.source    = 'git@github.com:demo-MyLib.git'
因為不開源
s.license   = 'MIT' -> 'proprietary'
另外LICENSE檔案也要改掉,輸入

echo -e "Copyright (c) 2017, MyCompany\nAll right reserved." > LICENSE      

接下來驗證podspec是否正確

pod lib lint --allow-warnings

Cocoapods會因為一些無關痛癢的warnings造成驗證失敗,因為是要自己用,所以加入 --allow-warnings 略過。
3

將MyLib連結到私有庫

測試通過後,需要先在MyLib打上tag。
再把MyLib連結到剛剛建立的私有庫。

pod repo push --allow-warnings demo-roger-specs MyLib.podspec

4

安裝Private Pod

指定private spec的位置,就是demo-roger-specs的位置,再把private pod加到專案的Podfile

source 'git@github.com:demo-roger-specs.git'        #私有仓库地址

target 'TestPriavetPod' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for TestPriavetPod
  pod 'MyLib'
end

更新Private Pod

  1. 修改程式
  2. 修改 .podspec的tag
  3. push 程式
  4. github 打上tag,要和第二步的tag一樣
  5. run pod repo push --allow-warnings demo-roger-specs MyLib.podspec

依照上述的步驟,新增一個function:fooUpdate(),並且把ReplaceMe.swift刪掉。    

測試

開啟terminal run pod update。
5

打開專案,可以看到裡面的ReplaceMe.swift已經不見,而且多了一個fooUpdate function。    

刪除private spec

pod repo remove demo-roger-specs
CCRogerWang commented 6 years ago

pod lib lint --allow-warnings出現的問題:

[iOS] unknown: Encountered an unknown error (757: unexpected token at '2018-04-26 12:47:33.830 simctl[92572:914929] CoreSimulator detected Xcode.app relocation or CoreSimulatorService version change. Framework path (/Applications/Xcode/Xcode_8.3.3.app/Contents/Developer/Library/PrivateFrameworks/CoreSimulator.framework) and version (375.21) does not match existing job path (/Library/Developer/PrivateFrameworks/CoreSimulator.framework/Versions/A/XPCServices/com.apple.CoreSimulator.CoreSimulatorService.xpc) and version (494.33). Attempting to remove the stale service in order to add the expected version. ') during validation.

解法:

可能是因為有多個xcode在電腦裡,使用:

xcode-select -s [Xcode的完整路徑]

就解決了。