iOS Cocoapods创建自己的Cocoapods公共库组件
注册CocoaPods账户信息
打开终端,创建一个开源pod库,需要注册CocoaPods
pod trunk register 邮箱地址 '用户名' --verbose
创建pod库如下步骤
Demo:SayHi为例
- 打开终端:cd File
cd /Users/bruceyao/Desktop/pod_public
- 命令行: pod lib create 项目名称 回车
pod lib create SayHi
会有如下信息:
What platform do you want to use?? [ iOS / macOS ]
> ios //那个平台
What language do you want to use?? [ Swift / ObjC ]
> Swift //语言
Would you like to include a demo application with your library? [ Yes / No ]
> yes //是否需要生成一个 demo 应用,这里最好 Yes 这样你可以查看你创建的库是否可以使用
Which testing frameworks will you use? [ Quick / None ]
> quick //需要用到的测试框架,随意选
Would you like to do view based testing? [ Yes / No ]
> yes //基于视图的测试 Yes/No 都可以
- 配置信息,在 demo 工程中有一个 Podspec Metadata 文件夹,查看 SayHi. podspec 配置里面的信息
注意:
s.swift_version = '5.0'// 指定版本Swift,不然会有警告
s.frameworks = 'UIKit' //项目中用到的系统框架
s.source_files = 'SayHi/Classes/**/*' //将来你要放自己代码的地方,路径一定要配置正确
s.version = '0.1.0' //版本,
#
# Be sure to run `pod lib lint SayHi.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
s.name = 'SayHi'//工程名
s.version = '0.1.0' //版本
s.summary = 'just SayHi.'//描述你的库
s.dependency 'AFNetworking', '~> 2.3' //第三方依赖
# 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/xxxx/SayHi' //切记不要带.git
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'authorName' => '[email protected]' }
s.source = { :git => 'https://github.com/xxxx/SayHi.git', :tag => s.version.to_s } //资源完整路径Git
s.swift_version = '5.0'// 指定版本Swift,不然会有警告
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '8.0'
s.source_files = 'SayHi/Classes/**/*' //将来你要放自己代码的地方
# s.resource_bundles = {
# 'SayHi' => ['SayHi/Assets/*.png']
# }
# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
# s.dependency 'AFNetworking', '~> 2.3'
end
- github上创建对应工程的项目
点击:Create repository即可
注意:不需要README.md文件啦,已经有啦
- 把我们的创建的SayHi代码放到Classes路径下
注意:通过Show in Finder 进入文件夹
所在文件目录:…/SayHi/SayHi/Classes/
把ReplaceMe.swift文件删除,不要通过Xcode直接拉进来
YSayHi.swift代码
import UIKit
open class YSayHi {
public static func sayHello() {
print("Hello world")
}
}
- 打开终端,执行如下,切记在SayHi根目录下
git add .
git commit -am 'SayHi'
//第一次提交用:
git remote add origin https://github.com/xxxx/SayHi.git
git push -u origin master
//打标签
git tag '0.1.0'
git push origin --tags
git tag //查看
- 用命令行进入 Example目录下,执行
pod install
可以看到我们工程文件:YSayHi添加到啦pod上啦
- 退出到上个目录下,验证我们的库是否正确,执行如下命令:
cd ..
pod lib lint --allow-warnings //忽略警告
看到有两个警告,上面第三部有解决方案分别是描述和swift版本问题:
- WARN | summary: The summary is not meaningful.
- 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.
- 提交我们的代码到pod公共库上执行如下:
这个命令有可能要等一段时间,也有可能会很快执行完毕
pod trunk push SayHi.podspec --allow-warnings
- 自己新建一个工程Demo,用pod管理工程
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, "9.0"
use_frameworks!
def public_pods
pod 'YYExtentions'
pod 'SayHi'
end
target 'Demo12' do
xcodeproj 'Demo12.xcodeproj'
public_pods
end
在ViewController调用结果如下
import SayHi
override func viewDidLoad() {
super.viewDidLoad()
YSayHi.sayHello()
}
打印结果如下:Hello world
11. 大功告成!