iOS Cocoapods創建自己的Cocoapods公共庫組件

iOS Cocoapods創建自己的Cocoapods公共庫組件

註冊CocoaPods賬戶信息

打開終端,創建一個開源pod庫,需要註冊CocoaPods

pod trunk register 郵箱地址 '用戶名' --verbose

在這裏插入圖片描述

創建pod庫如下步驟

Demo:SayHi爲例

  1. 打開終端:cd File
cd /Users/bruceyao/Desktop/pod_public
  1. 命令行: pod lib create 項目名稱 回車
pod lib create SayHi

會有如下信息:

What platform do you want to use?? [ iOS / macOS ]
 > ios  //那個平臺

What language do you want to use?? [ Swift / ObjC ]
 > Swift //語言

Would you like to include a demo application with your library? [ Yes / No ]
 > yes //是否需要生成一個 demo 應用,這裏最好 Yes 這樣你可以查看你創建的庫是否可以使用

Which testing frameworks will you use? [ Quick / None ]
 > quick //需要用到的測試框架,隨意選

Would you like to do view based testing? [ Yes / No ]
 > yes //基於視圖的測試 Yes/No 都可以

在這裏插入圖片描述

  1. 配置信息,在 demo 工程中有一個 Podspec Metadata 文件夾,查看 SayHi. podspec 配置裏面的信息

注意:

s.swift_version = '5.0'// 指定版本Swift,不然會有警告
s.frameworks = 'UIKit' //項目中用到的系統框架
s.source_files = 'SayHi/Classes/**/*' //將來你要放自己代碼的地方,路徑一定要配置正確
s.version          = '0.1.0' //版本,
#
# Be sure to run `pod lib lint SayHi.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             = 'SayHi'//工程名
  s.version          = '0.1.0' //版本
  s.summary          = 'just SayHi.'//描述你的庫
  s.dependency 'AFNetworking', '~> 2.3' //第三方依賴
  

# 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/xxxx/SayHi' //切記不要帶.git
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'authorName' => '[email protected]' }
  s.source           = { :git => 'https://github.com/xxxx/SayHi.git', :tag => s.version.to_s } //資源完整路徑Git
  s.swift_version = '5.0'// 指定版本Swift,不然會有警告
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '8.0'

  s.source_files = 'SayHi/Classes/**/*' //將來你要放自己代碼的地方
  
  # s.resource_bundles = {
  #   'SayHi' => ['SayHi/Assets/*.png']
  # }

  # s.public_header_files = 'Pod/Classes/**/*.h'
  # s.frameworks = 'UIKit', 'MapKit' 
  # s.dependency 'AFNetworking', '~> 2.3'
end
  1. github上創建對應工程的項目

點擊:Create repository即可
注意:不需要README.md文件啦,已經有啦
在這裏插入圖片描述

  1. 把我們的創建的SayHi代碼放到Classes路徑下

注意:通過Show in Finder 進入文件夾
所在文件目錄:…/SayHi/SayHi/Classes/
把ReplaceMe.swift文件刪除,不要通過Xcode直接拉進來
在這裏插入圖片描述
在這裏插入圖片描述
YSayHi.swift代碼

import UIKit
open class YSayHi {
    
    public static func sayHello() {
        print("Hello world")
    }
}
  1. 打開終端,執行如下,切記在SayHi根目錄下
git add .
git commit -am 'SayHi'
//第一次提交用:
git remote add origin https://github.com/xxxx/SayHi.git
git push -u origin master

//打標籤
git tag '0.1.0'
git push origin --tags
git tag //查看
  1. 用命令行進入 Example目錄下,執行
pod install

在這裏插入圖片描述
可以看到我們工程文件:YSayHi添加到啦pod上啦

  1. 退出到上個目錄下,驗證我們的庫是否正確,執行如下命令:
cd ..
pod lib lint --allow-warnings //忽略警告

看到有兩個警告,上面第三部有解決方案分別是描述和swift版本問題:

- WARN  | summary: The summary is not meaningful.
    - WARN  | [iOS] swift: The validator used Swift `4.0` by default because no Swift version was specified. To specify a Swift version during validation, add the `swift_versions` attribute in your podspec. Note that usage of a `.swift-version` file is now deprecated.

在這裏插入圖片描述

  1. 提交我們的代碼到pod公共庫上執行如下:
    這個命令有可能要等一段時間,也有可能會很快執行完畢
pod trunk push SayHi.podspec --allow-warnings

在這裏插入圖片描述

  1. 自己新建一個工程Demo,用pod管理工程
source 'https://github.com/CocoaPods/Specs.git'

platform :ios, "9.0"
use_frameworks!
def public_pods
    pod 'YYExtentions'
    pod 'SayHi'
end
target 'Demo12' do
 xcodeproj 'Demo12.xcodeproj'
    public_pods
end

在這裏插入圖片描述

在ViewController調用結果如下

import SayHi
	override func viewDidLoad() {
        super.viewDidLoad()
        YSayHi.sayHello()   
    }

打印結果如下:Hello world

在這裏插入圖片描述
11. 大功告成!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章