Modular development Solution in IOS (pure dry Goods) _ios

Source: Internet
Author: User
Tags md5

About the iOS modular development solution Online also has some introduction, but the actual implementation in the concrete instance but rarely see, plan to write system articles to introduce my understanding of the modular solution, which will contain some about decoupling, routing, encapsulation, private pod management, etc. And write an instance of the project put in git for open source [Jiamoduledemo], which now has some encapsulated functional modules; will be constantly updated, if you are interested in Star, the project is constantly updated to improve the optimization If you have a better plan or good advice to lssues, I will update and revise the corresponding problem in a short time;

One: Problems existing in the project

1: When there are multiple projects in the company at the same time, and there may be more than one person in different projects, there will be as shown in the picture, in fact, each app is a lot of common modules, of course, you will be the same function module code to copy a new project, but this is not the best way, In the late iterations, different people will add a lot of code with personal color to it; this is like the same module project later for multiple projects Unified management is also disastrous, it is possible to lose control, even if the project transfer other people will waste a lot of time, increase maintenance costs, So in the case of more attention to some of the same modules for extraction, to seek common ground, and the modular combination of private pods management, for commonly used functions of encapsulation, as long as the opening of a number of simple switch configuration, you can implement a function, such as logging, network request module, network status change tips;

2: For the coupling between the pages, and the page is also different between the parameters, because of a variety of developers or simple ways, and other reasons, the types of parameters have differences, including entities, simple basic types, and so on, the previous project for the route is not supported, resulting in the implementation of a message to push a different page jump there is a hard coding situation, for the function expansion there are considerable problems, while the right is modular after the interactive mode of the page; there is no coupling between pages, only with the jiamediator. , and the reference is unified into the form of a dictionary; while it may be possible to sacrifice some convenience and random, but can decouple modularity, and add the way to deal with the transaction, the agreement of the relevant protocols for interaction, and this route to replace those third-party routing plug-ins because of its flexibility, The most important thing is to omit the problem that the third party routing plug-in should register the route when starting;

II: Modular implementation of the solution

1:jiacore (basic function encapsulation)

Jiacore is the entire app's most basic module, all of the modularity to rely on, mainly contains a number of global functional modules, such as Jiabaseviewcontroller, Jiaappdelegate, and so on, has been a number of default functions are integrated in the inside, Includes the network state change judgment and the prompt, the logging function and so on, and some related configuration content uses the Jiacoreconfigmanager this management class to unify the setting, for instance whether to turn on logging function The Jiacoreconfigmanager class is open to configure the global configuration of the specific app; The following is explained in one of the logging features:

Jiacore Basic Module related configuration
jiacoreconfigmanager *jiacoreconfig=[jiacoreconfigmanager sharedinstance];
Jiacoreconfig.recordlogger=yes;
Then the prefixheader.pch of the specific app introduces the namespace and sets the level of logging:
#import "JiaCocoaLumberjack.h"
//ddlog level
static const int Ddloglevel = Ddloglevelverbose;

This completes an app for the introduction of the logging module, Jiacore has helped you to complete the related logging configuration, and the error content in a more readable format recorded in the file files, and these file generation rules are also defined, Of course, if you are in the Xcode console display different levels of color, as long as the installation of Xcodecolors Plug-ins and simple to set up on it, for different levels of different colors have been completed in the Jiacore configuration;

2:jspatch Hot Update function

In the Jiacore also the default integration of the hot update function, as long as the introduction of a Simple Object array will start the hot update, where Jiapathchmodel is already a defined model, in the app, the interface request into the model array, where Patchid is the unique value name, MD5 is the MD5 value of JS file, the URL is a JS download path, Ver is to which version of the function; because we are in the outside of the app are multiple versions of coexistence, hot update to be version, only download and this version of the corresponding hot update JS file loading, and MD5 value is to increase security, To avoid JS files are modified by others and affect the operation of the app, in Jiacore will download the JS file for MD5 calculation and comparison; for not in the Jspatchmutablearray before the JS file will be deleted;

Hot update content
Jiapathchmodel *sample=[[jiapathchmodel alloc]init];
Sample.patchid = @ "Patchid_sample1";
SAMPLE.MD5 = @ "2cf1c6f6c5632dc21224bf42c698706b";
Sample.url = @ "Http://test.qshmall.net:9090/demo1.js";
Sample.ver = @ "1";
Jiapathchmodel *sample1=[[jiapathchmodel Alloc]init];
Sample1.patchid = @ "Patchid_sample2";
SAMPLE1.MD5 = @ "E8A4EAEADCE5A4598FB9A868E09C75FD";
Sample1.url = @ "Http://test.qshmall.net:9090/demo2.js";
Sample1.ver = @ "1";
Jiacore Basic Module related configuration
jiacoreconfigmanager *jiacoreconfig=[jiacoreconfigmanager sharedinstance];
Jiacoreconfig.jspatchmutablearray=[@[sample,sample1] mutablecopy];

3:JIAGT Module (push package)

Message push for an app is very important, generally using a Third-party SDK for integration, in fact, most of the SDK processing code is similar, in this instance of the differential content extraction, the example will be a push line modular, Because most of the message push code is concentrated in the appdelegate, resulting in a lot of messy code, of course, there are some people to extend the classification of appdelegate to remove the code, the example will adopt another solution for extraction, can achieve complete decoupling, In the specific app will not appear to push the SDK related content, as long as a simple configuration and processing messages can, the following is simply a list of some of the code, other packaging code see source code;

