Original link: http://blog.csdn.net/hherima/article/details/50827363
"Only for technical exchanges, without permission to prohibit the reprint"
Usually app-server uses the HTTP protocol to tell the app what it needs to display the picture, text. This is one of the most common protocols. Another kind of client-side protocol, such as the news app, clicks on a focus story and app jumps to the appropriate channel or topic. It is not difficult to find: the former is Server->app communication, mainly content type, the latter is App<->app communication, mainly command, action type.
How to better handle the intra-app communication, it is necessary to first introduce the iOS platform interprocess communication. inter-process communication
For example, the iOS platform, interprocess communication IPC, can be implemented through a custom URL Schema and then app-(BOOL) Application: (UIApplication *) OpenURL: (Nsurl *) sourceapplication :(NSString *) annotation: (ID) method that can handle some commands and parameters. Where Sourceapplication is the caller's bundleidentifier. The following URL, for example, will tune up the Twitter client and pull up the interface for sending tweets.
URL scheme perfectly solves the problem of invoking the app and transmitting messages and commands in the same operating system. The URL schema mechanism provides a good idea for developers to solve the problem of intra-app communication. Cpmessage was born. What is Cpmessage
Cpmessage (Cross Platform message cross-platform messages) can be used as a server/client (let the client handle some commands), or a bridge for internal communication within the client. The originator of the communication may be the server, Push, user action, HTML5, or other app. The client performs the appropriate action according to the specific command and corresponding parameters. In short, the cpmessage idea originates from the URL schema mechanism of the iOS platform. the mechanism of Cpmessage
In order to ensure the consistency of multi-platform, the protocol name CPM is used as the beginning, but the protocol is delivered in a different way. On iOS, the way to communicate to clients is Scheme+protocol, for example:
Cpm://message.cmd?messageid=msgid&urls=encodedurlstrings&uid=uid&ex1=ex1&ex2=ex2&ex3=ex3.
The following sections are labeled:
In general: command names are message.cmd, often changed by MessageID (described in detail below). In the argument list, MessageID indicates the action, such as opening a webview, opening a new page, or adjusting the login box. The argument list is followed by some auxiliary parameters. Some examples are given in the table below.
Parameters |
Use |
Format |
Example |
MessageID |
Used to specify a response from the client |
String |
1.1 |
Url |
URL to jump |
String |
Www.baidu.com |
Ex1 |
Extra parameters |
String |
"03" |
In addition, if Ex1, EX2 and ex3 are not enough. You can also use the more parameter. For example: more = {"title": "HelloWorld", "Name": "Jack"};more parameter format is in JSON format. Facilitates client resolution of fields.
MessageID
The
messageid can be classified according to Mainid and SubID, such as messageid=2.1. So Mainid is 2,subid is 1. Generally speaking, Mainid is the distinction of operation type, for example:
mainid=1 means open page.
mainid=2 is passed data.
mainid=3 is open to other SDK and so on.
SubID is a breakdown of some of the operations, such as: Open the page and so on.
apps are cross-platform, with at least iphone,android clients, so the URL schema header will be different. Example: Server to the H page, a link is a CPM message, URL scheme with the use of CPM, the following format:
Myapp://cpm://xxxxxx. The operating system level reads "myapp://" to pull up the client, and the client reads "cpm://" cpmessage How to apply cpmessage to its app? You need to do three things:
First, register some Uiviewcontroller and methods in advance, and encapsulate them into cpmessageitem.
Second, when Cpmessage comes over, it resolves into cpmessagecommand, where Cpmessagecommand contains the parameters.
Third, use a Cpmmanager class to correspond command and item to a uiviewcontroller and use Nsinvocation to invoke a previously registered method.
So, back to the first question, let's assume that MessageID = 1.1 is the jump channel, EX1 is the index of the channel. So, the server encapsulates the following URL message into the Focus news
cpm://message.cmd?messageid=1.1&ex1=03
Client Tabviewcontroller Registers a switch channel method-(void) Switchtochannel: (int) index, performing Cpmmanager,tabviewcontroller Executes the Switchtochannel method. Can be very elegant solution to the news app, switch to any channel problem. UML diagram + flowchart of Cpmmanager
Several key functions of Cpmmanger
Prepareprocessor register some class and class methods in advance.
Handleurl: Receive URL, with a CPM schema URL
Oncpmiscoming: Parameter is Cpmcommand
HANDLECPM: Parameter Cpmcommand. The callback is finally implemented through [Nsinvocation invoke]. the realization of Cpmmanger
Here are just a few of the code, the key code for Cpmmanger:
[objc] View Plain copy//callback function registered to Cpmparse. - (void) oncpmiscoming: (nsnotification *) noti { NSDictionary *userInfo = noti.userInfo; if ([ Userinfo iskindofclass:[nsdictionary class]] { cpmcommand *command = [userinfo objectforkey: kcpmessageparsernotificaitoncommandkey];// if ([Command iskindofclass:[cpmcommand class]]) { [self handleCPM:command]; } } } // Really deal with the Cpmessage place. Associate the message command with the Uiviewcontroller by nsinvocation - (void) HANDLECPM: (Cpmcommand *) message { [self prepareProcessors]; CPMItem *item = (cpmitem *) [Self.processors objectforkey: Nsstringfromcpmessagecommand (message)]; if (Nil == item) { return; } [item getInfoFromCPMessageCommand:message]; uiviewcontroller *visiblectrl = [self.assit topViewController]; Class receiverClass = Nsclassfromstring (item.classname); if (Item.classmethod != nil) { [ Self performclassselector:item.classmethod ontarget:receiverclass withargs:item.classargs]; [item clearinfofromcpmessagecommand