CocoaPods 简要使用笔记

1. 常用指定使用

1.1 指定库使用的 Swift 版本

post_install do |installer|
    installer.pods_project.targets.each do |target|
        if target.name == 'ActiveLabel'
            target.build_configurations.each do |config|
                config.build_settings['SWIFT_VERSION'] = '3.2'
            end
        end
    end
end
target 'MyTarget' do
    use_frameworks!

    # Post installation script that enables the Swift 4.2 compiler's
    # legacy 4.1 mode for 4.2-incompatible pods
    post_install do |installer|
      incompatiblePods = ['PodA', 'PodB']

      installer.pods_project.targets.each do |target|
          if incompatiblePods.include? target.name
              target.build_configurations.each do |config|
                  # Replace with whichever Swift version you're migrating from
                  config.build_settings['SWIFT_VERSION'] = '4.1'
              end
          end
      end
    end

    pod 'XXXX'
    pod 'XXXX'
end

可以通过 Podfile 设置旧的还没支持 Swift4 的库指定用 Swift3.2 编译。
当然也可以一个个的在 Target 的 Build Settings 中设置 Swift complie language version


1.2 指定在 Debug 下安装库

pod 'GDPerformanceView-Swift', '~> 1.2.0', :configurations => ['Debug']

1.3 指定 Master等分支版本

pod 'ActiveLabel', :git => 'https://github.com/optonaut/ActiveLabel.swift', :branch => 'master'

1.4 指定本地目

pod 'ICSMainFramework', :path => "./Library/ICSMainFramework/"

2. 发布 CocoaPod 库

1.如果已经本地已经有了建好的 CocoaTouchFrameWork 的 project,那可以在项目目录下执行:

pod spec create YourFrameworkName

如果本地没有建好的project可以执行:

pod lib create YourFrameworkName
  1. 编辑该目录下自动创建好的YourFrameworkName.podsepc文件
  2. 发布库:
    1. Github 上创建好库;
    2. clone 到本地,并把之前的代码拷贝到次 clone 的目录;
    3. 更新.podsepc文件里对应的 git URL 为你在 Github 的地址
    s.source = { :git => "https://github.com/xxx/xxx.git", :tag => "#{s.version}" }
    
    1. 把代码 push 上去;
    2. 打标记
    git tag 0.1.0
    git push --tags
    
    1. 注册 truck
    pod trunk register [email protected] 'xxxxx'
    
    1. 上面 truck register 会发一封邮件给你,去邮件里验证。然后就可以
    pod trunk push --allow-warnings
    
  3. 管理库:
    查看自己的信息
    pod trunk me
    
    添加管理维护者
    pod trunk add-owner [库的名字] [新管理者的邮件]
    pod trunk remove-owner [库名] [邮件]
    
    查看库信息
    pod trunk info [库名]
    
    小结:
    在验证和上传你的podspec文件到trunk之前,需要将你的源码push到Github上,tag一个版本号并发布一个release版本,这样podspec文件中的s.source的值才能是准确的:
    git add -A && git commit -m "Release 1.0.1."  
    git tag '1.0.1'  
    git push --tags  
    git push origin master
    这两条命令是为pod添加版本号并打上tag:
    set the new version to 1.0.1
    set the new tag to 1.0.1
    
  4. 更新发布:
    1. 修改podspec文件tag版本号,也就是修改s.version的值,提交修改后的代码和podspec文件到Github仓库,重新发布一个Release版本
    2. 进入项目根目录,校验podspec文件,校验成功后,重新push podspec文件到CocoaPods官方仓库,命令如下:
    pod cache clean --all // 清除pod缓存
    pod lib lint xxx.podspec --allow-warnings // 校验
    pod trunk push xxx.podspec --allow-warnings // 提交到CocoaPods官方仓库
    

遇到问题:
pod lib lint 在本地执行是成功的
但是在 pod trunk push 的时候一直报错:

`source_files` pattern did not match any file.

后来把文件路径修改为以.git文件所在级别后,本地lib lint不成功,但是push却成功

"Source/*.{h,m,swift}"修改成:
"HSCycleGalleryView/**/*.{h,m,swift}"

本人预测是pod lib lint 检测的source_files是针对本地.podspec所在的文件目录;而pod tunk push是检测github上的文件目录

#可以详细输出日志信息
pod trunk push xx.podspec --verbose

podspec文件指定Swift版本:
s.swift_version = '>= 3.2, <= 4.0'

