CocoaPods使用小结

原文连接html

CocoaPods是OS X和iOS下的一个第三类库管理工具,经过CocoaPods工具咱们能够为项目添加被称为“Pods”的依赖库(这些类库必须是CocoaPods自己所支持的),而且能够轻松管理其版本。react

使用CocoaPods有如下几点好处:ios

  • 在引入第三方库时它能够自动为咱们完成各类各样的配置,包括配置编译阶段、链接器选项、甚至是ARC环境下的一些配置等。
  • 使用CocoaPods能够很方便地管理的第三方SDK,大部分稳定好用的SDK都支持cocoapods导入。
  • 在项目模块化的过程当中方便咱们模块间解耦。

Install


sudo gem install cocoapods
复制代码

查看版本


pod --version
复制代码

在开发中安装使用cocoapods要注意版本,由于通常开发过程当中要你们一块儿使用同一个工程,通常为了指定版本咱们会在工程下建立Gemfile来指定使用cocoapods的版本。git

指定使用Cocoapods的版本


除了指定Gemfile之外 , 咱们还能够安装指定版本的podsgithub

sudo gem install cocoapods -v 1.3.1
复制代码

再查看一下pod版本咱们就会发现已经安装了1.3.1swift

卸载掉不须要的版本


当咱们本地同时存在多个版本的pod的时候能够把多余的卸载掉xcode

sudo gem uninstall cocoapods
复制代码

会提示咱们选择卸载的版本安全

Select gem to uninstall:
 1. cocoapods-1.2.1
 2. cocoapods-1.3.1
 3. All versions
>
复制代码

咱们选择想要卸载的版本的序号就行了 。ruby

如何查看某个SDK的详细信息


cocoapods支持咱们去查找想要使用的仓库 , 好比咱们想查找ReactoveObjC这个库bash

pod spec cat ReactiveObjC
复制代码

咱们能够看到该仓库的配置信息。

