iOS组件化(一)—Cocopods创建私有库-阿里云开发者社区

开发者社区> 40KClub> 正文

iOS组件化(一)—Cocopods创建私有库

简介: iOS组件化,Cocopods创建私有库
+关注继续查看

一、创建一个文件夹

mkdir Module

二、下载工程脚手架

pod lib create AaronSwift

AaronSwift是你要创建的组件工程的名称。安装过程中会提示你输入要下载工程的配置(如下:),依次输入:iOS、Swift、Yes、Quick、Yes,其中第二步如果想创建OC库,请输入ObjC。(各版本可能有不同,请根据提示输入)

Cloning `https://github.com/CocoaPods/pod-template.git` into `AaronSwift`.

Configuring AaronSwift template.

------------------------------

To get you started we need to ask a few questions, this should only take a minute.

If this is your first time we recommend running through with the guide: 

 - https://guides.cocoapods.org/making/using-pod-lib-create.html

 ( hold cmd and double click links to open in a browser. )

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

Which testing frameworks will you use? [ Quick / None ]

 > Quick

Would you like to do view based testing? [ Yes / No ]

 > Yes

Running pod install on your new library.

Analyzing dependencies

Downloading dependencies

Installing AaronSwift (0.1.0)

Installing FBSnapshotTestCase (2.1.4)

Installing Nimble (7.3.4)

Installing Nimble-Snapshots (6.3.0)

Installing Quick (1.2.0)

Generating Pods project

Integrating client project

[!] Please close any current Xcode sessions and use `AaronSwift.xcworkspace` for this project from now on.

Pod installation complete! There are 5 dependencies from the Podfile and 5 total pods installed.

[!] Automatically assigning platform `iOS` with version `9.3` on target `AaronSwift_Example` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.

 Ace! you're ready to go!

 We will start you off by opening your project in Xcode

 open 'AaronSwift/Example/AaronSwift.xcworkspace'

To learn more about the template see `https://github.com/CocoaPods/pod-template.git`.

To learn more about creating a new pod, see `https://guides.cocoapods.org/making/making-a-cocoapod`.

三、创建Git Hub远程仓库

如果是公司的项目,需要运维同事搭建一个Git Lab仓库并创建项目。这里用Git Hub代替。在Git Hub上创建一个AaronSwift的项目。地址:https://github.com/AaronYin0514/AaronSwift

四、代码提交到远程仓库

进入工程目录

cd AaronSwift/

git init

git add .

git commit -m "first commit"

git remote add origin https://github.com/AaronYin0514/AaronSwift.git

git push -u origin master

五、编写代码

现在我们可以为私有库添加代码,位置是跟目录下的AaronSwift/Classes,我们的代码必须加到这个目录下。创建库时自动生成了ReplaceMe.swift文件,我们添加一些测试代码:

import Foundation

public struct RepleaceMeTest {

 public func test() {

 print("Test!")

 }

}

六、提交代码

通过Source Tree管理代码,commit代码变动:

AaronSwift_Git_

七、添加Tag

每一个版本我们需要添加一个Tag,如下图

AaronSwift_Git_2

八、检测配置文件是否填写正确

pod spec lint --allow-warnings

其中--allow-warnings参数代表忽略警告,如果你的代码在编译时有警告,如果不加这个参数就会报错。结果如下:

 -> AaronSwift (0.1.0)

 - 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 'AaronSwift' 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.

AaronSwift.podspec passed validation.

九、配置文件说明

刚才说的检测配置文件是否配置正确,是哪个文件呢?就是工程跟目录下的后缀为podspec的文件,在我的例子中就是AaronSwift.podspec文件。

打开这个文件,里面是工程的配置。我们在用pod命令安装库时,就是找到这个文件,获取地址下载库,并根据配置下载好依赖库和其它工程的配置。

Pod::Spec.new do |s|

 s.name  = 'AaronSwift'

 s.version = '0.1.0'

 s.summary = 'Aaron私有Swift公共组件库AaronSwift.'

# 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/AaronYin0514/AaronSwift'

 # s.screenshots  = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'

 s.license = { :type => 'MIT', :file => 'LICENSE' }

 s.author  = { 'AaronYin0514' => '562540603@qq.com' }

 s.source  = { :git => 'https://github.com/AaronYin0514/AaronSwift.git', :tag => s.version.to_s }

 # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

 s.ios.deployment_target = '8.0'

 s.source_files = 'AaronSwift/Classes/**/*'

 # s.resource_bundles = {

 #  'AaronSwift' => ['AaronSwift/Assets/*.png']

 # }

 # s.public_header_files = 'Pod/Classes/**/*.h'

 # s.frameworks = 'UIKit', 'MapKit'

 # s.dependency 'AFNetworking', '~> 2.3'

