iOS接下来要研究的知识点cocoaPods

iOS开发 CocoaPods(一)创建私有库

2022-12-22  本文已影响0人  Leoeoo

一、创建私有库模板

1、创建LeoPrivatePodTest库

pod lib create LeoPrivatePodTest

2、执行完上面命令后,会让你选择私有库配置

//选择平台
What platform do you want to use?? [ iOS / macOS ]
 > iOS
//选择编程语言
What language do you want to use?? [ Swift / ObjC ]
 > ObjC
//是否创建测试Demo
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 ]
 > No
//测试Demo的类前缀
What is your class prefix?
 > Leo

3、会得到如下的模板项目

会自动在LeoPrivatePodTest目录下添加git,后续我们只需把git与远端的仓库关联。


image.png

4、创建文件并放到指定目录:

可以把创建的类放到Classes文件夹下,也可以放到其他目录。只需要在LeoPrivatePodTest.podspec中标记对应的路径即可:

Pod::Spec.new do |s|
  s.name             = 'LeoPrivatePodTest'
  s.version          = '0.1.0'
  s.summary          = 'A short description of LeoPrivatePodTest.'
  s.description      = <<-DESC
TODO: Add long description of the pod here.
                       DESC

  s.homepage         = 'https://gitee.com/ayangcool100/leo-private-pod-test'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'leo' => 'ayangcool100' }
  s.source           = { :git => 'https://gitee.com/ayangcool100/leo-private-pod-test.git', :tag => s.version.to_s }
  s.ios.deployment_target = '10.0'
  s.requires_arc = true
  s.static_framework = true
  s.default_subspecs = 'Source'
  s.subspec 'Source' do |sc|
    sc.source_files = 'LeoPrivatePodTest/**/*{h,m}'
    sc.pod_target_xcconfig = {
      'VALID_ARCHS' => 'armv7 arm64',
      'ENABLE_BITCODE' => 'NO',
#      'HEADER_SEARCH_PATHS' => '$(SRCROOT)/../../LeoPrivatePodTest/',
#      'LIBRARY_SEARCH_PATHS' => '$(SRCROOT)/../../LeoPrivatePodTest/',
      'BUILD_LIBRARY_FOR_DISTRIBUTION' => true
      # 'OTHER_LDFLAGS' => '$(inherited)'
    }
    sc.public_header_files = 'LeoPrivatePodTest/**/*.h'
  end
  
  #  s.subspec 'Dog' do |sd|
  #    sd.source_files = 'LeoPrivatePodTest/Dog/**/*{h,m}'
  #    sd.pod_target_xcconfig = {
  #      'VALID_ARCHS' => 'armv7 arm64',
  #      'ENABLE_BITCODE' => 'NO',
  ##      'HEADER_SEARCH_PATHS' => '$(SRCROOT)/../../LeoPrivatePodTest/',
  ##      'LIBRARY_SEARCH_PATHS' => '$(SRCROOT)/../../LeoPrivatePodTest/',
  #      'BUILD_LIBRARY_FOR_DISTRIBUTION' => true
  #      # 'OTHER_LDFLAGS' => '$(inherited)'
  #    }
  #    sd.public_header_files = 'LeoPrivatePodTest/Dog/**/*.h'
  #  end
  
  # s.resource_bundles = {
  #   'LeoPrivatePodTest' => ['LeoPrivatePodTest/Assets/*.png']
  # }

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

5、测试代码

修改完LeoPrivatePodTest.podspec后,cd到Example文件夹下执行pod install。检查是否设置正确。
代码更新后先关闭workspace,一定要执行:pod install 来更新,更新完需要重新打开workspace。

6、提交修改并打tag上传

cd到LeoPrivatePodTest(git所在目录)执行:

git add .
git commit -m "提交代码"
git remote add origin https://gitee.com/ayangcool100/leo-private-pod-test.git
git push origin master
git tag 0.1.0
git push origin 0.1.0

必须要打tag,因为其他人通过 pod 'LeoPrivatePodTest' 导入时,需要根据tag去查找。
tag建议在校验podspec之后再打,因为如果podspec校验不通过,还需要修改podspec或代码。

二、检验podspec

podspec制作后需要做检验,检验通过后上传到索引库,以供其他人通过索引使用。
cd到LeoPrivatePodTest(LeoPrivatePodTest.podspec所在目录)执行:

pod lib lint LeoPrivatePodTest.podspec --allow-warnings

--allow-warnings:允许警告
--use-libraries:代码中使用到了静态库.a
--skip-import-validation:跳过头文件验证(一般不要使用)
--sources:指定source,多个索引库中间用逗号分割。
如:
--sources=https://github.com/CocoaPods/Specs.git,https://gitee.com/ayangcool100/leo-podspec.git
下一步就是把校验通过的podspec上传到索引库:
iOS开发 CocoaPods(二)创建索引并使用

三、pod开发模式

1、由于创建的pod库只是在本地测试,没有上传到git。此时是通过:

pod 'LeoPrivatePodTest', :path => '../'

这种方式加载的。
而且在Pods的target中,是在Development Pods目录下。

2、开发模式加载其他三方库

1、以加载 Masonry 为例,先去Masonry仓库下载Masonry.podspec:https://github.com/SnapKit/Masonry/blob/master/Masonry.podspec

同时把Masonry源代码下载下来:https://github.com/SnapKit/Masonry
目录结构如下:

image.png
2、把Masonry文件夹放到Demo里面(可以随意放置,podfile里面路径写对就行)
image.png
3、podfile里加载Masonry:
image.png
此时Masonry就被加载进去了,其他的三方库加载类似。
demo地址:https://gitee.com/ayangcool100/LeoDNS
上一篇 下一篇

猜你喜欢

热点阅读