随着组件化的日益兴起,通过
CocoaPods
管理一些自己的库是基本的要求,本文将介绍如何将自己的项目支持通过CocoaPods
管理以及自己在实践过程中遇到的一些问题!
请将下面的内容替换为自己的
pod trunk register 272338444@qq.com 'password' --verbose
由于是第一次注册,下面给我返回了一堆东西
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
大概意思就是需要去注册的邮箱接收到的邮件,点击链接验证一下。大家应该都懂的,邮件是酱婶的:
pod trunk me
注册成功返回
- 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
pod spec create HQSliderView
Specification created at HQSliderView.podspec
这里特别说明一下,之前我自己照着网上的各种版本的教程弄的时候也是反反复复很多次才弄个差不多,其实这里仔细看下,每个说明都会有非常详细的英文说明,只是自己看到英文就很烦,不愿意去仔细翻译。
如果真的没有意愿自己翻译的话,那么尽量不要在网上找各种教程,因为教程很可能都过期了,包括我自己现在写的。
所以个人建议,直接到Github
上面看一看优秀的开元框架怎么配置的,这里面我就参考了Alamofire的Alamofire.podspec
文件,因为他们的永远是最新的。
下面是Alamofire.podspec
文件内的内容(2017年06月18日)
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
文件内容
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
看就好了。
配置完以后就可以进行验证了。
pod lib lint
-> HQSliderView (1.0.1)
HQSliderView passed validation.
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
平台,改正之后就好了。
s.ios.deployment_target = "9.0"
如果还有其它问题,按照提示,耐心点改都可以的,基本除了.podspec
文件有问题之外,剩下的就是你自己项目文件的问题了,之前我在自己文件的.h
和.m
里面定义了一些项目中用的常量,结果验证的时候就报错了。如下所示:
- 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'
说我项目中用到了没有定义的kScreenWidth
、HQTextColor
、HQColor
、HQLineColor
等这种问题。因为这些是我在项目里统一配置的,所以单个文件拎出来的时候,肯定会报错。自己在这两个文件里再定义一下就好了,或者有其它更好的办法也可以。
tag
之前,确保你已经把验证成功的.podspec
文件和.swift-version(如果是 swift 项目要有这个)
以及资源文件等,commit
并push
到GitHub
CocoaPods
是有版本管理的,比如你搜索SDWebImage
框架的时候,结果应该有这句:
pod 'SDWebImage', '~> 4.0.0'
git tag "1.0.1"
git tag
+ 版本号
,不是pod tag
+ 版本号
.podspec
文件里的s.version = "1.0.1"
对应了,不然虽然暂时不会报错,但是push
到GitHub
的时候也会报错。tag
推送到远程仓库git push --tags
然后会弹出这个框,除了允许
你还能选什么呢?
然后会再弹出一个框,让我们输入管理员密码
接下来终端输出:
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/hongqingWang/Test.git
* [new tag] 1.0.1 -> 1.0.1
HQSliderView.podspec
换成你自己的xxx.podspec
pod trunk push HQSliderView.podspec
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)
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!
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,你已经大功告成了。可以直接使用你的框架了。但是,你还是用不了。
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`.
而且你连搜都搜不到
bogon:Test wanghongqing$ pod search HQSliderView
[!] Unable to find a pod with name, author, summary, or description matching `HQSliderView`
别急,这里有解决办法:
CocoaPods
目录下~/资源库/Caches/CocoaPods
search_index.json
这个文件,这个文件是pod search
搜索时的缓存文件。bogon:Test wanghongqing$ pod search HQSliderView
[!] Unable to find a pod with name, author, summary, or description matching `HQSliderView`
pod
库pod setup
输出:
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
bogon:Test wanghongqing$ pod search HQSliderView
[!] Unable to find a pod with name, author, summary, or description matching `HQSliderView`
search_index.json
这个文件,再搜索,终于出来了。-> 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]
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`.