文章预读
Podspec Syntax Reference(Podspec语法)
使用 COCOAPODS 管理自定義庫
将自己的代码上传到CocoaPods上作为第三方使用
Cocoapods相关知识整理
ios从开发Framework到提交Cocoapods
上传自己代码到Cocoapods
详细步骤
- PART1:制作Framework
动态库Framework的创建
静态库Framework的创建
iOS开发库之(四):工程和库联调
iOS之Framework模拟器和真机文件合并
注意:
一定要合并,因为你这是给别人用的,万一别人要用真机调试呢或者别人想用模拟器调试呢,所以这里一定要合并真机和模拟器下的Framework,敲黑板!!!
- PART2:将Framework提交Cocoapods
Cocoapods默认只能基于git的代码管理,以下是基于MAC安装Cocoapods,并且使用Git命令进行操作。本文是在Github和Cocoapods联合使用,当然也可以用其他Git平台或者自己搭建的Git平台。推荐码云.
Git命令使用:(这个可看可不看,以下需要什么我会提醒)
iOS开发之Git命令以及冲突解决办法
iOS开发之使用Git命令上传工程或者资源到Git平台
iOS开发之将本地项目上传到Github上
①安装Cocoapods
iOS开发之安装CocoaPods
②创建Git代码管理仓库
举例如下:
地址是:https://github.com/K********n/FrameworkCocoapodsTest.git
③将GitHub上刚创建的项目clone到本地,随便在桌面上建立一个文件夹,此处我的为Framework,然后打开终端,进入此Framework目录下,输入如下命令:
$ $ git clone https://github.com/K**********n/FrameworkCocoapodsTest.git
然后就能看到down下来的项目:
④然后进入FrameworkCocoapodsTest目录下,也就是.git文件夹的同目录上,新建文件夹,比如说FrameworkFramework,并且把你的Framework和所需要用到的Bundle资源拖入到FrameworkFramework目录下即可,如图:
⑤将你的Framework和Bundle推送到GitHub上,还是在FrameworkCocoapodsTest项目目录下也就是.git仓库的同级目录:
//先进入FrameworkCocoapodsTest目录下,因为我之前的终端是在Framework目录下的
$ cd ./FrameworkCocoapodsTest/
$ git add .
$ git commit -m "提交Framework和Bundle"
$ git push
此处如果遇到如下的问题,那么可能是网络状态不好,再Push一次:
$ git push
fatal: unable to access 'https://github.com/K*********n/FrameworkCocoapodsTest.git/': Could not resolve host: github.com
⑥.给刚刚推送的内容打上版本tag,一般以版本号作为tag名字,并且推送到Git服务器
$ git tag 1.0.1
$ git push --tags
推送成功之后显示:
$ git push --tags
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/KodieJohnson/FrameworkCocoapodsTest.git
* [new tag] 1.0.1 -> 1.0.1
⑦注册Trunk
注册邮箱和用户名:
$ pod trunk register XXX@XXX.com '名字' --verbose
或者
$ pod trunk register XXX@XXX.com 名字 --verbose
示例:
$ pod trunk register 121****77@qq.com K****e --verbose
注册成功之后你会看到如下确认邮件的提示:
[!] Please verify the session by clicking the link in the verification email that has been sent to 12******77@qq.com
那么赶紧打开你的小邮箱验证吧,打开邮箱看到如下:
然后复制中间的链接放在浏览器打开即可,出现如下提示表明验证成功:
查看注册信息:
$ pod trunk me
结果如下:
$ pod trunk me
- Name: K****e
- Email: 12******77@qq.com
- Since: January 16th, 20:49
- Pods:
- FrameworkTestTest
- Sessions:
- January 16th, 20:49 - May 25th, 21:50. IP: 123.108.109.81
- January 19th, 00:33 - May 27th, 00:34. IP: 123.108.109.81
- January 19th, 00:35 - May 27th, 00:36. IP: 123.108.109.81
- January 19th, 00:42 - May 27th, 00:43. IP: 123.108.109.81
注意:
其中Pods这一项中的FrameworkTestTest表示的是我的pods上已经有一个FrameworkTestTest项目了,如果你成功添加了现在的这个项目会再多一个的
⑧配置podspec文件
进入FrameworkCocoapodsTest项目目录下也就是.git仓库的同级目录,创建一个FrameworkTestTool.podspec的文件,最好不要直接touch一个空文件,然后直接去网上扒源码,这样会出现我最后面提到的问题,所以按照我的步骤来是不会出错的:
$ pod spec create FrameworkTestTool
结果如下:
然后用xcode打开或者Sublime Text软件打开:
其中找到以下几项替换成对应的即可:
s.version = "0.0.1"
s.summary = "A short description of FrameworkTestTool."
s.description = <<-DESC
DESC
s.homepage = "http://EXAMPLE/FrameworkTestTool"
s.license = "MIT (example)"
# s.license = { :type => "MIT", :file => "FILE_LICENSE" }
# s.platform = :ios
# s.ios.deployment_target = "5.0"
s.source = { :git => "http://EXAMPLE/FrameworkTestTool.git", :tag => "#{s.version}" }
s.source_files = "Classes", "Classes/**/*.{h,m}"
s.exclude_files = "Classes/Exclude"
# s.resource = "icon.png"
# s.resources = "Resources/*.png"
s.ios.vendored_frameworks = 'FrameworkTest/Tool/FrameworkTest.framework'
# s.frameworks = "SomeFramework", "AnotherFramework"
s.version = "1.0.1"
s.summary = "Test For FrameworkTestTool."
s.description = <<-DESC
这就是一个测试,没别的,你信不信?
DESC
s.homepage = "https://github.com/K*********n/FrameworkCocoapodsTest.git"
# s.license = "MIT (example)"
s.license = { :type => "MIT", :file => "LICENSE" }
s.platform = :ios
s.ios.deployment_target = "8.0"
s.source = { :git => "https://github.com/K********n/FrameworkCocoapodsTest.git", :tag => "#{s.version}" }
#s.source_files = "Classes", "Classes/**/*.{h,m}"
#s.exclude_files = "Classes/Exclude"
s.resource = "FrameworkFramework/FrameworkTestBundle.bundle"
# s.resources = "Resources/*.png"
s.ios.vendored_frameworks = 'FrameworkFramework/FrameworkTest.framework'
s.frameworks = 'UIKit','Foundation'
注意问题:
上面我需要使用到的:
1>s.version版本一定要跟tag一致
2>s.summary不改的话会有警告
3>s.description要填写,不填写报错,如果嫌麻烦,可以用#注释掉
4>s.homepage填写你的github上的项目地址
5>s.license根据你github创建项目的时候使用的证书,一般选用MIT,后面的文件名LICENSE是谷歌创建的时候的文件名,你可以到你的项目中查找对应的文件名填写上
6>s.source后面填写github上的项目地址,请务必正确填写
7>s.resource后面的是你上传bundle的相对路径,我是把bundle放在FrameworkFramework目录下的,请按照我的填写来找到对应你自己的填写
8> s.ios.vendored_frameworks这一栏可能没有,可能有,没有就加上,这个是找到对应framework的路劲,我是放在FrameworkFramework下的,请按照我的填写来找到对应你自己的填写
9>s.frameworks这个是系统的framework,那么我们自己的Framework里面用到了哪些系统的framework就加上
上面我没有使用到的,其他的:
1>s.dependency这个是填写依赖的第三方库,比如说AFN:
s.dependency 'AFNetworking', '~> 2.6.1'
2>s.requires_arc表示是否是ARC,如果是那么就打开填写true,如下:
s.requires_arc = true
3>s.source_files表示的是源码文件,这个是我们的开源框架了,我们这里并没有就没写,如果我们要搞开源的框架。那么路径和我们的bundle那样找路径是一样的
4>s.library或者s.libraries指的是我们需要依赖系统的库,lib库,那么这个就看我们需要添加什么就写什么。比如说我们经常喜欢添加sqite
s.library = "libiconv.tbd"
s.libraries = "libiconv.tbd", "libsqlite3.tbd"
具体还要添加啥请查看官方语法:
Podspec Syntax Reference(Podspec语法)
⑨验证podspec文件
编辑完podspec文件后,需要验证一下这个文件是否可用,在FrameworkCocoapodsTest项目目录下也就是.git仓库的同级目录也就是现在的FrameworkTestTool.podspec文件的同级目录下
$ pod spec lint
注意:这个过程是会有很多的问题在的,如有遇到问题见以下问题解决
⑩提交到Cocoapods
在FrameworkCocoapodsTest项目目录下也就是.git仓库的同级目录也就是现在的FrameworkTestTool.podspec文件的同级目录下:
$ pod trunk push ./FrameworkTestTool.podspec
注意:
这里可能会出现如下问题见以下问题解决
如果出现如下提示,说明上传成功了:
--------------------------------------------------------------------------------
🎉 Congrats
🚀 FrameworkTestTool (1.0.1) successfully published
📅 January 19th, 02:59
🌎 https://cocoapods.org/pods/FrameworkTestTool
👍 Tell your friends!
--------------------------------------------------------------------------------
然后查看下pod信息:
$ pod trunk me
结果如下:
$ pod trunk me
- Name: K****e
- Email: 12*******77@qq.com
- Since: January 16th, 20:49
- Pods:
- FrameworkTestTest
- FrameworkTestTool
- Sessions:
- January 16th, 20:49 - May 25th, 21:50. IP: 123.108.109.81
- January 19th, 00:33 - May 27th, 00:34. IP: 123.108.109.81
- January 19th, 00:35 - May 27th, 00:36. IP: 123.108.109.81
- January 19th, 00:42 - May 27th, 03:18. IP: 123.108.109.81
比之前多了一个FrameworkTestTool 🎉🎉🎉
- PART3:使用Pod:
这个可以去百度去查,这个就不多说了
pod init
这个时候你更新下本地的repo仓库:
$ pod update
更新好之后,再查询下:
$ pod search FrameworkTestTool
或者
$ pod search 'FrameworkTestTool'
查询结果如下:
-> FrameworkTestTool (1.0.1)
Test For FrameworkTestTool.
pod 'FrameworkTestTool', '~> 1.0.1'
- Homepage: https://github.com/K*********n/FrameworkCocoapodsTest.git
- Source: https://github.com/K*********n/FrameworkCocoapodsTest.git
- Versions: 1.0.1 [master repo]
修改Podfile文件,添加如下条目:
pod 'FrameworkTestTool'
或者
pod 'FrameworkTestTool', '~> 1.0.1'
上面命令第一个是默认安装最新的,下面的是安装指定版本
然后接下来打开白色的*******.xcworkspace文件,就能看到了:
问题解决
问题一
问题现象:
$ pod spec lint
-> FrameworkTest.podspec
- ERROR | spec: The specification defined in `FrameworkTest.podspec` could not be loaded.
[!] Invalid `FrameworkTest.podspec` file: undefined method ` ' for Pod:Module.
# from FrameworkTest.podspec:5
# -------------------------------------------
#
> s.name = "FrameworkTest"
# s.version = "2.3.0"
# -------------------------------------------
Analyzed 1 podspec.
[!] The spec did not pass validation, due to 1 error.
问题解决:
出现此问题的原因可能是你的文件格式有问题,我当时是直接在目录下touch的.podspec文件,然后再把对应里面的内容copy过来,后面我按照如下命令让系统自动创建一个的时候就不会出现此问题了:
//以下FrameworkTest就是你的工程文件名
pod spec create FrameworkTest
问题二
问题现象:
$ pod spec lint
-> FrameworkTest.podspec
- ERROR | spec: The specification defined in `FrameworkTest.podspec` could not be loaded.
[!] Invalid `FrameworkTest.podspec` file: syntax error, unexpected <<
<<<<<<< HEAD
^
FrameworkTest.podspec:139: syntax error, unexpected ===, expecting keyword_end
=======
^
FrameworkTest.podspec:155: syntax error, unexpected >>, expecting keyword_end.
# from FrameworkTest.podspec:10
# -------------------------------------------
# Pod::Spec.new do |s|
> <<<<<<< HEAD
#
# -------------------------------------------
Analyzed 1 podspec.
[!] The spec did not pass validation, due to 1 error.
问题解决:
这是由于git的时候冲突了,进入文件里面把<<<<<<< HEAD和=======等删掉,还有冲突的地方给删除掉,然后用Git重新push一次。
问题三
问题现象:
$ pod spec lint
-> FrameworkTest (2.3.4)
- ERROR | [iOS] unknown: Encountered an unknown error (757: unexpected token at '2018-01-17 16:58:53.118 simctl[6173:1111197] CoreSimulator detected Xcode.app relocation or CoreSimulatorService version change. Framework path (/Library/Developer/PrivateFrameworks/CoreSimulator.framework) and version (459.9) does not match existing job path (/Applications/Xcode.app/Contents/Developer/Library/PrivateFrameworks/CoreSimulator.framework/Versions/A/XPCServices/com.apple.CoreSimulator.CoreSimulatorService.xpc) and version (338.16). Attempting to remove the stale service in order to add the expected version.
') during validation.
Analyzed 1 podspec.
[!] The spec did not pass validation, due to 1 error.
- ERROR | [iOS] unknown: Encountered an unknown error (757 : ....)
翻译:
Xcode检测到应用迁移或CoreSimulatorService版本变化。试图删除过期的服务以增加预期的版本)。
问题原因:
这个是由于电脑安装了多个Xcode版本导致,本错误不用处理,继续pod trunk即可。
问题四
问题现象:
$ pod trunk push FrameworkTest.podspec
Updating spec repo `master`
[!] CocoaPods was not able to update the `master` repo. If this is an unexpected issue and persists you can inspect it running `pod repo update --verbose`
问题解决:
直接按照红色字体中描述的那样,执行以下命令,更新一下repo:
pod repo update --verbose
问题五
问题现象:
$ pod trunk push FrameworkTest.podspec
Updating spec repo `master`
CocoaPods 1.4.0.rc.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.4.0.rc.1
Validating podspec
-> FrameworkTest (2.3.4)
- NOTE | [iOS] xcodebuild: ld: warning: ignoring file FrameworkTest/FrameworkTest/Tool/FrameworkTest.framework/FrameworkTest, file was built for archive which is not the architecture being linked (i386): FrameworkTest/FrameworkTest/Tool/FrameworkTest.framework/FrameworkTest
- NOTE | [iOS] xcodebuild: ld: warning: object file (FrameworkTest/FrameworkTest/Tool/FrameworkTest.framework/FrameworkTest(AlertViewShowTool.o)) was built for newer iOS version (10.2) than being linked (8.0)
- NOTE | [iOS] xcodebuild: ld: warning: object file (FrameworkTest/FrameworkTest/Tool/FrameworkTest.framework/FrameworkTest(NSBundle+KODLocalizable.o)) was built for newer iOS version (10.2) than being linked (8.0)
- NOTE | [iOS] xcodebuild: ld: warning: object file (FrameworkTest/FrameworkTest/Tool/FrameworkTest.framework/FrameworkTest(UIView+KODCurrentView.o)) was built for newer iOS version (10.2) than being linked (8.0)
- NOTE | [iOS] xcodebuild: ld: warning: object file (FrameworkTest/FrameworkTest/Tool/FrameworkTest.framework/FrameworkTest(KODCommonConst.o)) was built for newer iOS version (10.2) than being linked (8.0)
- NOTE | [iOS] xcodebuild: ld: warning: object file (FrameworkTest/FrameworkTest/Tool/FrameworkTest.framework/FrameworkTest(FrameworkTest_vers.o)) was built for newer iOS version (10.2) than being linked (8.0)
[!] {"name"=>["is already taken"]}
[!] {"name"=>["is already taken"]}
问题解决:
这个是由于已经有人上传了同一个名字的库了,所以需要更改一下.podspec文件里面的s.name那一栏,另外把.podspec这个文件的文件名和s.name改完后的名字保持一致,其他项不变即可(版本号最好变一下,我是变了的,没变我没试,大家可以试一下),如下面,其他项保持不变:
问题六
问题现象:
$ pod spec lint
-> FrameworkTestTest (2.3.6)
- ERROR | name: The name of the spec should match the name of the file.
- NOTE | [iOS] xcodebuild: ld: warning: ignoring file FrameworkTestTest/FrameworkTest/Tool/FrameworkTest.framework/FrameworkTest, file was built for archive which is not the architecture being linked (i386): FrameworkTestTest/FrameworkTest/Tool/FrameworkTest.framework/FrameworkTest
- NOTE | [iOS] xcodebuild: ld: warning: object file (FrameworkTestTest/FrameworkTest/Tool/FrameworkTest.framework/FrameworkTest(AlertViewShowTool.o)) was built for newer iOS version (10.2) than being linked (8.0)
- NOTE | [iOS] xcodebuild: ld: warning: object file (FrameworkTestTest/FrameworkTest/Tool/FrameworkTest.framework/FrameworkTest(NSBundle+KODLocalizable.o)) was built for newer iOS version (10.2) than being linked (8.0)
- NOTE | [iOS] xcodebuild: ld: warning: object file (FrameworkTestTest/FrameworkTest/Tool/FrameworkTest.framework/FrameworkTest(UIView+KODCurrentView.o)) was built for newer iOS version (10.2) than being linked (8.0)
- NOTE | [iOS] xcodebuild: ld: warning: object file (FrameworkTestTest/FrameworkTest/Tool/FrameworkTest.framework/FrameworkTest(KODCommonConst.o)) was built for newer iOS version (10.2) than being linked (8.0)
- NOTE | [iOS] xcodebuild: ld: warning: object file (FrameworkTestTest/FrameworkTest/Tool/FrameworkTest.framework/FrameworkTest(FrameworkTest_vers.o)) was built for newer iOS version (10.2) than being linked (8.0)
Analyzed 1 podspec.
[!] The spec did not pass validation, due to 1 error.
- ERROR | name: The name of the spec should match the name of the file.
问题解决:
这个说的是名字和文件名不匹配,可能就是上一个问题只改了s.name里面的名字。并没有把文件名给改成和s.name一致。同问题5.
问题七:
问题现象:
$ pod spec lint
-> FrameworkTestTool (1.0.1)
- WARN | description: The description is shorter than the summary.
- ERROR | [iOS] unknown: Encountered an unknown error (/usr/bin/xcrun simctl list -j devices
2018-01-19 16:44:20.889 simctl[58645:2602125] CoreSimulator detected Xcode.app relocation or CoreSimulatorService version change. Framework path (/Library/Developer/PrivateFrameworks/CoreSimulator.framework) and version (459.9) does not match existing job path (/Applications/Xcode.app/Contents/Developer/Library/PrivateFrameworks/CoreSimulator.framework/Versions/A/XPCServices/com.apple.CoreSimulator.CoreSimulatorService.xpc) and version (338.16). Attempting to remove the stale service in order to add the expected version.
2018-01-19 16:44:21.519 simctl[58645:2602127] CoreSimulatorService connection interrupted. Resubscribing to notifications.
2018-01-19 16:44:24.581 simctl[58645:2602127] CoreSimulator detected Xcode.app relocation or CoreSimulatorService version change. Framework path (/Library/Developer/PrivateFrameworks/CoreSimulator.framework) and version (459.9) does not match existing job path (/Applications/Xcode.app/Contents/Developer/Library/PrivateFrameworks/CoreSimulator.framework/Versions/A/XPCServices/com.apple.CoreSimulator.CoreSimulatorService.xpc) and version (338.16). Attempting to remove the stale service in order to add the expected version.
2018-01-19 16:44:24.583 simctl[58645:2602221] CoreSimulatorService connection became invalid. Simulator services will no longer be available.
Unable to locate device set: Error Domain=NSPOSIXErrorDomain Code=53 "Software caused connection abort" UserInfo={NSLocalizedFailureReason=Failed to subscribe to notifications from CoreSimulatorService., NSLocalizedDescription=Failed to initialize simulator device set., NSUnderlyingError=0x7feb04d4f930 {Error Domain=NSPOSIXErrorDomain Code=53 "Software caused connection abort" UserInfo={NSLocalizedDescription=Error returned in reply to notification request: Connection interrupted}}}
) during validation.
Analyzed 1 podspec.
[!] The spec did not pass validation, due to 1 error and 1 warning.
问题解决:
这个问题呢,Error里面有两句是说因为Xcode的多个版本问题,最后一句说的是Software caused connection abort软件引起的连接错误,类似问题三,所以解决途径是直接忽略!!!直接进行下一步pod trunk push
问题八:
问题现象:
$ pod trunk push ./FrameworkTestTool.podspec
Updating spec repo `master`
CocoaPods 1.4.0.rc.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.4.0.rc.1
Validating podspec
-> FrameworkTestTool (1.0.1)
- WARN | description: The description is shorter than the summary.
- NOTE | [iOS] xcodebuild: ld: warning: ignoring file FrameworkTestTool/FrameworkFramework/FrameworkTest.framework/FrameworkTest, file was built for archive which is not the architecture being linked (i386): FrameworkTestTool/FrameworkFramework/FrameworkTest.framework/FrameworkTest
- NOTE | [iOS] xcodebuild: ld: warning: object file (FrameworkTestTool/FrameworkFramework/FrameworkTest.framework/FrameworkTest(AlertViewShowTool.o)) was built for newer iOS version (10.2) than being linked (8.0)
- NOTE | [iOS] xcodebuild: ld: warning: object file (FrameworkTestTool/FrameworkFramework/FrameworkTest.framework/FrameworkTest(NSBundle+KODLocalizable.o)) was built for newer iOS version (10.2) than being linked (8.0)
- NOTE | [iOS] xcodebuild: ld: warning: object file (FrameworkTestTool/FrameworkFramework/FrameworkTest.framework/FrameworkTest(UIView+KODCurrentView.o)) was built for newer iOS version (10.2) than being linked (8.0)
- NOTE | [iOS] xcodebuild: ld: warning: object file (FrameworkTestTool/FrameworkFramework/FrameworkTest.framework/FrameworkTest(KODCommonConst.o)) was built for newer iOS version (10.2) than being linked (8.0)
- NOTE | [iOS] xcodebuild: ld: warning: object file (FrameworkTestTool/FrameworkFramework/FrameworkTest.framework/FrameworkTest(FrameworkTest_vers.o)) was built for newer iOS version (10.2) than being linked (8.0)
[!] The spec did not pass validation, due to 1 warning (but you can use `--allow-warnings` to ignore it).
问题解决:
$ pod trunk push –allow-warnings ./FrameworkTestTool.podspec
还有其他的问题请看:
使用 COCOAPODS 管理自定義庫
另外私有库请看:==> 利用cocoapods创建基于git的私有库Spec Repo
特此感谢大神# LDeath的无私帮助,为大神打Call!!!
👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍
觉得好点个赞,有问题可以简书@我,转载请注明出处,以上!!!