本篇文章介绍如何使自己的GitHub支持Cocoapods,然后可以在项目中使用Cocoapods引用该框架。
(1)在GitHub中创建一个空的仓库。
(2)在本地创建一个与仓库名称相同的iOS项目。
(3)在项目目录中创建MIT许可文件, 文件内容如下:
MIT License
Copyright (c) 2019 iOSLibrary
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
(4)在项目目录中创建.gitignore文件,文件内容如下:
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
## User settings
xcuserdata/
## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9)
*.xcscmblueprint
*.xccheckout
## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4)
build/
DerivedData/
*.moved-aside
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
## Obj-C/Swift specific
*.hmap
## App packaging
*.ipa
*.dSYM.zip
*.dSYM
## Playgrounds
timeline.xctimeline
playground.xcworkspace
# Swift Package Manager
#
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
# Package.pins
# Package.resolved
# *.xcodeproj
#
# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata
# hence it is not needed unless you have added a package configuration file to your project
# .swiftpm
.build/
# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
# Pods/
#
# Add this line if you want to avoid checking in source code from the Xcode workspace
# *.xcworkspace
# Carthage
#
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts
Carthage/Build/
# Accio dependency management
Dependencies/
.accio/
# fastlane
#
# It is recommended to not store the screenshots in the git repo.
# Instead, use fastlane to re-generate the screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/#source-control
fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots/**/*.png
fastlane/test_output
# Code Injection
#
# After new code Injection tools there's a generated folder /iOSInjectionProject
# https://github.com/johnno1962/injectionforxcode
iOSInjectionProject/
.DS_Store
(5)打开终端并切换到项目目录,执行下面的命令将该代码上传到GitHub:
echo "# Test" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:iOSLibrary/Test.git
git push -u origin master
(6)创建podspec
$ pod spec create Test
Specification created at Test.podspec
(7)使用文本编辑器打开Test.podspec, 修改下面的内容:
spec.name = "Test" // 名称, 不要修改,该字符串必须与项目名称一样
spec.version = "0.0.1" // 版本号,与GitHub中的release版本号一致
spec.summary = "A short description of Test." // 摘要
spec.description = 'Test' // 描述,必须添加
spec.homepage = "https://github.com/iOSLibrary/QTPictureBrower_Swift.git" // 主页地址,也可以使用自己网站的地址
spec.license = { :type => "MIT", :file => "LICENSE" } // 许可证
spec.author = { "author" => "238495349@qq.com" } // 作者
spec.platform = :ios, "9.0" // 平台
spec.source = { :git => "https://github.com/iOSLibrary/Test.git", :tag => "#{spec.version}" } // 仓库地址
spec.source_files = "Test", "Test/**/*.{h,m,swift}" // 源代码文件位置
spec.dependency "JSONKit", "~> 1.4" // 添加依赖框架,如果需要才添加
(8)验证podspec文件
$ $ pod spec lint
-> Test (0.0.2)
- WARN | source: Git SSH URLs will NOT work for people behind firewalls configured to only allow HTTP, therefore HTTPS is preferred.
- WARN | summary: The summary is not meaningful.
- WARN | description: The description is shorter than the summary.
- WARN | url: There was a problem validating the URL git@github.com:iOSLibrary/Test.git.
- WARN | [iOS] swift: The validator used Swift `4.0` by default because no Swift version was specified. To specify a Swift version during validation, add the `swift_versions` attribute in your podspec. Note that usage of a `.swift-version` file is now deprecated.
- NOTE | xcodebuild: note: Using new build system
- NOTE | [iOS] xcodebuild: note: Planning build
- NOTE | [iOS] xcodebuild: note: Constructing build description
- NOTE | [iOS] xcodebuild: warning: Skipping code signing because the target does not have an Info.plist file and one is not being generated automatically. (in target 'App' from project 'App')
- NOTE | [iOS] xcodebuild: note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'QTPictureBrower_Swift' from project 'Pods')
- NOTE | [iOS] xcodebuild: note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'Pods-App' from project 'Pods')
- NOTE | [iOS] xcodebuild: note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'App' from project 'App')
Analyzed 1 podspec.
[!] The spec did not pass validation, due to 5 warnings (but you can use `--allow-warnings` to ignore them).
或忽略警告验证
$ pod spec lint --allow-warnings
-> Test (0.0.1)
- WARN | source: Git SSH URLs will NOT work for people behind firewalls configured to only allow HTTP, therefore HTTPS is preferred.
- WARN | summary: The summary is not meaningful.
- WARN | description: The description is shorter than the summary.
- WARN | url: There was a problem validating the URL git@github.com:iOSLibrary/Test.git.
- WARN | [iOS] swift: The validator used Swift `4.0` by default because no Swift version was specified. To specify a Swift version during validation, add the `swift_versions` attribute in your podspec. Note that usage of a `.swift-version` file is now deprecated.
- NOTE | xcodebuild: note: Using new build system
- NOTE | [iOS] xcodebuild: note: Planning build
- NOTE | [iOS] xcodebuild: note: Constructing build description
- NOTE | [iOS] xcodebuild: warning: Skipping code signing because the target does not have an Info.plist file and one is not being generated automatically. (in target 'App' from project 'App')
- NOTE | [iOS] xcodebuild: note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'QTPictureBrower_Swift' from project 'Pods')
- NOTE | [iOS] xcodebuild: note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'Pods-App' from project 'Pods')
- NOTE | [iOS] xcodebuild: note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'App' from project 'App')
Analyzed 1 podspec.
Test.podspec passed validation.
(9)执行注册Cocoapods账号命令,执行完成后你注册的邮箱会受到一封邮件,打开邮件后点击里面的链接即可注册成功。
$ pod trunk register 邮箱地址 ‘用户名’ —description='描述信息'
(10)将修改后的代码提交到GitHub
(11)在GitHub中创建0.0.1的release版本
(12)执行下面的命令上传代码到Cocoapods
$ pod trunk push Test.podspec --allow-warnings
Updating spec repo `trunk`
CocoaPods 1.9.0.beta.2 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.9.0.beta.2
Validating podspec
-> Test (0.0.1)
- WARN | summary: The summary is not meaningful.
- WARN | description: The description is shorter than the summary.
- WARN | [iOS] swift: The validator used Swift `4.0` by default because no Swift version was specified. To specify a Swift version during validation, add the `swift_versions` attribute in your podspec. Note that usage of a `.swift-version` file is now deprecated.
- NOTE | xcodebuild: note: Using new build system
- NOTE | [iOS] xcodebuild: note: Planning build
- NOTE | [iOS] xcodebuild: note: Constructing build description
- NOTE | [iOS] xcodebuild: warning: Skipping code signing because the target does not have an Info.plist file and one is not being generated automatically. (in target 'App' from project 'App')
- NOTE | [iOS] xcodebuild: note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'QTPictureBrower_Swift' from project 'Pods')
- NOTE | [iOS] xcodebuild: note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'Pods-App' from project 'Pods')
- NOTE | [iOS] xcodebuild: note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'App' from project 'App')
Updating spec repo `trunk`
CocoaPods 1.9.0.beta.2 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.9.0.beta.2
--------------------------------------------------------------------------------
🎉 Congrats
🚀 Test (0.0.1) successfully published
📅 December 28th, 11:42
🌎 https://cocoapods.org/pods/Test
👍 Tell your friends!
--------------------------------------------------------------------------------
(13)检验上传是否成功,如果在Pods列中存在刚才上传的框架即表示上传成功
$ pod trunk me
- Name: author
- Email: 238495349@qq.com
- Since: December 28th, 04:24
- Pods:
- Test
- Sessions:
- December 28th, 04:24 - May 4th, 2020 22:06. IP: 113.87.129.196
Description: regist trunk
(14)一切准备就绪,现在可以在项目中的Podfile文件中添加刚才的Cocoapods框架
pod 'Test'
(15)执行下面的命令,更新Cocoapods
$ pod update
(16)现在你可以在自己的项目中使用框架里面的公开类和方法了。
注意:如果使用Swift进行开发的话,需要在公开的方法和类中使用public修饰符对类和方法的外进行开放。
如果该文章对你有用,麻烦给我个赞, 谢谢!