工作生活

CocoaPods管理私有库

2019-07-02  本文已影响0人  相瑾瑜

将可以复用的功能模块或者业务模块提取出来制作成私有库,通过Git托管代码,CocoaPods来管理这些私有库。不论是公司内部使用还是组件化都是必不可少的。

手动引入三方代码库或者其他团队的代码库,有时候需要对项目进行一些配置,出现错误有的还不太好解决,用CocoaPods管理这些代码库,可以简化配置,可以自动配置编译选项。通过GitLab(或者其他代码托管平台)来创建私有库,可以很好的保证的代码库的隐私性。

本文是使用GitLab和CocoaPods来创建、导入私有库。


1. 本地创建私有库

1. 在本地需要创建私有库的位置使用pod创建私有库

 pod lib create PrivateRepo

2. 回答几个问题

下图回答仅供参考,可以根据实际情况填写。

本地创建私有库

3. 现在会弹出新建好的项目,在项目文件夹中找到PrivateRepo.podspec文件,打开文件,进行编辑。

文件原始内容为:

#
# Be sure to run `pod lib lint PrivateRepo.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             = 'PrivateRepo'
  s.version          = '0.1.0'
  s.summary          = 'A short description of PrivateRepo.'

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

  s.ios.deployment_target = '8.0'

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

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

这个文件的内容决定了你项目的配置,依赖。下面分块进行说明:

A. 基本信息

Pod::Spec.new do |s|
    //库名称
    s.name          = 'PrivateRepo'

    //库版本号,这也是我们podfile文件指定的版本号。 每次发布版本都需要打tag标签(名称就是版本号)
    s.version       = ‘1.0.0'

    //pod简介
    s.summary = 'ARC and GCD Compatible Reachability Class for iOS and OS X.'
        
    //详细描述
    s.description = <<-DESC                     Computes the meaning of life.
                 Features:
                 1. Is self aware
                 ...
                 42. Likes candies.
               DESC

B. 下载链接、版本信息

