前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS-将自己的库支持CocoaPods

iOS-将自己的库支持CocoaPods

作者头像
用户1890628
发布2018-05-10 17:43:28
2.6K0
发布2018-05-10 17:43:28
举报
文章被收录于专栏:Objective-C

前言

随着组件化的日益兴起,通过CocoaPods管理一些自己的库是基本的要求,本文将介绍如何将自己的项目支持通过CocoaPods管理以及自己在实践过程中遇到的一些问题!

绑定自己的 GitHub 帐号

请将下面的内容替换为自己的

代码语言:javascript
复制
pod trunk register 272338444@qq.com 'password' --verbose

由于是第一次注册,下面给我返回了一堆东西

代码语言:javascript
复制
opening connection to trunk.cocoapods.org:443...
opened
starting SSL for trunk.cocoapods.org:443...
SSL established
<- "POST /api/v1/sessions HTTP/1.1\r\nContent-Type: application/json; charset=utf-8\r\nAccept: application/json; charset=utf-8\r\nUser-Agent: CocoaPods/1.0.1\r\nAccept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3\r\nHost: trunk.cocoapods.org\r\nContent-Length: 72\r\n\r\n"
<- "{\"email\":\"272338444@qq.com\",\"name\":\"wanghongqing123\",\"description\":null}"
-> "HTTP/1.1 201 Created\r\n"
-> "Date: Wed, 03 May 2017 02:00:30 GMT\r\n"
-> "Connection: keep-alive\r\n"
-> "Strict-Transport-Security: max-age=31536000\r\n"
-> "Content-Type: application/json\r\n"
-> "Content-Length: 193\r\n"
-> "X-Content-Type-Options: nosniff\r\n"
-> "Server: thin 1.6.2 codename Doc Brown\r\n"
-> "Via: 1.1 vegur\r\n"
-> "\r\n"
reading 193 bytes...
-> "{\"created_at\":\"2017-05-03 02:00:30 UTC\",\"valid_until\":\"2017-09-08 02:00:30 UTC\",\"verified\":false,\"created_from_ip\":\"1.180.215.104\",\"description\":null,\"token\":\"a2294ce47765df05a239c6e29c0e0bf2\"}"
read 193 bytes
Conn keep-alive
[!] Please verify the session by clicking the link in the verification email that has been sent to 272338444@qq.com

大概意思就是需要去注册的邮箱接收到的邮件,点击链接验证一下。大家应该都懂的,邮件是酱婶的:

  • 验证成功

查看是否注册成功,终端中执行下列命令

代码语言:javascript
复制
pod trunk me

注册成功返回

代码语言:javascript
复制
- Name:     wanghongqing123
- Email:    272338444@qq.com
- Since:    May 2nd, 20:00
- Pods:     None
- Sessions:
- May 2nd, 20:00 - September 7th, 20:06. IP: 1.180.215.104

创建 .podspec 文件

代码语言:javascript
复制
pod spec create HQSliderView
  • 如果成功,终端输出:
代码语言:javascript
复制
Specification created at HQSliderView.podspec

设置 podspec 文件内容

这里特别说明一下,之前我自己照着网上的各种版本的教程弄的时候也是反反复复很多次才弄个差不多,其实这里仔细看下,每个说明都会有非常详细的英文说明,只是自己看到英文就很烦,不愿意去仔细翻译。

如果真的没有意愿自己翻译的话,那么尽量不要在网上找各种教程,因为教程很可能都过期了,包括我自己现在写的。 所以个人建议,直接到Github上面看一看优秀的开元框架怎么配置的,这里面我就参考了AlamofireAlamofire.podspec文件,因为他们的永远是最新的。

下面是Alamofire.podspec文件内的内容(2017年06月18日)

代码语言:javascript
复制
Pod::Spec.new do |s|
s.name = 'Alamofire'
s.version = '4.5.0'
s.license = 'MIT'
s.summary = 'Elegant HTTP Networking in Swift'
s.homepage = 'https://github.com/Alamofire/Alamofire'
s.social_media_url = 'http://twitter.com/AlamofireSF'
s.authors = { 'Alamofire Software Foundation' => 'info@alamofire.org' }
s.source = { :git => 'https://github.com/Alamofire/Alamofire.git', :tag => s.version }

s.ios.deployment_target = '8.0'
s.osx.deployment_target = '10.10'
s.tvos.deployment_target = '9.0'
s.watchos.deployment_target = '2.0'

