0%

开源项目支持CocoaPods

开源项目支持CocoaPods

CocoaPods可以方便的管理你使用的第三方框架,这里讲解下怎么让你的项目支持CocoaPods。

这里使用ZCWDropDownMenu这个项目做例子,从头说明

Github上创建项目

首先要在Github上创建项目ZCWDropDownMenu,然后把自己的项目push上去。具体步骤这里不做详细介绍。

本地操作

首先在终端下进到项目目录

cd ZCWDropDownMenu/

创建podspec文件

pod spec create ZCWDropDownMenu

创建完成后会创建一个通用的podspec文件,我们只需要去修改下其中的内容就可以。

编辑修改podspec文件

vim ZCWDropDownMenu.podspec

我们可以根据里面的注释去修改。

下面是自动创建的模板,包括了所以详细信息,后面会弄一个简单版本。(可以参考下其他优秀开源框架的中的podspec文件)

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#
# Be sure to run `pod spec lint ZCWDropDownMenu.podspec' to ensure this is a
# valid spec and to remove all comments including this before submitting the spec.
#
# To learn more about Podspec attributes see http://docs.cocoapods.org/specification.html
# To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/
#

Pod::Spec.new do |s|

# ――― Spec Metadata ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# These will help people to find your library, and whilst it
# can feel like a chore to fill in it's definitely to your advantage. The
# summary should be tweet-length, and the description more in depth.
#
# 项目名
s.name = "ZCWDropDownMenu"
# 项目版本
s.version = "0.0.3"
# 项目简单描述
s.summary = "Drop-down Menu"

# 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 = 'Drop-down Menu 下拉菜单'

s.homepage = "https://github.com/zcwlwen/ZCWDropDownMenu"
# s.screenshots = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif"


# ――― Spec License ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# Licensing your code is important. See http://choosealicense.com for more info.
# CocoaPods will detect a license file if there is a named LICENSE*
# Popular ones are 'MIT', 'BSD' and 'Apache License, Version 2.0'.
#

s.license = "MIT"
# s.license = { :type => "MIT", :file => "FILE_LICENSE" }


# ――― Author Metadata ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# Specify the authors of the library, with email addresses. Email addresses
# of the authors are extracted from the SCM log. E.g. $ git log. CocoaPods also
# accepts just a name if you'd rather not provide an email address.
#
# Specify a social_media_url where others can refer to, for example a twitter
# profile URL.
#

s.author = { "zcw" => "zcw_lwen@sina.cn" }
# Or just: s.author = "zcw"
# s.authors = { "zcw" => "zcw_lwen@sina.cn" }
# s.social_media_url = "http://twitter.com/zcw"

# ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# If this Pod runs only on iOS or OS X, then specify the platform and
# the deployment target. You can optionally include the target after the platform.
#

s.platform = :ios
# s.platform = :ios, "5.0"

# When using multiple platforms
# s.ios.deployment_target = "5.0"
# s.osx.deployment_target = "10.7"
# s.watchos.deployment_target = "2.0"
# s.tvos.deployment_target = "9.0"


# ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# Specify the location from where the source should be retrieved.
# Supports git, hg, bzr, svn and HTTP.
#

s.source = { :git => "https://github.com/zcwlwen/ZCWDropDownMenu.git", :tag => "0.0.3" }


# ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# CocoaPods is smart about how it includes source code. For source files
# giving a folder will include any swift, h, m, mm, c & cpp files.
# For header files it will include any header in the folder.
# Not including the public_header_files will make all headers public.
#

s.source_files = "ZCWDropDownMenu/ZCWDropDownMenu/*"
# s.exclude_files = "Classes/Exclude"

# s.public_header_files = "Classes/**/*.h"


# ――― Resources ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# A list of resources included with the Pod. These are copied into the
# target bundle with a build phase script. Anything else will be cleaned.
# You can preserve files from being cleaned, please don't preserve
# non-essential files like tests, examples and documentation.
#

# s.resource = "icon.png"
# s.resources = "Resources/*.png"

# s.preserve_paths = "FilesToSave", "MoreFilesToSave"


# ――― Project Linking ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# Link your library with frameworks, or libraries. Libraries do not include
# the lib prefix of their name.
#

