iOS builds a component-based private library

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

Insert image description here

2. Add private library index locally

Add private library index

pod repo add SYComponentSpec https://gitee.com/sun-shiyu/sycomponent-spec

Insert image description here

Insert image description here

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:

Insert image description here
2. Create SYBasicComponentsa local library.
Default creation path: /Users/sunshiyu/SYBasicComponents.

pod lib create SYBasicComponents

Insert image description here
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 Classesunder the folder ReplaceMe.mand 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 --------");
}

Insert image description here

4. Update the pod library of this project

  1. cdGo to Examplefile
  2. implementpod install
    Insert image description here

5. When modifying .podspecthe file
Insert image description here
Insert image description here
, 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 masterfirst :master

 cd /Users/sunshiyu/SYBasicComponents 
git branch master
git checkout master

Insert image description here

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)

Insert image description here
7. Local and remote verification

1. Local verification, in the current private library directory, enter the command:

pod lib lint --private --allow-warnings

Insert image description here
Verification successful:SYBasicComponents passed validation.

2. For remote verification, enter the command in the current private library directory:

pod spec lint --private --allow-warnings

Insert image description here
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

Insert image description here
2. After pushing, view the following figure in the local index library:
Insert image description here

geteeView the following picture on the remote end:

Insert image description here

4. Use basic component library

Create a project named arbitrarily SYSpecDemoand initialize pod:

 cd /Users/sunshiyu/Desktop/SYSpecDemo
pod init
pod install

Open edit podfilefile

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:
Insert image description here

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, YYModeletc., the operation is as follows:

1. Modify the component library SYBasicComponents.podspecfile and add dependencies:
Insert image description here

The component library then performs Exampleproject 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 tagwith the index library .version

Insert image description here

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 podfilefile and add github CocoaPodsthe index library to load githubthird-party libraries:

source 'https://github.com/CocoaPods/Specs.git'

Insert image description here

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:tagspecversionpodfilepod installtagpodfile

# 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 installgive it a try. PodsThe directory structure will change as shown below:

Insert image description here
The local component library SYBasicComponentscontains the latest code. Because our component library depends on it AFNetworking YYModel, Podsthese two libraries appear in .

Finish! ! !

Reference:
Build iOS private library to build
iOS component-based private library

Guess you like

Origin blog.csdn.net/SSY_1992/article/details/132039577