//许可证,除非源代码包含了LICENSE.*或者LICENCE.*文件,否则必须指定许可证文件。文件扩展名可以没有,或者是.txt,.md,.markdown
    s.license       = { :type => 'BSD’ }
    or s.license = ‘MIT'
    or s.license = { :type => 'MIT', :file => 'MIT-LICENSE.txt’ }
    or s.license = { :type => 'MIT', :text => <<-LICENSE
                        Copyright 2012
                         Permission is granted to...
                         LICENSE
                      }

    //pod主页
    s.homepage      = 'https://github.com/tonymillion/Reachability’      

    //pod库维护者的名车和邮箱
    s.authors       = { 'Tony Million' => 'tonymillion@gmail.com’ } 
    or s.author = 'Darth Vader'
    or s.authors = 'Darth Vader', 'Wookiee'
    or s.authors = { 'Darth Vader' => 'darthvader@darkside.com',
                    'Wookiee'     => 'wookiee@aggrrttaaggrrt.com' }     

    //指定多媒体地址,如果是推特发布版本会有通知
    s.social_media_url = 'https: //twitter.com/cocoapods 
    or s.social_media_url = 'https: //groups.google.com/forum/#!forum/cocoapods'
    
   
    
    //获取库的地址
    a. Git:git地址,tag:值以v开头,支持子模块
        s.source        = { :git => 'https://github.com/tonymillion/Reachability.git', :tag => 'v3.1.0’ }
        s.source = { :git => 'https://github.com/typhoon-framework/Typhoon.git',
                :tag => "v#{s.version}", :submodules => true }
    b. Svn:svn地址
        s.source = { :svn => 'http://svn.code.sf.net/p/polyclipping/code', :tag => ‘4.8.8‘ }
    c. Hg:Mercurial
        s.source = { :hg => 'https://bitbucket.org/dcutting/hyperbek', :revision => "#{s.version}" }
    
    // Pod 屏幕截图,支持单个或者数组,主要适用于UI类的pod库。cocoapods推荐使用gif
    s.screenshot  = 'http://dl.dropbox.com/u/378729/MBProgressHUD/1.png'
    or s.screenshots = [ 'http://dl.dropbox.com/u/378729/MBProgressHUD/1.png',
                            'http://dl.dropbox.com/u/378729/MBProgressHUD/2.png' ]
    
    // 说明文档地址
    s.documentation_url  =  'http://www.example.com/docs.html’
    
    // pod下载完成之后,执行的命令。可以创建,删除,修改任何下载的文件。该命令在pod清理之前和pod创建之前执行。
    s.prepare_command = 'ruby build_files.rb'
    or s.prepare_command = <<-CMD                        sed -i 's/MyNameSpacedHeader/Header/g' ./**/*.h
                    sed -i 's/MyNameOtherSpacedHeader/OtherHeader/g' ./**/*.h
             CMD
            
    //module name
    s.module_name   = ‘Rich'
    //支持的swift版本
    s.swift_version = ‘4.0'
    // 支持的Cocoapods版本
    s.cocoapods_version = ‘>=0.36’

C. 是否要废弃库

    // 库是否废弃
    s.deprecated = true

    //  废弃的pod名称
    s.deprecated_in_favor_of = 'NewMoreAwesomePod'

D. 如果项目中依赖了静态库,比如:,需要说明使用静态库

    // 是否使用静态库。如果podfile指明了use_frameworks!命令,但是pod仓库需要使用静态库则需要设置
    s.static_framework = true

E. 支持平台

    // pod支持的平台,如果没有设置意味着支持所有平台,使用deployment_target支持选择多个平台
    s.platform = :osx,  ’10.8'
    or s.platform = :ios
    or s.platform = :osx         

F. 添加Framework

  s.vendored_frameworks = 'SQShareKit/ShareKitPlatformSDK/QQ/*.framework' # 使用的三方framework

G. 添加bundle资源

s.resource_bundles = {
    'TencentOpenApi_IOS_Bundle' => ['SQShareKit/ShareKitPlatformSDK/QQ/*'] # 资源文件
  }

H. 依赖其他三方库

s.dependency 'WechatOpenSDK'

I. 如果有文件基于MRC,有文件支持ARC。可以使用subspec子模块配置来解决。

  s.requires_arc = true # 基于ARC
  non_arc_files = 'SQYCCategories/Classes/Foundation/NSThread+SQAdd.{h,m}' # 指出不支持ARC的文件
  s.exclude_files = non_arc_files 
  s.subspec 'no-arc' do |sp|  # 使用subspec(子模块配置),每一个子模块中可以各自设置,决定是否使用ARC
    sp.source_files = non_arc_files
    sp.requires_arc = false
  end

4. 编辑好PrivateRepo.podspec文件之后在,自动生成项目的Example下执行pod install,可以将依赖的三方库都pod下来

5.在PrivateRepo的Class文件夹下添加你的代码。然后再回到Example文件夹下执行pod install。如果PrivateRepo.podspec文件或者PrivateRepo文件下的内容有改动的话,都需要在Example下执行pod install。

6. 可以在Example项目中测试一下代码有没有问题。

7. 在GitLab上创建私有项目

GitLab创建私有项目

记录下来项目的地址和git地址

8. 运行pod lib lint PrivateRepo.podspec验证私有库正确性

有时候会遇到warnings、添加静态库等问题可以添加下面命令

--allow-warnings  忽略警告
--verbose 显示检查编译的详细信息
--use-libraries 使用静态库

基本上执行pod lib lint PrivateRepo.podspec --verbose --use-libraries --allow-warnings这一行就直接梭哈了。

9. 提交代码到GitHub,并打tag

$ git remote add origin http://gitlab.hqzhuanche.com/app/iOS-Architecture/PrivateRepo.git

$ git add .

$ git commit -a -m "1.0.0"

$ git pull origin master

$ git push origin master

$ git tag 1.0.0

$ git push origin 1.0.0

10. 创建发布私有库

在GitLab上继续创建一个私有项目,用来保存.podspec文件,然后项目里面通过这个私有项目来加载私有库。
这个私有项目命名为:PrivateSpecs

执行终端命令

pod repo add PrivateSpecs http://gitlab.hqzhuanche.com/app/iOS-Architecture/PrivateSpecs.git

11. 将PrivateRepo.podspec放到PrivateSpecs上。

pod repo push PrivateSpecs PrivateRepo.podspec --allow-warnings

12. 在你的项目里编辑Podfile文件

source 'https://github.com/CocoaPods/Specs.git'
source 'http://gitlab.hqzhuanche.com/app/iOS-Architecture/PrivateSpecs.git'

platform :ios, ‘8.0’

target “YourProject” do

pod 'PrivateRepo', '~> 1.0.0’

end

参考链接:

  1. 私有仓库的创建范例一
    https://juejin.im/post/5accdbc86fb9a028ca534f2e
  2. 私有仓库的创建范例二
    https://blog.devzeng.com/blog/ios-cocoapods-private-repo.html
  3. 私有仓库的创建范例三
    https://www.jianshu.com/p/0c640821b36f
  4. pod库包含MRC的文件
    https://blog.csdn.net/pwf2006/article/details/77244163
  5. 加载图片等资源文件
    https://www.jianshu.com/p/1699f537558a
  6. 解决报错 - ERROR | [iOS] unknown: Encountered an unknown error (The 'Pods-App' tar......
    https://blog.csdn.net/weixin_34006468/article/details/87206571
上一篇 下一篇

猜你喜欢

热点阅读