美文网首页
Cocoapods安装及私有库创建管理

Cocoapods安装及私有库创建管理

作者: BeanDu | 来源:发表于2017-03-07 18:32 被阅读0次

1. 安装

Mac下自带ruby,使用ruby的gem命令安装即可

  1. 安装ruby环境,添加淘宝ruby镜像(非必须)
    1. gem sources --remove https://rubygems.org/(移除亚马逊的云服务)
    2. gem sources -a http://ruby.taobao.org/(使用国内淘宝源)
    3. 使用命令gem sources -l查看时候设置成功
  2. gem升级使用命令sudo gem update --system
  3. 安装Cocoapods
    1. 使用命令sudo gem install cocoapods
    2. 使用命令pod setup设置pods环境
  4. Cocoapods常用命令
  1. pod init在当前目录生成Podfile文件(当前目录为Xcode项目目录)
  2. pod install罗列Podfile的依赖库的版本,并生成Podfile.lock,添加响应库到项目中,生成workspace
  3. pod list罗列所有可用的pods第三方库
  4. pod update更新项目中过时的依赖库,同时创建新的Podfile.lock文件
  5. pod search XXX搜索可用的pods库
  6. pod lib开发pods库
  7. pod setup设置pods环境,CocoaPods所有项目的Podspec(配置)文件都托管在https://github.com/CocoaPods/Specs.第一次执行时,CocoaPods会将这些podspec索引文件更新到本地的~/.cocoapods/目录下.
  __注__:`Podfile.lock`会锁定当前各依赖库的版本,多次`pod install`不会更改版本,`pod update`才会更改`Podfile.lock`

2. 管理第三方库

​以添加AFNetworking为例,演示CocoaPods管理第三方库

  1. 使用Xcode创建工程文件.eg Demo
  2. 打开终端,进入Demo文件夹
  3. 使用pod init命令生成Podfile文件.此时Demo目录下有Demo.xcodeproj,Demo/,DemoTests/,DemoUITests/,Podfile
  4. 使用编辑器打开Podfile文件进行编辑,内容如下:
    # Uncomment this line to define a global platform for your project
    # platform :ios, '8.0'
    # Uncomment this line if you're using Swift
    # use_frameworks!
    
    target 'Demo' do
    pod 'AFNetworking', '~> 2.5.4'
    end
    
  5. 在当前目录下执行pod install命令,完成即安装第三方库成功,打开Demo.xcworkspace即可使用
  6. 删除AFNetworking,只需在步骤4中把pod 'AFNetworking', '~> 2.5.4'删除,执行步骤5即可
    目录1.png

3. 创建私有静态库

创建静态库有两种方法:

  1. 在Xcode中创建一个Coco Touch Static Library,创建Podfile管理第三方库
  2. 使用pod自动创建
    1. 执行命令pod lib create XXX(DMBaseLib)

      目录2.png
    2. 进入DMBaseLib打开DMBaseLib.podspec文件,内容如下

      #
      # Be sure to run `pod lib lint DMBaseLib.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 http://guides.cocoapods.org/syntax/podspec.html
      #
      
      Pod::Spec.new do |s|
        s.name             = "DMBaseLib"
        s.version          = "0.1.0"
        s.summary          = "A short description of DMBaseLib."
      
      # 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/<GITHUB_USERNAME>/DMBaseLib"
        # s.screenshots     = "www.example.com/screenshots_1", "www.example.com/screenshots_2"
        s.license          = 'MIT'
        s.author           = { "Bean" => "xxxxxx@qq.com" }
        s.source           = { :git => "https://github.com/<GITHUB_USERNAME>/DMBaseLib.git", :tag => s.version.to_s }
        # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
      
        s.ios.deployment_target = '8.0'
      
        s.source_files = 'DMBaseLib/Classes/**/*'
        
        # s.resource_bundles = {
        #   'DMBaseLib' => ['DMBaseLib/Assets/*.png']
        # }
      
        # s.public_header_files = 'Pod/Classes/**/*.h'
        s.frameworks = 'UIKit'
        s.dependency 'AFNetworking', '~> 2.5.4'
        s.dependency 'FCUUID', '~> 1.1.4'
      end
      
    3. 验证类库:pod lib lint XXX.podspec或者pod lib lint --sources=https://github.com/NicolasKim/NCKSpecs.git,https://github.com/CocoaPods/Specs.git --allow-warnings

    4. 其中s.frameworkss.libraries指定依赖的SDK中的framework和类库,依赖不仅包含自己类库的依赖,还要包括所有第三方库的依赖,只有这样该静态文件打包成.a或者.framework时才能让其他项目正常使用.

    5. 将所创建的本地库引入项目

      1. DMBaseLib 同级目录下使用Xcode创建自己的项目ToDoList 目录3.png
      2. 使用命令pod init创建Podfile
      3. pod ‘DMBaseLib’, :path => ‘../DMBaseLib/’添加到Podfile中,执行pod install
      4. 提交本地代码库
      1. 修改s.source
        s.source = { :git => "/Users/name/workspace/DMBaseLib", :tag => '0.1.0'}
      2. 提交源码,并打tag
        git add . git commit -a -m 'v0.1.0' git tag -a 0.1.0 -m 'v0.1.0'
    6. 将所创建的库push到代码服务器,然后通过podfile引入项目

      1. 只需修改第5条的3,4.1步骤,其他照旧.
      1. 将第3步中的pod ‘DMBaseLib’, :path => ‘../DMBaseLib/’改为pod ‘DMBaseLib’, :git => 'https://xxx/DMBaseLib', :tag => '0.1.0'
      2. 将第4步中的s.source = { :git => "/Users/name/workspace/DMBaseLib", :tag => '0.1.0'}改为`s.source = { :git => 'https://xxx/DMBaseLib', :tag => '0.1.0'}

4.打包自己的第三方库

需要使用cocoapods的插件cocoapods-packager来完成类库的打包.手动编译打包过程相当繁琐.

  1. 安装打包插件命令sudo gem install cocoapods-packager
  2. 打包
    1. 执行命令pod package DMBaseLib.podspec --library --force(pod package xxx.podspec --library --spec-sources=ssh://xxxx/ios/specs.git --force)
      __注意:--library指定打包成.a
    2. 件,如果不带上,将会打包成.framework文件.__

参考文档1
参考文档2

相关文章

网友评论

      本文标题:Cocoapods安装及私有库创建管理

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