{
  "name": "ReactiveObjC",
  "version": "3.1.0",
  "summary": "The 2.x ReactiveCocoa Objective-C API: Streams of values over time",
  "description": "ReactiveObjC (formally ReactiveCocoa or RAC) is an Objective-C\nframework inspired by [Functional Reactive Programming](\nhttp://en.wikipedia.org/wiki/Functional_reactive_programming).\nIt provides APIs for composing and **transforming streams of values**.",
  "homepage": "https://reactivecocoa.io",
  "screenshots": "https://reactivecocoa.io/img/logo.png",
  "license": {
    "type": "MIT",
    "file": "LICENSE.md"
  },
  "documentation_url": "https://github.com/ReactiveCocoa/ReactiveObjC/tree/master/Documentation#readme",
  "authors": "ReactiveCocoa",
  "social_media_url": "https://twitter.com/ReactiveCocoa",
  "platforms": {
    "ios": "8.0",
    "osx": "10.9",
    "watchos": "2.0",
    "tvos": "9.0"
  },
  "source": {
    "git": "https://github.com/ReactiveCocoa/ReactiveObjC.git",
    "tag": "3.1.0"
  },
  "source_files": [
    "ReactiveObjC/*.{h,m,d}",
    "ReactiveObjC/extobjc/*.{h,m}"
  ],
  "private_header_files": [
    "**/*Private.h",
    "**/*EXTRuntimeExtensions.h",
    "**/RACEmpty*.h"
  ],
  "ios": {
    "exclude_files": "ReactiveObjC/**/*{AppKit,NSControl,NSText,NSTable}*"
  },
  "osx": {
    "exclude_files": "ReactiveObjC/**/*{UIActionSheet,UIAlertView,UIBarButtonItem,UIButton,UICollectionReusableView,UIControl,UIDatePicker,UIGestureRecognizer,UIImagePicker,UIRefreshControl,UISegmentedControl,UISlider,UIStepper,UISwitch,UITableViewCell,UITableViewHeaderFooterView,UIText,MK}*"
  },
  "tvos": {
    "exclude_files": "ReactiveObjC/**/*{AppKit,NSControl,NSText,NSTable,UIActionSheet,UIAlertView,UIDatePicker,UIImagePicker,UIRefreshControl,UISlider,UIStepper,UISwitch,MK}*"
  },
  "watchos": {
    "exclude_files": "ReactiveObjC/**/*{UIActionSheet,UIAlertView,UIBarButtonItem,UIButton,UICollectionReusableView,UIControl,UIDatePicker,UIGestureRecognizer,UIImagePicker,UIRefreshControl,UISegmentedControl,UISlider,UIStepper,UISwitch,UITableViewCell,UITableViewHeaderFooterView,UIText,MK,AppKit,NSControl,NSText,NSTable,NSURLConnection}*"
复制代码

开始使用

首先 , 新建一个singlgViewApp ,而后在命令行进入该project目录

pod init
复制代码

咱们能够看到cocoapods为咱们生成了一个Podfile platform表示这个工程安装的设备,后面是系统最低版本 如今咱们能够在里面添加一下刚才搜索的仓库

# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'

target 'ocTest' do
  # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
  # use_frameworks!
  pod 'ReactiveObjC'
  # Pods for ocTest
end
复制代码

这里咱们添加了一个仓库,接下来再命令行执行pod update来安装所须要的仓库,安装完毕后咱们能够看到当前路径下有两个工程文件

  • Test.xcodeproj
  • Test.xcworkspace

咱们在使用了cocoapods管理第三方库的时候,每次install或者update的时候就会生成*.xcworkspace这个文件咱们须要使用这个工程进行开发调试。 如今打开工程,定位到viewController.m中就能够import并使用ReactiveObjC啦 。

如何使用私有源


公司内部有本身搭建的gitlab服务时,有的公司搭建的gitlab服务为了安全并无提供外网接口,咱们只能在内网访问,这时候就要在podfile中添加私有源的地址

source 'http://xx.xxxx.com/ios/cocoapods-spec.git'
source 'https://github.com/CocoaPods/Specs.git'  # 官方库
复制代码

上面那个就是咱们要添加的私有源地址,下面的是官方源地址,若是都不写的话那么默认就会使用官方源 。

如何建立并发布仓库到私有repo


  • 首先执行命令,后面替换为你要发布SDK的名字
pod lib create TDFCommonUtil
复制代码

  • pod会给出一些选项让咱们选择
To get you started we need to ask a few questions, this should only take a minute.

If this is your first time we recommend running through with the guide:
 - https://guides.cocoapods.org/making/using-pod-lib-create.html
 ( hold cmd and click links to open in a browser. )


What platform do you want to use?? [ iOS / macOS ]
 >
ios
What language do you want to use?? [ Swift / ObjC ]
 >
swift
Would you like to include a demo application with your library? [ Yes / No ]
 >
yes
Which testing frameworks will you use? [ Quick / None ]
 > None

Would you like to do view based testing? [ Yes / No ]
 > NO
复制代码
  • 如今咱们须要添加一些信息

cd进入刚才建立的目录,执行命令 ls 能够看到控制台有如下输出。

Example               README.md           TDFCommonUtil.podspec
LICENSE               TDFCommonUtil         _Pods.xcodeproj
复制代码

解释一下这里都是啥

  • Example 这里面就是示例工程
  • README.md 这里面是咱们对改pod功能和使用等的介绍 , 使用markdown语法
  • TDFCommonUtil.podspec 这里是改模块的所有基本信息
  • LICENSE 这里放的是改开源项目遵照的协议
  • TDFCommonUtil 这里放着所有的类文件和资源文件
  • _Pods.xcodeproj 这是示例工程的快捷方式

咱们首先要为咱们的pod建立一个实际的git仓库 。

打开gitlab服务建立一个空的git仓库 而后先把pod目录提交到咱们的仓库 blalblabla

打开TDFCommonUtil.podspec文件

Pod::Spec.new do |s|
  s.name             = 'TDFCommonUtil'
  s.version          = '0.1.0'
  s.summary          = 'A short description of TDFCommonUtil.'



  s.description      = <<-DESC TODO: Add long description of the pod here. DESC

  s.homepage         = 'https://github.com/xxxxx@yeah.net/TDFCommonUtil'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'xxxxx' => 'xxxxx@2dfire.com' }
  s.source           = { :git => 'https://github.com/xxxxx@yeah.net/TDFCommonUtil.git', :tag => s.version.to_s }

  s.ios.deployment_target = '8.0'

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

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

这里的配置项就是生成pod须要配置的大部分选项了 ,简单介绍一下

  • name pod名称
  • version 版本号 ,特别注意下在更新时须要对git仓库打tag
  • summary 简介
  • description 描述
  • homepage 主页
  • license 使用协议
  • author 做者
  • deployment_target 须要系统版本
  • source_files 源文件路径
  • resource_bundles 资源文件路径 注意这里会把资源文件打包成bundle , 调用的时候要注意下
  • public_header_files 公开的头文件 (有些头文件咱们不想要外部看见能够在这里去掉)
  • frameworks 须要依赖的framework库
  • dependency 须要依赖的其余pod

如今把这些填好吧 。

如何添加依赖


有的时候咱们开发的pod仓库须要依赖其余仓库,好比咱们须要依赖ReactiveObjC 这里就能够在TDFCommonUtil.podspec下面添加这一行

s.dependency 'ReactiveObjC'
复制代码

ps ,这里能够指向特定的版本也能够用 '~> 2.3' 的形式表示依赖此仓库至少大于2.3版本可是不会超过3.0 。

使用lint命令检测咱们的仓库是否还有问题


一切就绪后你能够在里面建立你的文件,添加代码了,别忘了再有文件的增长或删除后在运行一遍 ‘pod install’。

先随便建立几个文件,而后咱们使用cocoapods检测咱们的库

pod lib lint --sources='git@git.xxx.com:ios/cocoapods-spec.git' --use-libraries --allow-warnings --verbose --no-clean
复制代码

这里的sources填写你所使用的私有gitlab服务,而后咱们就能够静静的看着命令行了。

最后若是出现

Test passed validation.
复制代码

证实你的库是可运行的,若是没有出现passed就注意下输出中的error信息,搜索一下error看是什么致使的 。

向私有源推送


lint经过后咱们就能够把本身的仓库信息推送到私有源了,注意不是「仓库」是「仓库信息」,也就是x.podspec 。 cocoapod能够自动帮咱们完成这件事情

pod repo push xxx-cocoapods-spec TDFOpenShopSDK.podspec --sources=git@git.xxx.com:ios/cocoapods-spec.git --allow-warnings --use-libraries --verbose
复制代码

总结


Cocoapods极大方便了咱们管理外部SDK和内部模块化解耦,用好这个管理工具无疑会为咱们的工做效率带来巨大提高。

相关文章
相关标签/搜索