在开发的过程中很多功能模块可能会在同一个App的不同的业务模块出现,也可能在不同的App出现,为了提高我们的开发效率,我们应该将普遍使用的功能模块封装成组件,并通过Cocoapods来管理
注册
pod trunk register 邮箱 '用户名' --description='电脑描述'
查收邮件
- 如果是QQ邮箱,可能会被放到“垃圾箱”中,并不一定是“收件箱”
- 点击邮件中的链接: https://trunk.cocoapods.org/sessions/verify/xxxx
查看个人信息
- pod trunk me
- Name: 'Cyrill'- Email: lichunyang@outlook.com- Since: December 4th, 2016 21:22- Pods: None- Sessions:- December 4th, 2016 21:22 - July 13th, 21:00. IP: 103.244.255.84 Description: ‘MacbookProOfCyrill’
中间可能遇到这种错误
NoMethodError - undefined method 'last' for #<Netrc::Entry:0x007fc59c246378>
这时候需要尝试更新gem源或者pod
- sudo gem update –system
- sudo gem install cocoapods
- sudo gem install cocospods-trunk
创建podspec文件
接下来需要在项目根路径创建一个podspec文件来描述你的项目信息
- pod spec cretae 文件名 (比如pod spec cretae MJExtension就会生成一个MJExtension.podspec)
填写podspec内容
# spec开始 s作为spec的别名Pod::Spec.new do |s| # 项目名称 s.name = "CYPhotoLib" # 版本号 s.version = "0.0.1" # 简介 s.summary = "A photo selection framework used PhotoLib." # 项目地址,主页 s.homepage = "https://github.com/CoderCYLee/CYPhotoLib" # 开源协议 s.license = "MIT" # 作者 s.author = { "Cyrill" => "lichunyang@outlook.com" } # 作者的社交url s.social_media_url = "http://weibo.com/exceptions" # 支持最低ios版本 s.platform = :ios, "8.0" # 代码的git源地址 这里的tag=>"#{s.version}" 引用了上面的s.version 如果,在打tag的时候是用的 git tag 'v1.0.0' 那么这里需要使用 tag => "v#{s.version}" s.source = { :git => "https://github.com/CoderCYLee/CYPhotoLib.git", :tag => s.version } # 项目需要用到的ios framework s.frameworks = "UIKit", "Photos" # 项目中用到的库 # s.libraries = "stdc++ # 代码文件 s.source_files = "CYPhotoLib/Classes/**/*.{h,m}" # 资源文件 s.resources = "CYPhotoLib/Resource/*.png" # 是否ARC s.requires_arc = trueend
- 值得注意的是,现在的podspec必须有tag,所以最好先打个tag,传到github:
git tag 0.0.1git push --tags
- 删除tag
git tag -d 1.0git push origin :refs/tags/1.0git tag -a 1.0 -m "Version 1.0"git push --tags
检测podspec语法
pod spec lint CYPhotoLib.podspecpod lib lint CYPhotoLib.podspecpod spec相对于pod lib会更为精确,pod lib相当于只验证一个本地仓库,pod spec会同时验证本地仓库和远程仓库。
发布podspec
pod trunk push CYPhotoLib.podspec如果是第一次发布pod,需要去 https://trunk.cocoapods.org/claims/new 认领pod
有可能出现的错误
pod lib lint 的时候是通过了的,没有错误然后 pod trunk push 的时候报错,如下: [iOS] unknown: Encountered an unknown error (Pod::DSLError) during validation.解决:这种状况有可能是git的tag和podspec的tag不对
检测
pod setup : 初始化pod repo update : 更新仓库pod search CYPhotoLib
仓库更新
如果仓库更新慢,可以考虑更换仓库镜像
pod repo remove masterpod repo add master http://git.oschina.net/akuandev/Specs.git
遇到的坑:
- 每次修改完代码,podspec内容的version字段都要修改,否则一直会读取原来的内容
- podspec的version字段和git提交的tag需要保持相同的
- 代码中引入三方库的时候,不要用
#import"xxx.h"
,要通过#import<xxx/xxxx.h>
来引入,否则的话,会报include of non-modular header inside framework module “xxx.h”
的错误