1. 安装
Mac下自带ruby,使用ruby的gem命令安装即可
- 安装ruby环境,添加淘宝ruby镜像(非必须)
-
gem sources --remove https://rubygems.org/(移除亚马逊的云服务) -
gem sources -a http://ruby.taobao.org/(使用国内淘宝源) - 使用命令
gem sources -l查看时候设置成功
-
- gem升级使用命令
sudo gem update --system - 安装Cocoapods
- 使用命令
sudo gem install cocoapods - 使用命令
pod setup设置pods环境
- 使用命令
- Cocoapods常用命令
pod init在当前目录生成Podfile文件(当前目录为Xcode项目目录)pod install罗列Podfile的依赖库的版本,并生成Podfile.lock,添加响应库到项目中,生成workspacepod list罗列所有可用的pods第三方库pod update更新项目中过时的依赖库,同时创建新的Podfile.lock文件pod search XXX搜索可用的pods库pod lib开发pods库pod setup设置pods环境,CocoaPods所有项目的Podspec(配置)文件都托管在https://github.com/CocoaPods/Specs.第一次执行时,CocoaPods会将这些podspec索引文件更新到本地的~/.cocoapods/目录下.
__注__:`Podfile.lock`会锁定当前各依赖库的版本,多次`pod install`不会更改版本,`pod update`才会更改`Podfile.lock`
2. 管理第三方库
以添加AFNetworking为例,演示CocoaPods管理第三方库
- 使用Xcode创建工程文件.eg
Demo - 打开终端,进入
Demo文件夹 - 使用
pod init命令生成Podfile文件.此时Demo目录下有Demo.xcodeproj,Demo/,DemoTests/,DemoUITests/,Podfile - 使用编辑器打开
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 - 在当前目录下执行
pod install命令,完成即安装第三方库成功,打开Demo.xcworkspace即可使用 - 删除
AFNetworking,只需在步骤4中把pod 'AFNetworking', '~> 2.5.4'删除,执行步骤5即可
目录1.png
3. 创建私有静态库
创建静态库有两种方法:
- 在Xcode中创建一个
Coco Touch Static Library,创建Podfile同 管理第三方库 - 使用pod自动创建
-
执行命令
pod lib create XXX(DMBaseLib)
目录2.png -
进入
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 验证类库:
pod lib lint XXX.podspec或者pod lib lint --sources=https://github.com/NicolasKim/NCKSpecs.git,https://github.com/CocoaPods/Specs.git --allow-warnings其中
s.frameworks和s.libraries指定依赖的SDK中的framework和类库,依赖不仅包含自己类库的依赖,还要包括所有第三方库的依赖,只有这样该静态文件打包成.a或者.framework时才能让其他项目正常使用.-
将所创建的本地库引入项目
- 在
DMBaseLib同级目录下使用Xcode创建自己的项目ToDoList目录3.png - 使用命令
pod init创建Podfile - 将
pod ‘DMBaseLib’, :path => ‘../DMBaseLib/’添加到Podfile中,执行pod install - 提交本地代码库
- 修改
s.source
s.source = { :git => "/Users/name/workspace/DMBaseLib", :tag => '0.1.0'} - 提交源码,并打tag
git add . git commit -a -m 'v0.1.0' git tag -a 0.1.0 -m 'v0.1.0'
- 在
-
将所创建的库push到代码服务器,然后通过podfile引入项目
- 只需修改第5条的3,4.1步骤,其他照旧.
- 将第3步中的
pod ‘DMBaseLib’, :path => ‘../DMBaseLib/’改为pod ‘DMBaseLib’, :git => 'https://xxx/DMBaseLib', :tag => '0.1.0' - 将第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来完成类库的打包.手动编译打包过程相当繁琐.
- 安装打包插件命令
sudo gem install cocoapods-packager - 打包
- 执行命令
pod package DMBaseLib.podspec --library --force(pod package xxx.podspec --library --spec-sources=ssh://xxxx/ios/specs.git --force)
__注意:--library指定打包成.a文 - 件,如果不带上,将会打包成
.framework文件.__
- 执行命令



