一、背景
在iOS開發(fā)中旧巾,我們經(jīng)常會(huì)使用到一些第三方庫(kù),如AFNetworking忍些、SDWebImage等鲁猩,一般使用cocoapods來管理。本篇文章將介紹如何封裝自己的pod庫(kù)罢坝。
二廓握、封裝步驟
1. 在本地創(chuàng)建pod庫(kù)
利用pod命令創(chuàng)建了名為JCNetworking的pod庫(kù)
jiangchongdeMacBook-Pro:Desktop yellow$ pod lib create JCNetworking
執(zhí)行完上述命令之后,它會(huì)問你幾個(gè)問題嘁酿,按需求填寫即可
What platform do you want to use?? [ iOS / macOS ]
> iOS
What language do you want to use?? [ Swift / ObjC ]
> ObjC
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?
> JC
到這里pod庫(kù)就創(chuàng)建完成了隙券,它會(huì)自己打開剛才創(chuàng)建的pod庫(kù)。
2.添加庫(kù)文件
我們創(chuàng)建一個(gè)簡(jiǎn)單的類闹司,聲明一個(gè)打印方法娱仔,以便后面檢測(cè)是否可用。
#import "JCNetworkManager.h"
@implementation JCNetworkManager
- (void)testPrint:(NSString *)text {
NSLog(@"JCNetworkManager print == %@",text);
}
@end
將庫(kù)文件放入Classes文件夾里游桩,并刪除ReplaceMe.m在Example路徑下執(zhí)行pod install命令牲迫,看看是否能將剛剛添加的庫(kù)文件引入到工程中
jiangchongdeMacBook-Pro:Desktop yellow$ cd JCNetworking/Example/
jiangchongdeMacBook-Pro:Example yellow$ pod install
pod install執(zhí)行完成后可以在工程中看到添加的文件已經(jīng)被成功引入3. 修改JCNetworking.podspec
首先在github創(chuàng)建一個(gè)新的repository修改JCNetworking.podspec文件
Pod::Spec.new do |s|
s.name = 'JCNetworking'
# 默認(rèn)版本號(hào)
s.version = '0.1.0'
s.summary = 'A short description of JCNetworking.'
# 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
# 主頁(yè)
s.homepage = 'https://github.com/huqigu/JCNetworking'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'huqigu' => 'huqigu@163.com' }
# source路徑
s.source = { :git => 'https://github.com/huqigu/JCNetworking.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '8.0'
s.source_files = 'JCNetworking/Classes/**/*'
# s.resource_bundles = {
# 'JCNetworking' => ['JCNetworking/Assets/*.png']
# }
# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
#嘗試引入第三方依賴庫(kù)
s.dependency 'AFNetworking', '~> 2.3'
end
4. 項(xiàng)目發(fā)布
cd到項(xiàng)目路徑下執(zhí)行g(shù)it命令耐朴,將項(xiàng)目發(fā)布到上一步創(chuàng)建的github里。
yellow@jiangchongdeMacBook-Pro ~ cd /Users/yellow/Desktop/JCNetworking
// 添加github項(xiàng)目路徑
yellow@jiangchongdeMacBook-Pro ~/Desktop/JCNetworking master ● git remote add origin https://github.com/huqigu/JCNetworking.git
// 添加文件
yellow@jiangchongdeMacBook-Pro ~/Desktop/JCNetworking master ● git add .
yellow@jiangchongdeMacBook-Pro ~/Desktop/JCNetworking master ? git commit -m "first commit"
# --allow-unrelated-histories
# git pull origin maste會(huì)失敗 ,提示:fatal: refusing to merge unrelated histories
# 原因是遠(yuǎn)程倉(cāng)庫(kù)origin上的分支master和本地分支master被Git認(rèn)為是不同的倉(cāng)庫(kù)盹憎,所以不能直接合并筛峭,需要添加 --allow-unrelated-histories
yellow@jiangchongdeMacBook-Pro ~/Desktop/JCNetworking master git pull origin master --allow-unrelated-histories
// 推送到master分支
? yellow@jiangchongdeMacBook-Pro ~/Desktop/JCNetworking master git push origin master
// 提交版本號(hào)并push
#注意這里的版本號(hào)要與.podspec中的版本號(hào)保持一致
yellow@jiangchongdeMacBook-Pro ~/Desktop/JCNetworking master git tag 0.1.0
yellow@jiangchongdeMacBook-Pro ~/Desktop/JCNetworking master git push origin 0.1.0
執(zhí)行完上述命令之后可以去github上查看是否已經(jīng)推送上去了。
5. 嘗試使用
我們新建一個(gè)項(xiàng)目并引用我們的pod庫(kù) 看看是否能成功pod下了脚乡。
項(xiàng)目的podfile如下:
target 'TestSDK' do
# Uncomment the next line if you're using Swift or would like to use dynamic frameworks
# use_frameworks!
'jc',:inhibit_warnings => true
pod 'JCNetworking',:git =>"https://github.com/huqigu/JCNetworking.git"
# Pods for TestSDK
target 'TestSDKTests' do
inherit! :search_paths
# Pods for testing
end
target 'TestSDKUITests' do
inherit! :search_paths
# Pods for testing
end
end
執(zhí)行pod命令:
yellow@jiangchongdeMacBook-Pro ~/Desktop/testSDK/TestSDK pod install
Analyzing dependencies
Pre-downloading: `JCNetworking` from `https://github.com/huqigu/JCNetworking.git`, commit `1fbdb285f6b6702b4ba35747951625cf29f4a380`
Downloading dependencies
Installing JCNetworking (0.0.1)
Generating Pods project
Integrating client project
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
在項(xiàng)目里使用pod庫(kù)中的方法6. 總結(jié)
我們還可以在.podspec文件中指定其他的依賴蜒滩,比如我們想基于AFNetworking,封裝一套屬于自己的網(wǎng)絡(luò)請(qǐng)求庫(kù)奶稠,那么我們就可以一并將AFNetworking也pod下了俯艰,只需要增加一行代碼即可
#嘗試引入第三方依賴庫(kù)
s.dependency 'AFNetworking', '~> 2.3'
我們的私有庫(kù)創(chuàng)建就介紹到這里,多強(qiáng)大的功能還需要自己去挖掘锌订。
更新
將podspec推送到cocoapods倉(cāng)庫(kù)
執(zhí)行如下命令
// 郵箱為github綁定的郵箱竹握,會(huì)發(fā)送一封帶有鏈接的郵件,打開鏈接即完成注冊(cè)
yellow@jiangchongdeMacBook-Pro ~ pod trunk register huqigu@163.com 'mxObject-c' --description='regist trunk'
// 然后將倉(cāng)庫(kù)推送到cocoapods上
yellow@jiangchongdeMacBook-Pro ~/Desktop/JCNetworking master ● pod trunk push JCNetworking.podspec --allow-warnings
// 出現(xiàn)如下信息表示上傳成功
?? Congrats
?? JCNetworking (0.1.0) successfully published
?? February 27th, 21:23
?? https://cocoapods.org/pods/JCNetworking
?? Tell your friends!
上傳成功之后可以直接使用 pod 'JCNetworking'進(jìn)行引用辆飘,而不需要添加git鏈接啦辐。