Record how you create private components and post any problems you encounter along the way. Make a note in case you forget what you did later and look back
Component creation
1. Pull the template
We will create it on the desktop in a folder called Demo. Enter the folder through terminal, and then enter the command:
pod lib create MSDNetworking
Copy the code
What platform do you want to use?? [iOS/macOS] > What language do you want to use?? [Swift/ObjC] > ObjC Would you like to include a demo application with your library? [ Yes / No ] > Yes Which testing frameworks will you use? [ Specta / Kiwi / None ] > None Would you like to do view based testing? [Yes/No] > Yes // > MSDCopy the code
After confirmation, the system will automatically configure the component project for you. The created project is as follows
thisExample
Index files have been created for your componentpodspec
And integrate the component.
Let’s look at the Podfile for Example:
use_frameworks! Platform :ios, '9.0' target 'MSDNetWorking_Example' do pod ', :path => '.. /' target 'MSDNetWorking_Tests' do inherit! :search_paths pod 'FBSnapshotTestCase' end endCopy the code
A test case is integrated for youpod 'FBSnapshotTestCase'
, can be ignored for now
We can see: pod ‘MSDNetWorking’, :path => ‘.. /’ path indicates the local directory, which corresponds to the MSDNetWorking home directory:
In this folder, one store your various class files, one store image resources, etc.
1.2 podspec file
After you answer the previous question, Pod automatically creates the file for you and executes the pod Install command, which finds the component’s index file (also local) MSDNetWorking. Podspec:
# # Be sure to run `pod lib lint MSDNetWorking.podspec' to ensure this is a # valid spec before submitting. # # Any lines starting with a # are optional, but their use is encouraged # To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html # Pod: : Spec. New do | s | s.n ame = 'MSDNetWorking' # component engineering of s.v ersion = '0.1.0 from' # component version number s.s ummary = 'A short description of # 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: The Add long description of the pod here. # DESC Sheldon horowitz at remote warehouse address # omepage = 'https://github.com/MaShudong/MSDNetWorking' s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2' s.license = { :type => 'MIT', :file => 'LICENSE' } s.author = { 'MaShudong' => '[email protected]' } s.source = { :git => 'https://github.com/MaShudong/MSDNetWorking.git', To_s} # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>' S.iso. Deployment_target = '9.0' s.source_files = 'MSDNetWorking/Classes/**/*' # s.resource_bundles = { # 'MSDNetWorking' => ['MSDNetWorking/Assets/*.png'] # } # s.public_header_files = 'Pod/Classes/**/*.h' # s.frameworks = 'UIKit', 'MapKit' #s.dependency 'AFNetworking', '~> 2.3' #s.dependency is the dependency of other components, third-party components, etc. endCopy the code
This file automatically configures some basic information for your component. Of course, this information needs to be modified according to the situation. For more information, you can search the related documentation.
> < p style = “max-width: 100%; clear: both; min-height: 1em;
2. Set shared files
The podspec file s.source_files = ‘MSDNetworking/Classes/**/*’ refers to the shared resource path where we need to put the shared files.
We open the component’s directory and see that there is already a file named ReplaceMe, which implies that you should replace it with a shared file.
There is one more comment left out in the PodSpec file:
# s.resource_bundles = { # 'MSDNetworking' => ['MSDNetworking/Assets/*.png'] # }
Copy the code
Let’s create an MSDNetworking class:
@interface MSDNetworking : NSObject -(NSString*)getSomething;
@end @implementation MSDNetworking
-(NSString *)getSomething{ return @"test method."; }
@end
Copy the code
Move it to the component’s shared directory and delete the empty file ReplaceMe:
So we have set up the shared content, the component development. Next, we useExample
Project to use the content of this component
Go to the Example project directory and run the pod install command to install the component.
We noticed that the latest files were added under Pods/Development Pods/MSDNetworking in the Example project
Use components
After we have installed the component, we can use the functionality of the component as we would with a tripartite library:
#import "MSDViewController.h" #import "MSDNetworking.h" **@interface** MSDViewController () **@end** **@implementation** MSDViewController - (**void**)viewDidLoad { [**super** viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (**void**)didReceiveMemoryWarning { [**super** didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (**void**)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { MSDNetworking * working = [[MSDNetworking alloc] init]; NSLog(@"%@",[working gerSomething]); }Copy the code
Console output:
**2022-01-10 15:03:44.293512+0800 MSDNetworking_Example[2879:175584] test something**
Copy the code
This means that the function is normal.
During component development, use pod ‘MSDNetworking’, :path => ‘.. /’ It is necessary to point the path locally to test that your components are properly configured and functional, rather than pushing to remote, publishing, and integrating.
Fourthly, the third party depends on the library
Sometimes, our component also depends on other components, or a tripartite library. We use the s.dependency field to set it up so that multiple libraries can be written multiple times.
Analyzing dependencies Fetching podspec for `MSDNetworking` from `.. / 'Downloading dependencies Installing AFNetworking (3.4.0)......Copy the code
We see that Example automatically pulls other components that the MSDNetworking component depends on. Another advantage of the CocoaPods tool is that it automatically detects installation for you when multiple components depend on the same component, rather than importing it repeatedly.
We found that the Example project’s Pods have locally developed components and remotely distributed components in separate directories.
With AFNetworking, you can modify your Web request component
#import "MSDNetworking.h" **@implementation** MSDNetworking - (NSString *)gerSomething{ **return** @"test something"; } #pragma mark ** ** -(**void**)GET:(NSString *)URLString parameters:(NSDictionary *)parameters success:(success)success failure:(Failure)failure{ AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/html",@"application/json", **nil**]; [manager GET:URLString parameters:parameters headers:parameters progress:^(NSProgress * **_Nonnull** downloadProgress) { } success:^(NSURLSessionDataTask * **_Nonnull** task, **id** **_Nullable** responseObject) { ; } failure:^(NSURLSessionDataTask * **_Nullable** task, NSError * **_Nonnull** error) { }]; } @endCopy the code
Once modified, it cannot be used directly in Example. You need to uninstall the component and reinstall it. Comment pod ‘MSDNetworking’, :path => ‘.. /’ Then execute pod install to complete the uninstallation.
At this point, component creation, file sharing, localization test use, and updates are complete. However, our component is to serve the host project after all. If it can only be integrated locally, it doesn’t mean much. We need to associate it with a remote server and push it to gitee, GitHub, Coding and other platforms built locally.
5. Associated remote warehouse
In the template podSpec file, you can specify a gitee repository address that you can use or modify. We chose to use it here, first going to Gitee to create the corresponding repository.
The system created the local Git repository for us when we initially created the component
Can be achieved by
Ls -la // CheckCopy the code
If not, you can create one using the command git init.
- Now we commit the previous changes (local commit).
Git commit -amCopy the code
- Add an associated remote repository
git remote add origin https://gitee.com/********/msdnetworking.git
Copy the code
- Push local content to a remote repository
git push -u origin master
Copy the code
Push failed, then violent push overwrite (the first warehouse without any code)
git push -f origin master
Copy the code
Go back to Gittee and refresh to see your commit record.
Six.tag
Send to remote repository
We have successfully associated the local repository and pushed it to the remote repository, now we want to publish a working component.
- First we have to type one for the current project
tag
Version number, inpodspec
In:
S.v ersion = '0.1.0 from'Copy the code
- The version number specified is
0.1.0 from
So let’s do the same0.1.0 from
็tag
:
Git tag 0.1.0 $git push $git pushCopy the code
- Refresh the page of the project
release
The version you just typed will appear in the options.
Publish to CocoaPods
7.1. Verify
Since we create projects and tag versions that use the information in the PodSpec file, we can directly verify that the information in the PodSpec file is valid, and if we need to adjust it, we should also verify it first: note
The version number of the PodSpec file must be the same as the tag.
pod spec lint
Copy the code
I got an error using this
It’s a different approach
pod lib lint MSDNetworking.podspec –allow-warnings –use-libraries
๐ perfect ๏ผ๏ผ๏ผ๏ผ๏ผ
Released 7.2.
You can now submit your PodSpec file to CocoaPods:
The first step is to generate a session via trunk registration:
// POD Trunk Register Pod Trunk register [email protected] XIAOMAGE1105 --description=NETWORKINGDEMON [!] Please verify the session by clicking the link in the verification email that has been sent to [email protected]Copy the code
Receive the email
To verify the
So that’s the proof of success
Now you can submit your PodSpec to CocoaPods. This file will be an index for others to search for your component.
pod trunk push MSDNetworking.podspec --allow-warnings
Copy the code
๐๐ป and the upload was successful
7.3. The search
After uploading, you can use Pod Search MSDNetworking to search for your own components
No search? ๐ ๐ป
Don’t panic if you don’t find it!
Try deleting the local search file
rm ~/Library/Caches/CocoaPods/search_index.json
Copy the code
The search fails even after this command is executed
Don’t panic!
1. Update the pod library
pod repo update
Copy the code
The problem is that the remote index library is not found here
Delete the local search index file
rm ~/Library/Caches/CocoaPods/search_index.json
Copy the code
Re-establish search
pod search MSDNetworking
Copy the code
๐๐๐ perfect search!
Integrate into the host project
We have completed the creation and release of the web component and support for the integration of CocoaPods. Now we need to integrate the component to the reservoir engineering, this part nothing to mention, because use and integrated library is the same, to say the three party libraries is only others write functional components, our component can also provide for the use of team members, compared with the pure tripartite library, many of our components are associated with the business part, Or other components based on the private, so the scope is smaller.