s.source_files = 'Source/*.swift'
end

下面是我自己的HQChannelListView.podspec文件内容

代码语言:javascript
复制
Pod::Spec.new do |s|

s.name = "HQChannelListView"
s.version = "0.0.1"
s.license = "MIT"
s.summary = "A ChannelListView with CollectionView by swift, it is like NetEaseNews Channel style."
s.homepage = "https://github.com/hongqingWang/adsdadasda"
s.author = { "hongqingWang" => "272338444@qq.com" }
s.source = { :git => "https://github.com/hongqingWang/adsdadasda.git", :tag => s.version }

s.ios.deployment_target = "9.0"

s.source_files = "Source/*.swift"
end

对比一下,几乎就是踩着大神的足迹往前走,其实我个人比较建议这样,因为,他们都是经过了无数次的经验之后总结出来,最简单、最需要设置的东西都写在里面了。

其实这里面相对比较重要的就是:

  • version(版本号)
  • ios.deployment_target(iOS项目支持的最低系统)
  • source_files(资源文件)

source_files这里面特别说明一下,最好也仿照Alamofire的资源文件写,clone下来好好研究一下。

如果你写的是swift的项目,你的项目里面一定要有一个.swift-version的配置文件。

不知道里面该写什么东西,没关系,看大神的(其实里面就写了一个swift的版本3.0而已)

这部其实很关键的,因为配置不好的话无法向下进行,而且网上查到的都是乱七八糟的,强烈建议以后类似的问题直接去GitHub看就好了。 配置完以后就可以进行验证了。

验证 podspec 文件

代码语言:javascript
复制
pod lib lint
  • 如果成功
代码语言:javascript
复制
-> HQSliderView (1.0.1)

HQSliderView passed validation.
  • 如果失败
代码语言:javascript
复制
bogon:Test wanghongqing$ pod lib lint HQSliderView.podspec

-> HQSliderView (1.0.1)
- ERROR | xcodebuild: Returned an unsuccessful exit code. You can use `--verbose` for more information.
- NOTE  | [OSX] xcodebuild:  /Users/wanghongqing/Documents/GitHub/Test/HQSliderView/HQSliderView.h:9:9: fatal error: 'UIKit/UIKit.h' file not found
- ERROR | [watchOS] xcodebuild:  /Users/wanghongqing/Documents/GitHub/Test/HQSliderView/HQSliderView.h:11:27: error: cannot find interface declaration for 'UIView', superclass of 'HQSliderView'
- WARN  | [watchOS] xcodebuild:  /Users/wanghongqing/Documents/GitHub/Test/HQSliderView/HQSliderView.h:11:12: warning: class 'HQSliderView' defined without specifying a base class [-Wobjc-root-class]
- NOTE  | [watchOS] xcodebuild:  /Users/wanghongqing/Documents/GitHub/Test/HQSliderView/HQSliderView.h:11:24: note: add a super class to fix this problem

[!] HQSliderView did not pass validation, due to 2 errors and 1 warning.
[!] The validator for Swift projects uses Swift 3.0 by default, if you are using a different version of swift you can use a `.swift-version` file to set the version for your Pod. For example to use Swift 2.3, run:
`echo "2.3" > .swift-version`.
You can use the `--no-clean` option to inspect any issue.

此处失败是因为之前我没有指定为iOS平台,改正之后就好了。

代码语言:javascript
复制
s.ios.deployment_target = "9.0"

如果还有其它问题,按照提示,耐心点改都可以的,基本除了.podspec文件有问题之外,剩下的就是你自己项目文件的问题了,之前我在自己文件的.h.m里面定义了一些项目中用的常量,结果验证的时候就报错了。如下所示:

代码语言:javascript
复制
- ERROR | xcodebuild:  /Users/wanghongqing/Documents/GitHub/Test/HQSliderView/HQSliderView.m:33:21: error: use of undeclared identifier 'kScreenWidth'
- ERROR | xcodebuild:  /Users/wanghongqing/Documents/GitHub/Test/HQSliderView/HQSliderView.m:44:17: error: use of undeclared identifier 'kScreenWidth'

