iOS---如何在CocoaPods上发布自己的项目

时间:2023-01-17 22:14:50

目前, 在iOS开发中, CocoaPods应该算是使用最为频繁的包管理工具了.

使用CocoaPods管理iOS项目中的第三方类库

CocoaPods的安装如下:

sudo gem install cocoapods # 会被墙掉。
gem sources --remove https://rubygems.org/
gem sources -a https://ruby.taobao.org/
gem sources -l
sudo gem install cocoapods

在项目所在目录下执行* pod init * , 生成Podfile文件, 我们需要对其进行相应的配置, Podfile文件如下:

# Uncomment this line to define a global platform for your project
platform :ios, '8.0'

# Uncomment this line if you're using Swift
use_frameworks!

pod 'CSSwiftExtension'

pod 'Reachability'
pod 'Alamofire'
pod 'SwiftyJSON'

target 'myProject' do

end

target 'myProjectTests' do

end

然后, 执行* pod install pod update * ,
即可安装这些第三方类库. 然后在项目中导入即可使用:

import Alamofire

// xxx

Alamofire.request(.GET, url, parameters: nil).validate()
.responseJSON { response in
// xxx
}

怎么样, 非常方便吧?
看到这里, 是不是也想将自己的一些代码在CocoaPods上发布呢!
接着往下看!

Swift中常见的extension方法

这里, 将以CSSwiftExtension为例, 简单介绍下如何在CocoaPods上发布自己的代码.
CSSwiftExtension是一个Swift的extension集合, 包含了一些常见的方法:
如Foundation的一些基本extension方法:

extension String {
public func cs_trim() -> String
public func cs_intValue() -> Int?
public func cs_stringValue() -> String?
}

extension NSData {
public func cs_jsonObjectFromNSData() -> AnyObject
}

extension NSFileManager {
public func cs_homeDirectory() -> String
public func cs_tempDirectory() -> String
public func cs_documentsDirectory() -> String
public func cs_libraryDirectory() -> String
public func cs_cacheDirectory() -> String
}

以及, UIImage的缩放cs_imageScaledToSize, 以及不会引起离屏渲染的图片圆角cs_imageWithCornerRadius.

extension UIImage {
public func cs_saveImageToFile(filePath: String, compressionFactor: CGFloat) -> Bool
public func cs_imageScaledToSize(size: CGSize, withOriginalRatio: Bool) -> UIImage
public func cs_imageRotatedByDegrees(degrees: CGFloat) -> UIImage
public func cs_imageWithCornerRadius(cornerRadius: CGFloat) -> UIImage
}

UIDevice的一些常见方法, 以及UIApplication的cs_currentViewController可用于获取当前的ViewController,
cs_isRunningAppVersionReleased方法接收当前App在AppStore中的id, 即可查询当前版本是否已经release.

extension UIDevice {
public func cs_screenSize() -> CGSize
public func cs_isIPhone4s() -> Bool
public func cs_isIPhone5() -> Bool
public func cs_isIPhone6() -> Bool
public func cs_isIPhone6Plus() -> Bool
public func cs_isIPhone6PlusBigMode() -> Bool
public func cs_isIPadAir2() -> Bool
public func cs_isIPadPro() -> Bool
}

extension UIDevice {
public func cs_currentLanguage() -> String
public func cs_isCurrentLanguage_en() -> Bool
public func cs_isCurrentLanguage_zh_Hans() -> Bool
public func cs_isCurrentLanguage_zh_Hant() -> Bool
public func cs_isCurrentLanguage_ja() -> Bool
public func cs_isCurrentLanguage_ko() -> Bool
}

extension UIApplication {
public func cs_appDelegate() -> UIApplicationDelegate
public func cs_currentViewController() -> UIViewController
}

extension UIApplication {
public func cs_appVersion() -> String
public func cs_appVersionInAppStore(appId: String) -> String
public func cs_isRunningAppVersionReleased(appId: String) -> Bool
}

extension UIApplication {
public func cs_snapShot(inView: UIView) -> UIImage
}

另外, 其中的CSNetworkManager可用于同步获取网络请求的JSON数据.