Set up a push module configuration Jiagtconfigmanager *gtconfig=[jiagtconfigmanager sharedinstance];
gtconfig.jiagtappid=@ "0uuwznwonianok07jerwgas";
gtconfig.jiagtappkey=@ "26LEO4STBRA7TEYMUJDXLX3";

gtconfig.jiagtappsecret=@ "2282VL0IWZD9KL3ZPDYOUL7"; #pragma mark message push related processing/** * @author Wujunyang, 16-07-07 16:07:25 * * @brief processing a PUSH message * * @param notificationmessage * *-(Voi
d) Gtnotification: (Nsdictionary *) notificationmessage {NSLog (@ "%@", notificationmessage[@ "payload"]);
NSLog (@ "--received a push notice------"); /** * @author Wujunyang, 16-07-07 16:07:40 * * @brief processing REMOTE Apple Notification * * @param remotenotificationmessage/-(void) Receiverem
Otenotification: (nsdictionary *) remotenotificationmessage {NSLog (@ "%@", remotenotificationmessage[@ "message"]);
NSLog (@ "--received Apple notice------"); }/** * @author Wujunyang, 16-09-21 14:09:33 * * * * * @brief Devicetoken can do some binding operations on it @param Devicetoken < #devic Etoken description#>/-(void) Receivedevicetoken: (NSString *) Devicetoken {NSLog (@ "---current devicetoken:%@------",
Devicetoken); }

4:jiaanalytics Module (Friendship Union statistics package)

Jiaanalytics module is in the Alliance Statistics SDK and aspect, based on the combination of the introduction of the page to the statistics using the AOP section, the original should be in the life cycle of each page of the statistical code removed, app use as long as the simple configuration of the Union of the corresponding information, You can also set the filter conditions for the page to be counted, at present, there are three kinds of prefix string arrays for the beginning page to be counted, an array of page name strings to be counted, an array of not-counted page name strings, which can be used in combination to achieve accurate statistics of the page, and to put the statistic code on an asynchronous thread, Does not affect the response of the main thread;

__weak typeof (self) ws = self;
Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{
[Uiviewcontroller aspect_ Hookselector: @selector (viewwillappear:) withoptions:aspectpositionafter usingblock:^ (id<aspectinfo> info, BOOL animated) {
Uiviewcontroller *controller = [info instance];
BOOL filterresult=[ws Fileterwithcontrollername:nsstringfromclass ([Controller class])];
if (filterresult) {
[WS Beginlogpageview:[controller Class]];
}
error:null];
[Uiviewcontroller aspect_hookselector: @selector (viewwilldisappear:) Withoptions:aspectpositionafter usingBlock:^ ( id<aspectinfo> info, BOOL animated) {
Uiviewcontroller *controller = [info instance];
BOOL filterresult=[ws Fileterwithcontrollername:nsstringfromclass ([Controller class])];
if (filterresult) {
[WS Endlogpageview:[controller Class]];
}
error:null];
};

Three: Solution implementation of the page decoupling

Jiamediator acts as an intermediary, and all the response interactions between modules are done through it, and each module expands it into categories (for example, jiamediator+ module a), which is primarily intended for local invocation and does not want to be routed. The way to route is to pay attention to the accurate writing of routing constraints, it will directly affect whether the correct response to the target, in the case of the use of notifications in the form of callback parameters of the return problem;

The instance code is as follows:

Nsdictionary *curparams=@{kdesignermoduleactionsdictionarykeyname:@ "Wujunyang",
kdesignermoduleactionsdictionarykeyid:@ "1001", kdesignermoduleactionsdictionarykeyimage:@ "DesignerImage"}; Switch (indexpath.row) {case 0: {uiviewcontroller *viewcontroller=[[jiamediator Sharedinstance]jiamediator_designer_
Viewcontrollerfordetail:curparams];
[Self Presentviewcontroller:viewcontroller animated:yes completion:nil];
Break Case 1: {Uiviewcontroller *viewcontroller=[[jiamediator Sharedinstance]jiamediator_designer_
Viewcontrollerfordetail:curparams];
[Self.navigationcontroller Pushviewcontroller:viewcontroller Animated:yes];
Break Case 2: {nsstring *curroue=@ "jiascheme://designer/nativefetchdetailviewcontroller?name=wujunyang&id=1001
&image=designerimage "; Uiviewcontroller *viewcontroller=[[jiamediator Sharedinstance]performactionwithurl:[nsurl URLWithString:curRoue]
completion:^ (Nsdictionary *info) {}];
[Self.navigationcontroller Pushviewcontroller:viewcontroller Animated:yes];Break
Case 3: {nsdictionary *userparadictionary=@{kusermoduleactionsdictionarykeyid:@ "1"}; Uiviewcontroller *viewcontroller=[[jiamediator Sharedinstance] Jiamediator_user_viewcontrollerfordetail:
Userparadictionary];
[Self.navigationcontroller Pushviewcontroller:viewcontroller Animated:yes];
Break
} Default:break; }

Four: Modular combination of private pods scheme

Examples of only the relevant modular extraction are reflected in a project, and finally to implement a combination of pods management, separate management of each module, the different app can be simple through the pods instructions can achieve the effect of introducing the module, for some of the same module can be repeated in different app reference, Reduce the cost of repeat development;

The above mentioned is a small set of iOS in the introduction of the modular development of the solution (pure dry goods), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.