- ERROR | xcodebuild:  /Users/wanghongqing/Documents/GitHub/Test/HQSliderView/HQSliderView.m:57:31: error: use of undeclared identifier 'HQTextColor'
- ERROR | xcodebuild:  /Users/wanghongqing/Documents/GitHub/Test/HQSliderView/HQSliderView.m:58:31: error: use of undeclared identifier 'HQColor'
- ERROR | xcodebuild:  /Users/wanghongqing/Documents/GitHub/Test/HQSliderView/HQSliderView.m:75:33: error: use of undeclared identifier 'HQLineColor'
- ERROR | xcodebuild:  /Users/wanghongqing/Documents/GitHub/Test/HQSliderView/HQSliderView.m:83:34: error: use of undeclared identifier 'HQColor'
- ERROR | xcodebuild:  /Users/wanghongqing/Documents/GitHub/Test/HQSliderView/HQSliderView.m:93:29: error: use of undeclared identifier 'HQLineColor'

说我项目中用到了没有定义的kScreenWidthHQTextColorHQColorHQLineColor等这种问题。因为这些是我在项目里统一配置的,所以单个文件拎出来的时候,肯定会报错。自己在这两个文件里再定义一下就好了,或者有其它更好的办法也可以。

给项目打上 tag 版本并推送到 GitHub

  • 在进行打tag之前,确保你已经把验证成功的.podspec文件和.swift-version(如果是 swift 项目要有这个)以及资源文件等,commitpushGitHub

CocoaPods是有版本管理的,比如你搜索SDWebImage框架的时候,结果应该有这句:

代码语言:javascript
复制
pod 'SDWebImage', '~> 4.0.0'
  • 所以你的项目也必须要打上版本号
代码语言:javascript
复制
git tag "1.0.1"
  • 注意:
    • 只打版本号终端不会有任何输出
    • git tag+ 版本号,不是pod tag+ 版本号
    • 版本号一定要和你的.podspec文件里的s.version = "1.0.1"对应了,不然虽然暂时不会报错,但是pushGitHub的时候也会报错。
  • 将你的tag推送到远程仓库
代码语言:javascript
复制
git push --tags

然后会弹出这个框,除了允许你还能选什么呢?

然后会再弹出一个框,让我们输入管理员密码

接下来终端输出:

代码语言:javascript
复制
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/hongqingWang/Test.git
* [new tag]         1.0.1 -> 1.0.1

把 Podspec 文件推送到 Cocoapods 官方库

  • HQSliderView.podspec换成你自己的xxx.podspec
代码语言:javascript
复制
pod trunk push HQSliderView.podspec

如果成功

代码语言:javascript
复制
Updating spec repo `master`
代码语言:javascript
复制
CocoaPods 1.3.0.beta.1 is available.
To update use: `sudo gem install cocoapods --pre`
[!] This is a test version we'd love you to try.

For more information, see https://blog.cocoapods.org and the CHANGELOG for this version at https://github.com/CocoaPods/CocoaPods/releases/tag/1.3.0.beta.1

Validating podspec
-> HQSliderView (1.0.1)

Updating spec repo `master`

CocoaPods 1.3.0.beta.1 is available.
To update use: `sudo gem install cocoapods --pre`
[!] This is a test version we'd love you to try.

For more information, see https://blog.cocoapods.org and the CHANGELOG for this version at https://github.com/CocoaPods/CocoaPods/releases/tag/1.3.0.beta.1


--------------------------------------------------------------------------------
?  Congrats

?  HQSliderView (1.0.1) successfully published
?  June 8th, 01:47
?  https://cocoapods.org/pods/HQSliderView
?  Tell your friends!

如果不成功

代码语言:javascript
复制
Updating spec repo `master`

CocoaPods 1.3.0.beta.1 is available.
To update use: `sudo gem install cocoapods --pre`
[!] This is a test version we'd love you to try.

For more information, see https://blog.cocoapods.org and the CHANGELOG for this version at https://github.com/CocoaPods/CocoaPods/releases/tag/1.3.0.beta.1

Validating podspec
-> HQSliderView (1.0.1)
- ERROR | [iOS] unknown: Encountered an unknown error ([!] /usr/bin/git clone https://github.com/hongqingWang/Test.git /var/folders/mp/3mq9dyy14dx34qb12_r6nfj00000gn/T/d20170608-4256-i7n2sv --template= --single-branch --depth 1 --branch 1.0.1
                                                       
Cloning into '/var/folders/mp/3mq9dyy14dx34qb12_r6nfj00000gn/T/d20170608-4256-i7n2sv'...
warning: Could not find remote branch 1.0.1 to clone.
fatal: Remote branch 1.0.1 not found in upstream origin ) during validation.