public class CSNetworkManager : NSObject {
public class func sharedInstance() -> CSSwiftExtension.CSNetworkManager
public class func cs_GET(urlString: String, completionHandler: (jsonObject: AnyObject) -> Void)
public class func cs_getJSONObjectSynchronously(urlString: String, completionHandler: (jsonObject: AnyObject) -> Void) -> AnyObject
}

下面, 介绍CSSwiftExtension的发布过程.

创建并正确配置项目的podspec文件

pod spec create CSSwiftExtension

生成的CSSwiftExtension.podspec文件如下:

Pod::Spec.new do |s|
s.name = "CSSwiftExtension"
s.version = "0.0.1"
s.summary = "Some useful extension for Swift."
s.description = <<-DESC
Some useful extension for Swift.
Including Foundation, UIKit and CSNetworkManager.
DESC
s.homepage = "https://github.com/icetime17/CSSwiftExtension"
s.license = { :type => "MIT", :file => "LICENSE.md" }
s.author = { "Chris Hu" => "xxxxx@126.com" }
s.platform = :ios, "8.0"
s.source = { :git => "https://github.com/icetime17/CSSwiftExtension.git", :tag => "#{s.version}" }
s.source_files = "CSSwiftExtension/**/*"
s.requires_arc = true
end

其中, s.source_files指定源文件的路径.

然后, 执行* pod lib lint * 可以验证podspec的正确性.
验证通过后, 可以将podspec文件也添加到git管理中.

git add CSSwiftExtension.podspec
git commit -m'add podspec file'
git push

git tag -a 0.0.1 -m "Tag 0.0.1"
git push --tags

使用pod trunk命令上传项目至Cocoapods

pod trunk命令是CocoaPods提供的用来管理个人提交的命令:
命令帮助可以参考getting-setup-with-trunk.
需要先注册:

pod trunk register xxxxxx@gmail.com 'Chris Hu' --description='Chris Hu'

然后在邮箱中点击确认之后, 就可以执行下边的命令, 将代码push到CocoaPods官方了.

pod trunk push CSSwiftExtension.podspec

打印的log如下:

☁  CSSwiftExtension [develop] pod trunk push CSSwiftExtension.podspec
Updating spec repo `master`
Validating podspec
-> CSSwiftExtension (0.0.1)

Updating spec repo `master`
- Data URL: https://raw.githubusercontent.com/CocoaPods/Specs/32d8166bff5e30508e565d1c76db980ab5de987f/Specs/CSSwiftExtension/0.1/CSSwiftExtension.podspec.json
- Log messages:
- June 25th, 13:51: Push for `CSSwiftExtension 0.0.1' initiated.
- June 25th, 13:51: Push for `CSSwiftExtension 0.0.1' has been pushed (0.282206408 s).

成功之后即可通过* pod search CSSwiftExtension * 来搜索:

-> CSSwiftExtension (0.0.1)
Some useful extension for Swift.
pod 'CSSwiftExtension', '~> 0.0.1'
- Homepage: https://github.com/icetime17/CSSwiftExtension
- Source: https://github.com/icetime17/CSSwiftExtension.git
- Versions: 0.0.1 [master repo]

另外, 可使用* pod trunk delete CSSwiftExtension 0.0.1 * 来删除该提交.
* pod trunk me * 可用来查看相应的信息.
我们在CocoaPods的目录中, 可以看到相关信息了.

☁  CSSwiftExtension [master] pwd
/Users/chris/.cocoapods/repos/master/Specs/CSSwiftExtension
☁ CSSwiftExtension [master] ls
0.0.1 0.0.2 0.0.3
☁ CSSwiftExtension [master]

CocoaPods官方的类库都是在master目录下.
如果, 我们需要添加私人的pod库, 在Podfile中加入

source 'http://xxxxx.com/iosmodules/specs.git' ***

然后, 执行* pod install 或者 pod udpate 的时候, 会将该source中所有pod库的spec信息都拉取到本地, 在/Users/chris/.cocoapods/repos/目录下单独建立一个新的目录用于存储这些spec, 然后我们使用 pod search * 即可查询到这些类库.

Demo

做到了这些, 就能在CocoaPods上使用自己的代码库了.
Demo请参考
CSSwiftExtension.
如果觉得在Swift代码中用得上, 请在github上给个star吧.