1. Create a private library index
Step 1 is only needed when there is no index library or when adding a new index (create a basic component library).
First, create a private library index on the code cloud and name itSYComponentSpec
2. Add private library index locally
Add private library index
pod repo add SYComponentSpec https://gitee.com/sun-shiyu/sycomponent-spec
3. Create a basic component library
When you need to create a new component, start here. If you want to modify the original library, you can modify it directly in the previous component warehouse.
1. Create a component library on the code cloud
and name it SYBasicComponents
, as shown in the figure:
2. Create SYBasicComponents
a local library.
Default creation path: /Users/sunshiyu/SYBasicComponents
.
pod lib create SYBasicComponents
Finally, the project will be automatically opened after the project local component library is created.
3. Import your own code in the private library
Delete the files Classes
under the folder ReplaceMe.m
and replace them with the code you want to upload the private library. Import two simple test files here:
// SYLog.h
+ (void)logger;
// SYLog.m
+ (void)logger {
NSLog(@"-------- Log --------");
}
4. Update the pod library of this project
cd
Go toExample
file- implement
pod install
5. When modifying .podspec
the file
, you need to pay attention to two attributes:
s.homepage
: The address of the private code warehouse https://gitee.com/sun-shiyu/sybasic-components
s.source
: The source address of the private code warehousehttps://gitee.com/sun-shiyu/sybasic-components.git
Configure other properties as needed.
6. Push the private library to the remote warehouse
Note that the remote end needs a branch, so create a branch master
first :master
cd /Users/sunshiyu/SYBasicComponents
git branch master
git checkout master
Commit the code locally and push it to the remote end:
git branch (检查当前所在分支)
git status (查看当前git存了什么文件)
git add . (将所有文件缓存到待提交文件区域)
git commit -m "上传组件"
git remote add origin https://gitee.com/sun-shiyu/sybasic-components.git (私有库git仓库地址)
git push -f origin master (将代码推送到远程私有库git仓库的master分支上面已经创建了)
git tag 0.1.0 (这里的版本号必须和podspec里面写的版本号一致)
git push -- tags (提交tag)
7. Local and remote verification
1. Local verification, in the current private library directory, enter the command:
pod lib lint --private --allow-warnings
Verification successful:SYBasicComponents passed validation.
2. For remote verification, enter the command in the current private library directory:
pod spec lint --private --allow-warnings
Verification successful:SYBasicComponents.podspec passed validation.
8. Submit the index file to the remote index library
1. After all verifications are passed, the spec file must be pushed to the private library index library created at the beginning.
cd 到私有库项目目录,
pod repo push <本地索引库名称> <索引文件名> --allow-warnings
<本地索引库名称>在 /Users/sunshiyu/.cocoapods/repos 下的私有库索引项目名称
<索引文件名>就是以 podspec 结尾的,注意不要弄错
For example, enter the command:
pod repo push SYComponentSpec SYBasicComponents.podspec --allow-warnings
2. After pushing, view the following figure in the local index library:
getee
View the following picture on the remote end:
4. Use basic component library
Create a project named arbitrarily SYSpecDemo
and initialize pod
:
cd /Users/sunshiyu/Desktop/SYSpecDemo
pod init
pod install
Open edit podfile
file
open podfile
as follows:
# Uncomment the next line to define a global platform for your project
platform :ios, '11.0'
# source 添加对应的索引库,否则会pod install失败
source 'https://gitee.com/sun-shiyu/sycomponent-spec.git'
target 'SYSpecDemo' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for SYSpecDemo
# pod 对应组件,可以对应版本
pod 'SYBasicComponents'
end
again:
pod install
You can see this library:
Import header files using this library:
#import <SYBasicComponents/SYLog.h>
// 打印 log
[SYLog logger];
5. The component library relies on third-party libraries
If our component library needs to rely on third-party libraries, such as AFNetWorking
, YYModel
etc., the operation is as follows:
1. Modify the component library SYBasicComponents.podspec
file and add dependencies:
The component library then performs Example
project execution pod install
.
2. Same as step 6 above, transfer the code modified by the component library to the remote end. Remember to remember to type , and it must be consistent tag
with the index library .version
git tag 0.2.0 (这里的版本号必须和podspec里面写的版本号一致)
git push -- tags (提交tag)
3. Submit the remote end of the index database, the same as step 8 above:
pod repo push SYComponentSpec SYBasicComponents.podspec --allow-warnings
4. Modify the main project podfile
file and add github CocoaPods
the index library to load github
third-party libraries:
source 'https://github.com/CocoaPods/Specs.git'
Then execute pod install
.
The above is the way to use the component library, that is, the remote method. This method requires opening and modifying the pod 'SYBasicComponents'
component library before updating , and then modifying the main project file (may not need to be modified depending on the situation), and finally the main project. Project re . In this way, you can get the latest code of the remote component library. The disadvantage is that every time the main project wants to update the latest code of the component library, it needs the component library to update the version of the component library. How to achieve that as long as the component library has code updates, the main project can directly pull the latest code. What about the code? The answer is to reference the local index library. We only need to modify the main project file as follows:tag
spec
version
podfile
pod install
tag
podfile
# Uncomment the next line to define a global platform for your project
platform :ios, '11.0'
# source 添加对应的索引库,否则会 pod install 失败
# source 'https://gitee.com/sun-shiyu/sycomponent-spec.git'
source 'https://github.com/CocoaPods/Specs.git'
target 'SYSpecDemo' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for SYSpecDemo
# pod 对应组件,可以对应版本
# 方式一、引用远端组件库
# pod 'SYBasicComponents'
# 方式二:引用本地组件库,不需要 source 'https://gitee.com/sun-shiyu/sycomponent-spec.git'
pod 'SYBasicComponents', :path => '~/SYBasicComponents/'
end
That is, use pod 'SYBasicComponents', :path => '~/SYBasicComponents/'
the method to directly reference the code of the local component library, so that the pulled code is the latest every time. Let’s pod install
give it a try. Pods
The directory structure will change as shown below:
The local component library SYBasicComponents
contains the latest code. Because our component library depends on it AFNetworking
YYModel
, Pods
these two libraries appear in .
Finish! ! !
Reference:
Build iOS private library to build
iOS component-based private library