美文网首页
iOS 利用cocoapods进行本地组件化

iOS 利用cocoapods进行本地组件化

作者: Smallwolf_JS | 来源:发表于2018-05-30 09:05 被阅读11次

项目到了一定规模, 项目的代码组织和结构显得尤为重要.

重构项目结构, 可以从分离代码开始.

代码分离, 可以把常用稳定的组件封装抽离出来.

我的做法是使用 cocoapods 来管理.

下面进入今天的主题: 使用 cocoapods 管理自己的本地代码.

使用 xcode 创建一个工程, 工程名就起为TestPods.在桌面或者你喜欢的目录下面都可以.

在 TestPods 下面创建 LocalLib 目录, 用来放置分离的代码.

在 LocalLib 下面, 我的 pod 库代码名称为 download.可以新建这个目录.

我的目录如下

image

在 download 目录下面, 创建 podspec 文件, 创建命令如下

pod spec create download  

具体文件内容, 大家在创建后, 可以自行查看.

修改 download.podspec, 主要修改几个关键地方:

1. 源码位置

2. 源码版本

# ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #  
 #  
 #  Specify the location from where the source should be retrieved.  
 #  Supports git, hg, bzr, svn and HTTP.  
 #  
  
 s.source       = { :git => "", :tag => "0.0.1" }  
  
  
 # ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #  
 #  
 #  CocoaPods is smart about how it includes source code. For source files  
 #  giving a folder will include any swift, h, m, mm, c & cpp files.  
 #  For header files it will include any header in the folder.  
 #  Not including the public_header_files will make all headers public.  
 #  
  
 s.source_files  = "Source", "Source/**/*.{h,m}"  
 s.exclude_files = "Source/Exclude"  

另外, 配置好相关描述信息, 不要包含'Example'的字样, 不然, 新版的 pod 在执行 pod install 时候, 会报出警告和错误.

其他工程可以使用 pods 库了.

将 TestPods 改为 cocoapods 项目.

在 TestPods 目录, 执行

pod init 

修改 Podfile 文件

# Uncomment this line to define a global platform for your project  
# platform :ios, '7.0'  
# Uncomment this line if you're using Swift  
# use_frameworks!  
  
target 'TestPods' do  
  
pod 'download', :path => './LocalLib/download/'  
  
#pod 'core_lib_spec', :svn => 'http://svn.ids111.com/o2o/client/ios/trunks/master/Frameworks/CoreLibrary'  
  
end  
  
target 'TestPodsTests' do  
  
end  
  
target 'TestPodsUITests' do  
  
end  

关键是指明 pod 库的位置.

pod 'download', :path => './LocalLib/download/'  

在 TestPods 下面, pod install 即可.

如果, pod install 报错, 一般都是你的 pod 库的配置文件(.podspec)里面写的不符合要求.

根据报错信息, 加以修改即可.

xcode 打开工程.

image

相关文章

网友评论

      本文标题:iOS 利用cocoapods进行本地组件化

      本文链接:https://www.haomeiwen.com/subject/tfjzjftx.html