# s.framework = "SomeFramework"
# s.frameworks = "SomeFramework", "AnotherFramework"

# s.library = "iconv"
# s.libraries = "iconv", "xml2"


# ――― Project Settings ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# If your library depends on compiler flags you can set them in the xcconfig hash
# where they will only apply to your library. If you depend on other Podspecs
# you can include multiple dependencies to ensure it works.

# s.requires_arc = true

# s.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }
# s.dependency "JSONKit", "~> 1.4"

end

简单版本:

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

Pod::Spec.new do |s|

s.name = "ZCWDropDownMenu"
s.version = "0.0.3"
s.summary = "Drop-down Menu"

s.description = 'Drop-down Menu 下拉菜单'

s.homepage = "https://github.com/zcwlwen/ZCWDropDownMenu"

s.license = "MIT"

s.author = { "zcw" => "zcw_lwen@sina.cn" }

s.platform = :ios
s.platform = :ios, "8.0"

s.source = { :git => "https://github.com/zcwlwen/ZCWDropDownMenu.git", :tag => "0.0.3" }

s.source_files = "ZCWDropDownMenu/ZCWDropDownMenu/*"

s.requires_arc = true

end

创建tag并且推送到github

在项目目录下依次执行:

1
2
3
4
5
git add .
git commit -m "你要添加的注释内容"
git tag 0.0.3
git push --tags
git push origin master

验证podspec文件

执行:

pod spec lint ZCWDropDownMenu.podspec

然后验证结果通过情况如图:

验证结果不通过的情况:(错误情况会有很多种,这里可以根据错误信息去修改对应的错误,我这里是因为格式错误,把podspec文件末尾的end误删除导致加载失败)。错误信息比较详细。根据错误提示去修改对应的错误直到通过即可。

这里要注意的是如果测试不通过就要重新去执行添加tag这一步操作。在执行添加tag操作前,我们要先把刚刚创建的本地的tag和远程仓库的tag删掉:

1
2
git tag -d 0.0.3
git push origin :refs/tags/0.0.3

然后在执行添加tag命令。

这里还有一个注意的点就是在podspec文件中的s.version一定要在tags中的某一个。比如如果项目的tag只有0.0.3和0.0.4,你在s.version后面跟0.0.5是不行的。这里的版本要和项目的tag对应。

验证通过后就可以提交到CocoaPods

注意如果是第一次提交需要先注册,要执行下面的命令:

1
pod trunk register 这里写邮箱 '用户名' --description=' 描述'

然后你的邮箱会收到一封邮件。点击邮件中的链接完成认证。之后就可以提交。

如果已经提交过就可以忽略这一步直接提交。

提交命令:

pod trunk push ZCWDropDownMenu.podspec

提交也会出现错误情况:根据错误信息去修改提交内容

这里是我一个提交错误的情况与解决办法:

根据错误提示发现是因为之前提交过0.0.3版本所以不能重复提交了。所以这个时候要提交不同的版本。

首先在添加一个tag:

1
2
3
git tag 0.0.4
git push --tags
git push origin master

然后修改podspec文件:中的s.versions.source。然后在执行验证步骤与提交步骤。

提交成功如图显示:

这时候就可以通过pod去集成到项目中去。

问题描述与解决

这里还有一个问题就是集成到项目中遇到:

1
ZCWDropDownMenu.m:44:1: Cannot synthesize weak property because the current deployment target does not support weak references

是因为项目不支持ARC的原因。根本原因是我在填写podspec文件的时候漏掉了一句话s.requires_arc = true

这个问题在我们集成比较老的开源项目的时候可能也会遇到,但是目前的新项目解决办法就是要加上这句话后,重新提交一个版本。

这也不是唯一的解决办法,在stackoverflow中找到了
https://stackoverflow.com/questions/37160688/set-deployment-target-for-cocoapodss-pod

在podfile中加上下面这些话:

1
2
3
4
5
6
7
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '9.2'
end
end
end

然后在执行下pod install这样也是可以使用。

但是如果是我们自己的新的项目,还是要从根本上解决解决问题把s.requires_arc = true加上然后发布一个更新版本。