end
  • s.name :pod search 搜索的关键词,注意这里一定要和.podspec的名称一样
  • s.version :版本号,这个版本号必须与对应的Tag一致。上面例子中我们设置的为0.1.0
  • s.summary : 简介,这个简介你需要修改一下,对项目的简短介绍,不修改的话会有警告。
  • s.homepage : 项目主页地址,这个地址需要是https地址
  • s.license : 许可证
  • s.author : 作者
  • s.social_media_url : 社交网址
  • s.source : 项目的地址
  • s.source_files : 需要包含的源文件,“” 表示匹配所有文件,“*” 表示匹配所有子目录。
  • s.resources: 资源文件
  • s.requires_arc : 是否支持ARC
  • s.dependency :依赖库
  • s.ios.deployment_target = '8.0' : 支持的pod最低版本

十、发布版本

pod repo add AaronSwift https://github.com/AaronYin0514/AaronSwift.git

pod repo push AaronSwift AaronSwift.podspec --allow-warnings

执行这两步操作,操作过程如下

yinzhongzhengdeMacBook-Pro:AaronSwift yinzhongzheng$ pod repo add AaronSwift https://github.com/AaronYin0514/AaronSwift.git

Cloning spec repo `AaronSwift` from `https://github.com/AaronYin0514/AaronSwift.git`

yinzhongzhengdeMacBook-Pro:AaronSwift yinzhongzheng$ pod repo push AaronSwift AaronSwift.podspec --allow-warnings

Validating spec

 -> AaronSwift (0.1.0)

 - 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 'AaronSwift' 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 the `AaronSwift' repo

Adding the spec to the `AaronSwift' repo

 - [Update] AaronSwift (0.1.0)

Pushing the `AaronSwift' repo

十一、解释pod repo add 库名称 库地址

这一个操作是将原创仓库添加到本地,执行下面命令:

cd ~/.cocoapods/repos/

open .

就能看到Cocopods的所有本地仓库列表了,例子的库是AaronSwift。我们也可以浏览一下其它的目录,就能找到很多后缀为podspec的文件。

pod install命令就是根据要安装的库的名字在这些目录中遍历,找到对应的配置文件后,解析里面的地址和配置进行下载。

Pod update命令是从远程库,把这些配置文件下载到本地的这个目录中,再install。

十二、解释pod repo push 库名 库配置文件(后缀为podspec) --allow-warnings

这个命令就是发布版本的命令,将版本push到远程,我们在~/.cocoapods/repos/AaronSwift/AaronSwift/目录中,可以找到对应版本号的目录,目录里面就是配置文件(后缀为podspec)。

pod install时指定版本或最新版本时,就是根据版本号找到对应的配置文件的。

十三、pod udpate

进入Example,这个目录中是我们测试代码的工程,pod update后,就安装了我们发布的版本,到此,整个过程就完成了。

yinzhongzhengdeMacBook-Pro:AaronSwift yinzhongzheng$ cd Example/

yinzhongzhengdeMacBook-Pro:Example yinzhongzheng$ ls

AaronSwift  Podfile  Tests

AaronSwift.xcodeproj  Podfile.lock

AaronSwift.xcworkspace  Pods

yinzhongzhengdeMacBook-Pro:Example yinzhongzheng$ pod update

Update all pods

Updating local specs repositories

^C[!] Cancelled

yinzhongzhengdeMacBook-Pro:Example yinzhongzheng$ pod update --no-repo-update

Update all pods

Analyzing dependencies

Downloading dependencies

Installing AaronSwift 0.1.0

Generating Pods project

Integrating client project

Pod installation complete! There are 5 dependencies from the Podfile and 5 total pods installed.

[!] Automatically assigning platform `iOS` with version `9.3` on target `AaronSwift_Example` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
基于IOS系统的动态创建视图UI 解决方案 - LazyScrollView
LazyScrollView 是 iOS 的 ScrollView,用于解决视图的复用问题。
38 0
怎么设置阿里云服务器安全组?阿里云安全组规则详细解说
阿里云服务器安全组设置规则分享,阿里云服务器安全组如何放行端口设置教程
8394 0
技能学习:学习使用Node.js + Vue.js,开发前端全栈网站-4.使用axios,并创建接口上传数据到mongodb数据库
根据我的业务范围,设想网站内容分为三类,设计、开发、小程序,设计中有二级分类平面设计、UI设计,平面设计又有三级分类包括VI设计、名片设计、LOGO设计…之后网站发布的不管是文章还是合作订单,全部归属到这些类别中,这样就需要用到数据的关联。
68 0
iOS组件化(一)—Cocopods创建私有库
iOS组件化,Cocopods创建私有库
928 0
图灵ios培训第一周(使用UIWebView创建简易浏览器)
       借着给学弟学妹们培训ios的机会,给大家ios新手带来一系列的新手知识博客,希望能给广大ios爱好者以及希望进入ios领域的童鞋们得到帮助。大神路过的请指点。 课前小探索 制作一个简易的浏览器 首先打开Xcode,新建一个工程。
745 0
+关注
1
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载