美文网首页
iOS 创建自己的cocoapods库

iOS 创建自己的cocoapods库

作者: 不忘初心_R | 来源:发表于2019-08-23 14:32 被阅读0次

一、简介

参考这里

使用cocoaPods结合git源代码管理工具可以轻松实现项目组件化,多分支开发,本篇文章将介绍如何封装自己的pod库。

二、创建步骤

1. 创建本地库

创建名为LCPodTest的pod库(最好先cd到要创建的路径下)
$ pod lib create LCPodTest
执行完上述命令之后,会遇到以下问题,按需求填写即可

问题演示.jpg
pod库创建完成,它会自己打开刚才创建的pod库。
2. 添加库文件

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

#import "LCTestManager.h"

@implementation LCTestManager

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

@end

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


库文件添加路径.jpg

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


引入成功.jpg
3. 在GitHub上创建一个新的repository
创建repository.jpg
4.修改LCPodTest.podspec文件
#
# Be sure to run `pod lib lint LCPodTest.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             = 'LCPodTest'
  s.version          = '0.1.0'
  s.summary          = 'A short description of LCPodTest.'

# 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/905579522@qq.com/LCPodTest'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { '905579522@qq.com' => '905579522@qq.com' }
  s.source           = { :git => 'https://github.com/905579522@qq.com/LCPodTest.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '8.0'

  s.source_files = 'LCPodTest/Classes/**/*'
  
  # s.resource_bundles = {
  #   'LCPodTest' => ['LCPodTest/Assets/*.png']
  # }

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

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

 leyou@localhost  ~/Desktop/LCPodTest   master ●✚  git remote add origin https://github.com/905579522@qq.com/LCPodTest.git
 leyou@localhost  ~/Desktop/LCPodTest   master ●✚  git add .
 leyou@localhost  ~/Desktop/LCPodTest   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
 leyou@localhost  ~/Desktop/LCPodTest   master  git pull origin master --allow-unrelated-histories
// 推送到master分支
leyou@localhost  ~/Desktop/LCPodTest   master ●✚ >M<  git push origin master
// 提交版本号并push
#注意这里的版本号要与.podspec中的版本号保持一致
leyou@localhost  ~/Desktop/LCPodTest   master ●✚ >M<  git tag 0.1.0
 leyou@localhost  ~/Desktop/LCPodTest   master ●✚ >M<  git push origin 0.1.0

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

6.使用

新建一个项目并引用我们的pod库。

podfile内容如下:

target 'test' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # Pods for test
pod 'LCPodTest',:git =>"https://github.com/905579522@qq.com/LCPodTest.git"
  target 'testTests' do
    inherit! :search_paths
    # Pods for testing
  end

  target 'testUITests' do
    # Pods for testing
  end

end

cd到新建工程目录下,执行pod install


新建工程.png
7.总结

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

#尝试引入第三方依赖库
s.dependency 'AFNetworking', '~> 2.3'
更新
将podspec推送到cocoapods仓库

执行如下命令

# 邮箱为github绑定的邮箱,会发送一封带有链接的邮件,打开链接即完成注册
 leyou@localhost  ~/Desktop/LCPodTest   master  pod trunk register 905579522@qq.com 'lcObject-c' --description='regist trunk'
[!] Please verify the session by clicking the link in the verification email that has been sent to 905579522@qq.com
 leyou@localhost  ~/Desktop/LCPodTest   master  pod trunk push LCPodTest.podspec --allow-warnings
// 出现如下信息表示上传成功
 🎉  Congrats

 🚀  LCPodTest (0.1.0) successfully published
 📅  August 23rd, 00:24
 🌎  https://cocoapods.org/pods/LCPodTest
 👍  Tell your friends!

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

相关文章

网友评论

      本文标题:iOS 创建自己的cocoapods库

      本文链接:https://www.haomeiwen.com/subject/sqdosctx.html