iOS创建自己的pod库

一、背景

在iOS开发中,我们经常会使用到一些第三方库,如AFNetworking、SDWebImage等,一般使用cocoapods来管理。本篇文章将介绍如何封装自己的pod库。

二、封装步骤

1. 在本地创建pod库

利用pod命令创建了名为JCNetworking的pod库

jiangchongdeMacBook-Pro:Desktop yellow$ pod lib create JCNetworking

执行完上述命令之后,它会问你几个问题,按需求填写即可

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库就创建完成了,它会自己打开刚才创建的pod库。

目录结构如下图

2.添加库文件

我们创建一个简单的类,声明一个打印方法,以便后面检测是否可用。

#import "JCNetworkManager.h"

@implementation JCNetworkManager

- (void)testPrint:(NSString *)text {
    
    NSLog(@"JCNetworkManager print == %@",text);
    
}

@end

 

 

将库文件放入Classes文件夹里,并删除ReplaceMe.m

在Example路径下执行pod install命令,看看是否能将刚刚添加的库文件引入到工程中

jiangchongdeMacBook-Pro:Desktop yellow$ cd JCNetworking/Example/
jiangchongdeMacBook-Pro:Example yellow$ pod install

pod install执行完成后可以在工程中看到添加的文件已经被成功引入

3. 修改JCNetworking.podspec

 

 

首先在github创建一个新的repository

修改JCNetworking.podspec文件

Pod::Spec.new do |s|
  s.name             = 'JCNetworking'
# 默认版本号
  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
# 主页
  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'
#尝试引入第三方依赖库
   s.dependency 'AFNetworking', '~> 2.3'
end

4. 项目发布

cd到项目路径下执行git命令,将项目发布到上一步创建的github里。

yellow@jiangchongdeMacBook-Pro  ~  cd /Users/yellow/Desktop/JCNetworking
// 添加github项目路径
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会失败 ,提示:fatal: refusing to merge unrelated histories
# 原因是远程仓库origin上的分支master和本地分支master被Git认为是不同的仓库,所以不能直接合并,需要添加 --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
// 提交版本号并push
#注意这里的版本号要与.podspec中的版本号保持一致
 yellow@jiangchongdeMacBook-Pro  ~/Desktop/JCNetworking   master  git tag 0.1.0
yellow@jiangchongdeMacBook-Pro  ~/Desktop/JCNetworking   master  git push origin 0.1.0

执行完上述命令之后可以去github上查看是否已经推送上去了。

5. 尝试使用

我们新建一个项目并引用我们的pod库 看看是否能成功pod下了。

项目的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

执行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.

 

 

在项目里使用pod库中的方法

6. 总结

我们还可以在.podspec文件中指定其他的依赖,比如我们想基于AFNetworking,封装一套属于自己的网络请求库,那么我们就可以一并将AFNetworking也pod下了,只需要增加一行代码即可

#尝试引入第三方依赖库
s.dependency 'AFNetworking', '~> 2.3'

我们的私有库创建就介绍到这里,多强大的功能还需要自己去挖掘。

更新

将podspec推送到cocoapods仓库

执行如下命令

// 邮箱为github绑定的邮箱,会发送一封带有链接的邮件,打开链接即完成注册
yellow@jiangchongdeMacBook-Pro  ~  pod trunk register huqigu@163.com 
// 然后将仓库推送到cocoapods上
yellow@jiangchongdeMacBook-Pro  ~/Desktop/JCNetworking   master ●  pod trunk push JCNetworking.podspec --allow-warnings
// 出现如下信息表示上传成功
🎉  Congrats

 🚀  JCNetworking (0.1.0) successfully published
 📅  February 27th, 21:23
 🌎  https://cocoapods.org/pods/JCNetworking
 👍  Tell your friends!

上传成功之后可以直接使用 pod 'JCNetworking'进行引用,而不需要添加git链接。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值