[!] The spec did not pass validation, due to 1 error.
[!] The validator for Swift projects uses Swift 3.0 by default, if you are using a different version of swift you can use a `.swift-version` file to set the version for your Pod. For example to use Swift 2.3, run:
`echo "2.3" > .swift-version`.

原因主要在这句: fatal: Remote branch 1.0.1 not found in upstream origin 我在pod tag的时候,将版本号写成了pod tag "1.0.0" 但是我在.podspec文件里面写的是s.version = "1.0.1" 版本号不对应,所以报错。 重新打下tag值就好了。

搜不到自己的框架

当你做完了以上几步之后,看到的文章都会告诉你,OK,你已经大功告成了。可以直接使用你的框架了。但是,你还是用不了。

代码语言:javascript
复制
bogon:PodTest wanghongqing$ pod install
Analyzing dependencies
[!] Unable to find a specification for `HQSliderView`

[!] Automatically assigning platform ios with version 10.3 on target PodTest because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.

而且你连搜都搜不到

代码语言:javascript
复制
bogon:Test wanghongqing$ pod search HQSliderView
[!] Unable to find a pod with name, author, summary, or description matching `HQSliderView`

别急,这里有解决办法:

  • 进入CocoaPods目录下
代码语言:javascript
复制
~/资源库/Caches/CocoaPods
  • 删除search_index.json这个文件,这个文件是pod search 搜索时的缓存文件。
  • 再试一下,一般来讲,这样做就可以搜到了。但是我的依然没有搜到。FUCK!!!
代码语言:javascript
复制
bogon:Test wanghongqing$ pod search HQSliderView
[!] Unable to find a pod with name, author, summary, or description matching `HQSliderView`
  • 更新本地pod
代码语言:javascript
复制
pod setup

输出:

代码语言:javascript
复制
Setting up CocoaPods master repo
$ /usr/bin/git -C /Users/wanghongqing/.cocoapods/repos/master fetch origin
--progress
From https://github.com/CocoaPods/Specs
b696e05c885..817c4b4b816  master     -> origin/master
$ /usr/bin/git -C /Users/wanghongqing/.cocoapods/repos/master rev-parse
--abbrev-ref HEAD
master
$ /usr/bin/git -C /Users/wanghongqing/.cocoapods/repos/master reset --hard
origin/master
HEAD is now at 817c4b4b816 [Add] HQChannelListView 0.0.1

CocoaPods 1.3.0.beta.1 is available.
To update use: `sudo gem install cocoapods --pre`
[!] This is a test version we'd love you to try.

For more information, see https://blog.cocoapods.org and the CHANGELOG for this version at https://github.com/CocoaPods/CocoaPods/releases/tag/1.3.0.beta.1

Setup completed
  • 再搜索,还是搜不到。FUCK!FUCK!!FUCK!!!
代码语言:javascript
复制
bogon:Test wanghongqing$ pod search HQSliderView
[!] Unable to find a pod with name, author, summary, or description matching `HQSliderView`
  • 再删除search_index.json这个文件,再搜索,终于出来了。
代码语言:javascript
复制
-> HQSliderView (1.0.1)
A view for developer to creat sliderView
pod 'HQSliderView', '~> 1.0.1'
- Homepage: http://www.jianshu.com/u/1ab0fcff23e7
- Source:   https://github.com/hongqingWang/Test.git
- Versions: 1.0.1 [master repo]
  • 安装试下,成功
代码语言:javascript
复制
bogon:PodTest wanghongqing$ pod install
Analyzing dependencies
Downloading dependencies
Installing HQSliderView (1.0.1)
Using SDWebImage (4.0.0)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `HQSliderView.xcworkspace` for this project from now on.

Sending stats
Pod installation complete! There are 2 dependencies from the Podfile and 2 total pods installed.

[!] Automatically assigning platform ios with version 10.3 on target PodTest because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017.06.18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 绑定自己的 GitHub 帐号
  • 查看是否注册成功,终端中执行下列命令
  • 创建 .podspec 文件
  • 设置 podspec 文件内容
  • 验证 podspec 文件
  • 给项目打上 tag 版本并推送到 GitHub
  • 把 Podspec 文件推送到 Cocoapods 官方库
  • 如果成功
  • 如果不成功
  • 搜不到自己的框架
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档