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链接。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
netronlight .net开源流程图类,类似visio,这个较轻量级版本多出不少东西。支持Group,支持Undo。但有些东西实现的较为啰嗦。不过不失为一个学习和再开发的项目。 Netron Light v2.5 pre-release The final release will be v3.0 around the summer 2006. You can however rely on the current interfaces and base classes, the core is stable. Many things are still missing in this pre-release but I have also stripped many features and things like documentation, unit tests, code comments, ASP.Net features and so on. While all the code is yours you'll have to register (it'll be around $50,-) for the documentation and all the fancy stuff. I hope to complete the 'ultimate guide to diagramming in C#' by the summer which will be accessible to registered users. Support is still kinda of a problem since I have little time. I hope the Netron forum will continue to deliver support and inspire (thanks to all who keep an eye on the forum). I have considered sort of billable consultancy and delivering custom versions of the diagram control(s) but I fear I don't have enough time and quiting my job to work full-time on Netron is an unreachable dream. Thanks to all of you who have donated to Netron and support this project by Email, chat or via the forum. I hope this pre-release will make you happy in the same way I enjoyed creating it. I beleive it contains a lot of stuff to explore and it's a little pearl of design. Thanks for downloading this, Francois [[email protected]]
WordPress高亮代码显示,前面我们介绍过WP-Syntax插件,这款插件使用起来有些麻烦,而且还需要修改文件的相关配置,对于不太熟悉程序的朋友来说,有点小困难,经过多方寻觅,今找到一款不错的WP-CodeBox代码高亮插件,它支持多种语言、代码下载、复制到剪贴板、代码框收放及后台默认属性设置多项功能,使用起来也比较方便。 WP-CodeBox插件安装: 1.下载WP-CodeBox插件,将压缩包解压后,把文件夹上传到wp-content/plugins/目录下。 2.登录WordPress管理后台,点击“Plugins”找到上传的插件,激活该插件。 3.插件激活后,在“Setting”-->“WP-CodeBox“中设置相关信息,如下图所示(点击浏览大图): 在图中我基本上对插件的相关功能都进行了说明,在页面中,其实你也不用进行任何设置,保持默认即可,安装成功后,接下来就是使用插件了,使用之前先来介绍下WP-CodeBox插件的相关语法: <pre lang="LANGUAGE" line="N" file="download.txt" colla="+">代码</pre> lang="LANGUAGE":代码的语言,如ASP、PHP、Java等 file="download.txt":创建一个可下载的保存名称 line="N":开始行数 colla="+/-": ”+“表示展开,”-“表示收缩 知道语法后我们,就可以在文章中添加高亮代码了,新建post或pages页面时插入代码,进入HTML编辑状态(一定要记住),插入相关代码,示例如下: **Example 1: PHP, no line numbers** <pre lang="php"> <div id="foo"> <?php function foo() { echo "Hello World!\\n"; } for (\$i = 0; \$i < 10 $i++) { foo(); } ?> </div> </pre> 代码前台显示图示(其它的就贴图示了): **Example 2: Java, with line numbers,collapse codebox** <pre lang="java" line="1" colla="-"> public class Hello { public static void main(String[] args) { System.out.println("Hello World!"); } } </pre> **Example 3: Ruby, with line numbers starting at 18, code downloading(ruby.txt)** <pre lang="ruby" line="18" file="ruby.txt"> class Example def example(arg1) return "Hello: " + arg1.to_s end end </pre> 简单的几个例子,基本都可以对其功能进行说明,此款插件使用起来还是非常方便的,不需要额外的操作,而且高亮显示的效果也十分的明显。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值