3. SVN 私有库

按照SVN 工作流规范,在项目名称目录下建立三个文件夹

  • branches
  • tags
  • trunk
root
|-- trunk
|-- branches
|   |-- v1.0-dev
|   |-- v1.0-stage
|   |-- v2.1-dev
|   |-- v2.1-stage
|   |-- v2.1.1-stage
|   `-- ...
`-- tags
    |-- v1.0.0
    |-- v1.1.0
    |-- v1.1.1
    |-- v2.0.0
    `-- ...

在下面目录下 pod lib create XXX

/Users/Hanson/Desktop/ios-svn/ios-app/DingdongExtensionKit/trunk/

当库开发完成后,利用svn工具(比如cornerstone),在右击runk 目录建立 tag 比如 0.1.1

# DingdongExtensionKit.podspec
Pod::Spec.new do |s|
  s.name         = "DingdongExtensionKit"
  s.version      = "0.1.1"
  s.summary      = "DingdongExtensionKit."
  s.homepage     = "https://github.com/zyphs21/"
  s.author       = { "zyphs21" => "[email protected]" }
  # 注意这里使用 svn=> 以及指向之前 创建的tag
  s.source       = { :svn => "http://dev.vanyun.cn:8000/svn/vanyun/Dingdong/Projects/ios-app/DingdongExtensionKit/", :tag => s.version.to_s }
  s.source_files  = "DingdongExtensionKit/**/*.{swift}"
end

这个库就算创建好了,使用的时候在Podfile

# pod 'DingdongUserKit', :path => '/Users/Hanson/Desktop/ios-svn/ios-app/DingdongUserKit/trunk/'
pod 'DingdongExtensionKit', :svn => 'http://dev.vanyun.cn:8000/svn/vanyun/Dingdong/Projects/ios-app/DingdongExtensionKit/', :tag => '0.1.1'

4. Pod 里加入图片资源

Pod::Spec.new do |s|
  s.name           = "YMPhotoBrowser"
  s.version        = "1.0.0"
  s.summary        = "YMPhotoBrowser is a modern looking photo gallery written in Swift for iOS."
  s.homepage       = "https://github.com/zyuanming/YMPhotoBrowser"
  s.license        = { :type => 'MIT', :file => 'LICENSE' }
  s.author         = { "zyuanming" => "[email protected]" }
  s.swift_version  = '4.0'
  s.ios.deployment_target = '8.0'
  s.source         = { :git => "https://github.com/zyuanming/YMPhotoBrowser.git", :tag => "#{s.version}" }
  s.source_files   = "YMPhotoBrowser/*"
  
  # 使用Resource Bundle
  s.resource_bundles = { 'Image' => ['YMPhotoBrowser/Resources/*.png']}
  
  s.dependency 'Kingfisher', '~> 4.5.0'
end

然后使用图片的时候:

let frameworkBundle = Bundle(for: type(of: self))
// 这里对应 s.resource_bundles = { 'Image' => ['YMPhotoBrowser/Resources/*.png']}
let resourceBundleURL = frameworkBundle.url(forResource: "Image", withExtension: "bundle")
let resourceBundle = Bundle(url: resourceBundleURL!)
let image = UIImage(named: "photo_dismiss", in: resourceBundle, compatibleWith: nil)

5. Pod Update和outdated

每当你运行pod outdated命令时,CocoaPods会列出所有在Podfile.lock中的有新版本的pod库。这意味着当你对这些 pod 使用 pod update PODNAME 时,他们会更新(只要新版本仍然遵守你在Podfile中做的类似于pod 'MyPod', '~>x.y'这样的限制)

6. 版本号指定

= 0.1 Version 0.1.
> 0.1 Any version higher than 0.1.
>= 0.1 Version 0.1 and any higher version.
< 0.1 Any version lower than 0.1.
<= 0.1 Version 0.1 and any lower version.
~> 0.1.2 Version 0.1.2 and the versions up to 0.2, not including 0.2. This operator works based on the last component that you specify in your version requirement. The example is equal to >= 0.1.2 combined with < 0.2.0 and will always match the latest known version matching your requirements.
~> 0.1.3-beta.0 Beta and release versions for 0.1.3, release versions up to 0.2 excluding 0.2. Components separated by a dash (-) will not be considered for the version requirement.
A list of version requirements can be specified for even more fine grained control.

你可能感兴趣的:(CocoaPods 简要使用笔记)