Overview
Over the past year the iOS industry has discussed a number of modular scenarios, broadly speaking, with 3.
Protocol Registration Scheme
URL Registration Scenario
Target-action Runtime Invocation Scenario
URL Registration Scheme As far as I know, many large companies are adopting, the Mushroom Street App of the Modular Road (http://limboy.me/tech/2016/03/10/mgj-components.html) Mushroom Street limboy in this blog has made a very detailed elaboration
Target-action Runtime Call Scheme Casa in the iOS application architecture talk about the modular solution (http://casatwy.com/ ios-modulization.html) also made a very detailed description, the former time Casa opened a blog in the existing project implementation of the Ctmediator-based component of the program clearly describes how to implement the package
Protocol solution I have not seen anyone do a detailed share, perhaps I ignorant, but here, I will teach you to implement (Http://casatwy.com/modulization_in_action.html) component with protocol scheme, Not only that.
I will use the above 3 kinds of plans to implement the detailed implementation of 3 Demo,demo will be at the end of the text, this article but more elaborate on the merits and demerits of 3 kinds of programs, I will do a summary at the end, I hope to understand the components of the program of the students or to the project in preparation for the implementation of modular program to provide a reference
Business simulation Scenarios
Home Display Product List
Product Details page Show details of the product
Confirm Order page Generate order
Keep your business together. Click on a item in the home page, enter the product details for a item, click on the purchase now to confirm the order page in the Product Details page, click on the Confirmation order page to return to the Product Details page and inform the user of the order success on the Product Details page.
The real business scenario confirms that the order page point submission order is not returned to the Product Details page, simulating this scenario to implement a reverse callback in 2 modules in the demo.
First, Protocol registration scheme
Before the formal implementation of the demo, it is recommended that only the next main project on it (note: The download does not require pod install or pod update,pods on my private source I did not fill out the. gitignore file, downloaded can be run directly)
Main Project Address
Https://github.com/sun6boys/CRMainProject
Product Details Business interface Component address
Https://github.com/sun6boys/CRGoodsDetailServiceProtocol
Product Details Business component address
Https://github.com/sun6boys/CRGoodsDetail
Confirm Order Business Interface component address
Https://github.com/sun6boys/CRConfirmOrderServiceProtocol
Confirm Order Business Component address
Https://github.com/sun6boys/CRConfirmOrder
Business Dispatch Middleware Address
Https://github.com/sun6boys/CRProtocolManager
1. Basic preparatory work
First go to GitHub to create a project to hold a private repo source, repo address https://github.com/sun6boys/CRRepositories.git The following 3 scenarios private pod source will be placed here.
Local add private source terminal execute command pod repo add crrepositories https://github.com/sun6boys/CRRepositories.git (if not previously to GitHub The push file does not have the SSH public key saved to GitHub, and you should be prompted to enter your GitHub account password.
The above operation completed CD ~/.cocoapods/repos directory will have at least 2 folders *crrepositories and master, the master file is stored under the public source file, Crrepositories directory is currently empty, We'll store our private source files later.
Basic preparatory work completed.
2.Xcode creating a project [Crprotocolmanager]
Crprotocolmanager and Mgjrouter, ctmediator belong to the middleware of scheduling between modules
Create a folder named Crprotocolmanager under the Crprotocolmanager project, where the files that we need to make private pods are placed under that folder.
Create a Crprotocolmanager class (. h,.m), define 2 external interfaces
@interface+ (void) Registserviceprovide: (ID) provide forprotocol: (protocol* + (ID) Serviceprovideforprotocol: (Protocol *@end
The implementation of the specific method is very simple to see the demo, I am just simple to deal with.
The next step is to submit the project to GitHub as a private pod.
GitHub creates a new project named Crprotocolmanager
Terminal CD to Crprotocolmanager project directory execute commands git remote add Origin https://github.com/sun6boys/CRProtocolManager.git
Cocoapods Force add Open Source license file Execute command echo mit>file_license create a file named File_license
Terminal CD to Crprotocolmanager directory execution Command pod spec create Crprotocolmanager
Execute the command vim. Crprotocolmanager.podspec edit Podspec file, how to edit it can be see the Podspec file in the demo or Google
Quit edit to execute the command git Add.
' Git commit-m ' log '
git tag 0.0.1 tag must be consistent with the version in Podspec
Git push Origin master--tags–tags to submit the tag you just added
Execute command pod repo push crrepositories crprotocolmanager.podspec--verbose--allow-warnings Note: crrepositories is the private source warehouse in preparation
After success, pod search Crprotocolmanager should be able to search
Long March finally completed the first step, the infrastructure has been built.
3. Commodity Details Business Module
Now that we have the components, all of our business modules are separate project, but here I will divide 2 project, one is the commodity Details Business entrance module, the other is the commodity Details business module. The Business Portal module is the protocol that defines the module's external supply of the business interface, and if a module calls to the B module, the A module simply introduces the protocol of the Crprotocolmanager and B modules, rather than introducing the entire B module.
Create a new Projectcrgoodsdetailserviceprotocol, creating a protocol file with the same name as the project, and define the interface as follows
@protocol-(Uiviewcontroller *) Goodsdetailviewcontrollerwithgoodsid: (nsstring*) Goodsid Goodsname: ( NSString *@end
Make a private pod with reference to Crprotocolmanager
The above implementation is complete, create a new projectcrgoodsdetail, create a new 2 class
Crgoodsdetailserviceprovidecrgoodsdetailviewcontroller
Crgoodsdetailserviceprovide is the Crgoodsdetailserviceprotocol's creator, so he relies on
Crgoodsdetailserviceprotocol, because the commodity details module needs to jump to the order confirmation page, so he also relies on Crprotocolmanager.
Add podfile file Edit as follows
' Https://github.com/sun6boys/CRRepositories.git ' 'https://github.com/CocoaPods/Specs.git ' Crgoodsdetail'do 'crprotocolmanager" "crgoodsdetailserviceprotocol"
Perform pod install--verbose--no-repo-update
The final Crgoodsdetailserviceprovide implementation code is as follows
#import "CRGoodsDetailServiceProvide.h"#import#import #import "CRGoodsDetailViewController.h" @interfacecrgoodsdetailserviceprovide ()@end @implementationCrgoodsdetailserviceprovide+ (void) load{[Crprotocolmanager registserviceprovide:[[self alloc] init] Forprotocol:@protocol(Crgoodsdetailserviceprotocol)];} -(Uiviewcontroller *) Goodsdetailviewcontrollerwithgoodsid: (nsstring*) Goodsid goodsname: (NSString *) goodsname{Crgoodsdetailviewcontroller*GOODSDETAILVC =[[Crgoodsdetailviewcontroller alloc] Initwithgoodsid:goodsid goodsname:goodsname]; returnGOODSDETAILVC;} @end
The Crgoodsdetailviewcontroller implementation code is as follows
#import "CRGoodsDetailViewController.h" @interfaceCrgoodsdetailviewcontroller () @property (nonatomic, copy) NSString*Goodsid; @property (nonatomic, copy) NSString*Goodsname, @property (nonatomic, strong) UILabel*Statuslabel, @property (nonatomic, strong) UIButton*Buybutton;@end @implementationCrgoodsdetailviewcontroller-(Instancetype) Initwithgoodsid: (NSString *) Goodsid goodsname: (NSString *) goodsname{ Self=[Super Init]; if(self) {_goodsid=Goodsid; _goodsname=Goodsname; } returnSelf ;} - (void) viewdidload {[Super viewdidload]; Self.navigationItem.title=Self.title; [Self.view AddSubview:self.statusLabel]; [Self.view AddSubview:self.buyButton];} - (void) viewwilllayoutsubviews{[Super Viewwilllayoutsubviews]; Self.statusLabel.frame= CGRectMake (0,0, -, -); Self.statusLabel.center=Self.view.center; Self.buyButton.frame= CGRectMake (0, Self.view.frame.size.height- $, Self.view.frame.size.width, $);} #pragmamark-event-(void) Didclickbuybutton: (UIButton *) button{}#pragmamark-getters-(UILabel *) statuslabel{if(_statuslabel = =Nil) {_statuslabel=[[UILabel alloc] init]; _statuslabel.textcolor=[Uicolor Redcolor]; _statuslabel.font= [Uifont systemfontofsize: the. f]; _statuslabel.textalignment=Nstextalignmentcenter; _statuslabel.text=@"Pending Purchase"; } return_statuslabel;} -(UIButton *) buybutton{if(_buybutton = =Nil) {_buybutton=[UIButton Buttonwithtype:uibuttontypecustom]; [_buybutton settitle:@"Buy Now"Forstate:uicontrolstatenormal]; [_buybutton Settitlecolor:[uicolor Whitecolor] forstate:uicontrolstatenormal]; [_buybutton Setbackgroundcolor:[uicolor Redcolor]; [_buybutton addtarget:self Action: @selector (Didclickbuybutton:) forcontrolevents:uicontroleventtouchupinside]; } return_buybutton;}@end
Make Crgoodsdetail a private pod Remember to add dependencycrprotocolmanager when editing Podspec files Crgoodsdetailserviceprotocol
4. New Master project Mainproject
In order to less build a project home module I was directly placed in the main project, the home page should also be a separate pod.
Home business scenario is to display a list of items, click on a product to enter the Product Details page. So he relies on crgoodsdetailserviceprotocol and Crprotocolmanager because the home module is the main project so he has to rely on crgoodsdetail.
The final home core code is as follows
#pragma mark-event-(void) Didclickgoodsbutton: (UIButton *) button{ ID Goodsserviceprovide = [Crprotocolmanager serviceprovideforprotocol:@protocol( Crgoodsdetailserviceprotocol)]; *GOODSDETAILVC = [Goodsserviceprovide goodsdetailviewcontrollerwithgoodsid:@ "123" Goodsname:@ " farmer Spring Mineral water "]; [Self.navigationcontroller PUSHVIEWCONTROLLER:GOODSDETAILVC Animated:yes]; }
5. Confirm Order Module
Refer to the product details to create a new confirmation order business entry pod and a confirmation order business pod. And the product details are different, after the submission of orders to return to the product details and inform the product details users have purchased, so the Crconfirmorderserviceprotocol interface is defined as follows
@protocol-(Uiviewcontroller *) Confirmorderviewcontrollerwithgoodsid: (NSString *@end
Finally remember in the product details plus jump and podspec inside add dependency
Protocol Registration Scheme concluded
IOS Modular Scenarios