制作自己的CocoaPods框架

项目要求:最终可以让别人使用”pod yourProject”来使用你的框架,示例:’pod InviteCodeSDK’

1.CocoaPods版本要足够新

1
2
3
4
5
更新:
sudo gem install cocoapods --pre -n /usr/local/bin cocoa pods
降级:
sudo gem uninstall cocoapods
sudo gem install cocoapods -v 0.39.0 -n /usr/local/bin

2.执行pod lib create InviteCodeSDK命令,完成项目的搭建,回答五个问题,以桌面创建InviteCodeSDK为例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//询问项目使用语言
What language do you want to use?? [ Swift / ObjC ]
> Objc
//询问是否需要demo,可以写给使用者演示用法
Would you like to include a demo application with your library? [ Yes / No ]
> Yes
//询问使用的framework
Which testing frameworks will you use? [ Specta / Kiwi / None ]
> None
//询问是否要可视化视图
Would you like to do view based testing? [ Yes / No ]
> No
//项目的前缀名字
What is your class prefix?
> TSZ

3.回答完问题后项目会自动打开,结构如下图

更改配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Pod::Spec.new do |s|
s.name = 'InviteCodeSDK'
s.version = '0.1.0'
s.summary = 'A short description of InviteCodeSDK.'
# 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/tutu279737146/InviteCodeSDK'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = 'MIT'
s.author = { 'tushizhan' => '279737146@qq.com' }
s.source = { :git => '/Users/tushizhan/Desktop/InviteCodeSDK', :tag => '0.1.0' }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '8.0'
s.source_files = 'InviteCodeSDK/Classes/**/*.{h,m}'
# s.resource_bundles = {
# 'InviteCodeSDK' => ['InviteCodeSDK/Assets/*.png']
# }
s.public_header_files = 'InviteCodeSDK/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
# s.dependency 'AFNetworking', '~> 2.3'
end

4.进入Example文件夹,执行pod install --no-repo-update,更新demo项目配置,安装项目依赖库

1
2
3
4
5
6
7
8
9
Example git:(master) ✗ pod install --no-repo-update
Analyzing dependencies
Fetching podspec for `InviteCodeSDK` from `../`
Downloading dependencies
Installing InviteCodeSDK 0.1.0 (was 0.1.0)
Generating Pods project
Integrating client project
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

5.添加代码.示例中是将封装好的.h.m添加到Classes目录下(即项目结构中ReplaceMe.m位置),添加完成后执行pod install.注意:只要新加了类/资源文件/所依赖的第三方库都要重新运行pod install来更新

6.提交本地代码

1
2
3
4
5
6
git remote add origin https://github.com/tutu279737146/InviteCodeSDK.git //连接远程仓库并建了一个名叫:origin的别名
git push -u origin master //将本地仓库的东西提交到地址是origin的地址,master分支下###发布SDK到CocoaPods
git add . //添加
git commit -a -m 'v0.1.0'//添加描述
git tag -a 0.1.0 -m 'v0.1.0'//打包tag
git push

7.验证打包类库是否符合pod要求pod lib lint InviteCodeSDK.podspec --allow-warnings

1
2
3
4
5
6
7
InviteCodeSDK git:(master) ✗ pod lib lint InviteCodeSDK.podspec --allow-warnings
-> InviteCodeSDK (0.1.0)
- WARN | summary: The summary is not meaningful.
- WARN | xcodebuild: /Users/tushizhan/Desktop/InviteCodeSDK/InviteCodeSDK/Classes/QXInviteCode.h:43:13: warning: parameter 'uid' not found in the function declaration [-Wdocumentation]
InviteCodeSDK passed validation.

8.打包类库,手动麻烦,通过插件打包,安装插件sudo gem install cocoapods-packager,安装完成后,执行pod package InviteCodeSDK.podspec --library --force打包;如果命令后面加条尾巴 –library 则表示打包成 .a 文件,如果不带,则会打包成 .framework 文件。而 –force 则表示强制覆盖之前存在的文件。

9.将SDK上传到CocoaPods,注册账号pod trunk register youremail@qq.com 'yourname' -- description= 'your description',执行完成后到你的邮箱去认证注册,验证成功后pod trunk me查看是否注册成功

1
2
3
4
5
6
7
8
9
10
11
12
13
➜ ~ git:(master) ✗ pod trunk me
- Name: tushizhan
- Email: 279737146@qq.com
- Since: May 11th, 20:42
- Pods:
- tttt
- InviteCodeSDK
- Invite_Code_SDK
- Sessions:
- May 11th, 20:42 - September 19th, 22:36.
IP: 36.xxx.xx8.59 Description: My own
computer

接下来可以上传SDK,执行
pod trunk push InviteCodeSDK.podspec --verbose --allow-warnings,在这里遇到一个问题是上传完成但是新建了项目,添加’pod InviteCodeSDK’到podfile执行pod install无法找到,所以这里需要在执行上传命令之前需要先执行pod setup,来更新本地pod依赖才可以使用.

这个人很帅<br>他什么都不想说<br>