App competitive products technical analysis (6) thermal repair

8373 people read comment(7) Collection report
1 Native pages and Html5 pages of the mutual switch

Finally here, Native pages and Html5 pages of the mutual switch, which is the most exciting technology, more than I have been in the study of the App plug-in technology also shocked. Because the plug-in technology can only be applied to iOS, Android powerless. Even so, engage in Android plug-in technology needs to invest a lot of manpower and material resources. If the team is not big enough is not recommended to engage in plug-in programming. I remember 2 years ago, I went to a company interview, they were engaged in App plug-in, the interview asked me this thing, I was on the spot to throw a cold water, and then there is no then.

We know that Android is more in order to solve the problem of a serious crash or bug, and sometimes can also be an emergency on-line new features, and do not wait until the new version released. But the problem is precisely here, the real need for emergency repair is iOS, because each audit should be 1-2 weeks, and Android can be sent to the domestic market at any time. We can not do a loss of business, the cost of a huge human results found that there is no solution to the main contradiction.

So we will choose Html5, if found App had an accident, the module will temporarily switch to the Html5 website. But note, we usually put the whole module to switch to the Html5 site, this module will no longer have the Native page. This is some The loss outweighs the gain. So I began to think, can only modify the problem of the page, will be replaced by Html5, and the other pages of this module is still using Native?

I carefully studied a page - whether it is Android or iOS, the essential elements of the list are as follows:

First inlet and outlet, is Fudan in front of the main entrance security brother every day to ask the philosophical proposition, "where you go from here to where to go." To control the entrance and exit, in particular, the parameters of the transmission and outgoing parameters, we can do at any time between Html5 and Native switch. We can no longer arbitrarily A page in the B page, we should use the 7.1 introduction of the page Jump device, to decouple the dependencies between the various pages, in order to switch any Native page to Html5.

Note that the direct use of Navigator in the introduction of the is problematic. We define the dictionary in BaseActivity and BaseViewController, which is used to transfer parameters between pages. But Html5 does not recognize this mechanism. So it is necessary to define a new protocol, which is suitable for Android, iOS and pagename, and k1=v1&k2=v2 Html5 is a suitable protocol. For example, to jump from an HTML5 to Android or IOS page, protocol as shown in Figure 9-14, which single quotation marks in the content is in agreement with the, is composed of three parts, Android page name, IOS page name, parameter key pair, respectively with commas and semicolons separated.

Onclick= baobao.gotoAnyWhere "<a ('com.example.youngheart.MovieDetailActivity,
IOS.MovieDetailViewController:movieId= (Int> > > > > > > < 123'> >

Figure 9-14 protocol to jump from Html5 to Android or iOS page.

The second is the state, which includes global variables, local storage. A native page usually have to read and write global variables and local storage, if the switch to HTML5 page, you can't do these things the. Therefore, we should provide interactions between native and HTML5 for HTML5 page can read and write in native global variables and local storage.

Finally, the public components, such as network requests and management statistics. These are packaged into a common method in Native to facilitate the Html5 callback methods.

If the above three points are done, it can be replaced at any time line of a page, we as long as in app start time to call a MobileAPI interface, obtain a list of page, specify which page is what page of native HTML5 can be.

2 use scripting in iOS
2.1 find ways to quickly repair the App online bug

We mentioned in front of the App in the use of Html5, which is actually a script programming, but to show in the WebView.

I have seen some App by returning to the XML format or JSON format data, notify the App drawing UI. This is actually a scripting language, but it can only be drawn out of the UI, and can not be dynamically returned to a Native method, for example, click on the button to do something.

I am going to introduce the script programming, refers to the use of Lua or JavaScript in the iOS scripting language. For applications such as App, it does require scripting languages involved, especially those for the conversion rate of high demand for electricity supplier App, online once there is a deadly bug or Crash, you can quickly use scripting language to change. This like the body injured, posts a band aid, the wound healed (next new version), to remove the band aid.

In the field of mobile phone games, has been widely used Lua programming. The benefits of this is that every day by Lua editing code, add a new map or props, then through MobileAPI the Lua script returns to the app, achieve new function quickly on-line effect, without such on-line version control. In the next chapter, we look at how iOS is implanted in the Lua or JavaScript script.

2.2 iOS in the use of scripting language gossip history

First introduced the Wax third party open source library. Wax is a framework for using Lua scripting language to write iOS native applications, which establishes the mapping relationship between native objective-C iOS language and Lua scripting language.

But an invention wax this guy from 13 years not maintenance this framework, led to the wax in many of the remaining issues have not been resolved, for example does not support custom structure body and pointer, does not support multi thread and so on.

Later, the end of 13 years, Tu Yimin in wax based development of a WaxPatch. This is also on GitHub a open source project, its magic lies in, in the app starts will server to load the zip, zip package is written in Lua script patch, during the app to run, these patch files, to replace IOS in any of a class of any one method of implementation. Its implementation is to rewrite the class_replaceMethod method at runtime.[1]

Is in our lucky IOS found online quick fix bug solutions, then do not because the line, there are bugs and to put the boss can kill your eyes, apple in the February 15 years mandatory all new applications submitted must be compatible with the 64 bit, but the original use of the Lua wax frame is not support 64 bit of.

Things in life, nine out of ten.

So a few months, the open source community has given the 64 version of Wax, on this basis, we have the WaxPatch changes also transplanted in the past, there is a 64 version of WaxPatch.[2]

May 2015, JSPatch available. Its principle, like WaxPatch, is the implementation of any one of the methods to replace any of the classes in the App during the iOS operation, just as it is based on JavaScript. Estimation is the author of JSPatch Wax and WaxPatch can't wait to start the update has not so. At the same time, the author of JSPatch also provides a number of examples to help us understand this open source project.[3]

Wax and WaxPatch, after all, a long time not to maintain, it does not support the iOS multi-threaded syntax and custom structure and body structure, and JSPatch is to support these iOS features, so we recommend that you use JSPatch. When the book is about to be published, JSPatch has been relatively mature, but also continued to update, optimize the performance of the problem caused by reflection. Let us wait and see.

This book does not intend to introduce how to convert the Objective-C code to Lua or JavaScript, the official document has been very clear. In the following section, I will take WaxPatch as an example to introduce the use of it. JSPatch is the same as the use of ideas.

2.3 Zip package download strategy

Next introduces the WaxPatch compression package download rules. The content in the compressed package is the Lua script for hot patching.

First return to the Lua download address of the MobileAPI interface, to distinguish the version of App. For example a serious bug in the current version, to fix it into the, and we in the next version of repair the this bug, is not need to package, or said, the next version of the on-line found a new bug, this time to introduce So this MobileAPI interface should be based on the version number to return different Lua compression package download address.

How to control the App do not repeat the same Lua compression package download it? Get every call MobileAPI interface address to Lua compressed package, for example, we extract the compression package to local lua001 this directory, and put lua001 this value is stored in a local file variables luaVer. Next time you call the MobileAPI interface, you will be judged according to the address of the returned Lua,

  • If the value is null, the Lua script is not required to fix the bug, then the luaVer is set to null.
  • If the value is still no change, nothing to do;
  • If the value is the address of a new Lua compression package, for example,, then download the compressed package, its decompression to lua002 this new directory, and the value of luaVer is set to lua002.

In accordance with the above strategy, we can according to the value of luaVer, to control the App can be loaded to the latest Lua compression package, but also to avoid duplication.

2.4 debugging strategy

Our strategy is returned to the MobileAPI Lua compresses a bag in a dependence the download address, but not every development and debugging, a used to test Lua compressed package released to the server, because we during debugging are frequently modified Lua compressed files in the package.

Based on this, during debugging, we bypass from the server download Lua archive and version comparison approach, instead to Lua compressed package files directly copied to the local directory, for example, package 2 Lua files, we to these two files are integrated into the app project, when each time you start the app and put the two Lua files copied to the local, and can be used directly.

After all the debugging is completed, the code must be cut back to the mode that still downloads the Lua compression package from the server.

2.5 Lua does not support the scenarios and Solutions

Not all of the iOS code can be converted to Lua script. Here are some of the things I have encountered and the corresponding solutions.

1) if the variable or attribute declaration is wrong?

We know that the idea of WaxPatch programming is the implementation of any one of the methods that dynamically modifies any class at the time of the iOS runtime injection.

That is to say, any one method can be replaced by the Lua script, but it is not able to modify the signature of the method. But this is okay, in this case, we have to rewrite a method in Lua, a simple package of Objective-c does not conform to the way we want to go.

But if it is a type of attribute or class level variable that is wrong, we really have no way. Carefully examine the WaxPatch framework, and there is no definition of a property or variable place. In this case, our solution is to add a LuaClass class in the project, there is only one dictionary attribute dicLuaObject.

In the Lua script, we replace the attribute of the error type or any place of the variable to the correct type of variable, and this variable is defined in the dicLuaObject dictionary attribute of the LuaClass class.

2) how to deal with the block block?

Lua-Wax does not support block block. So once the code within the block block there is a problem, you want to rewrite the block block where the method, while the block block code package into another way, but also in the Lua script rewritten.

2.6 if the zip package was hijacked it?

Don't think MobileAPI returned to the Lua compression package download address, you can directly download and use the. Often a malicious attacker to hijack the server back to our download address, and let us go to download a malicious compression package. Once we download and extract the malicious package, the next possible all kinds of unexpected things.

To this end, we can not believe that any of the compression package online download is safe. We need a verification mechanism to ensure that the download to the compression package is provided by us, if verified, will delete or isolate the file.

SSH is the simplest solution, but the HTTPS protocol is too slow to visit, can make HTTP? We need to prepare a pair of public key and private key: the zip package using the private key to sign, then put them in the server available for download: the app download the zip package to a local, use save in the app's public key to check. We want to carry out strict confidentiality of the private key, can not leak to others, so even if someone in the App to get the public key, because there is no matching of the private key, there is no way to generate a zip package that we want to take.

2.7 the profound influence of Lua on iOS

With Lua this weapon, any Crash or bug on the line can be repaired with the fastest speed, and do not need to re submit a new version of the audit and wait for a long time. For example, our most distressed page management often found wrong or missed out a, this is to can not affect the data collection, using Lua can timely mending the vulnerability.

The last thing to add is that although the Lua language is very simple, especially the support of the WaxPatch framework, we can rewrite any method is easy. But I often see a lot of Objective-C methods have hundreds of lines of code, which has brought a lot of work to rewrite the workload. This has returned to the level of encoding norms, as far as possible to write the short method. Each method only does one thingthing.

[1]WaxPatch source address:

[2]64 bit versions of WaxPatch, see

[3]JSPatch download address, see


step on
Guess you're looking for
View comments
* the above user comments only represent their personal views, does not represent the views or position of the CSDN website
    My new book
    personal data
    • visit33929 times
    • Integral:Four hundred and sixty-three
    • Grade
    • Rank:Thousands of miles away
    • original9
    • Reproduced:0
    • Translation:0
    • Comments:34
    Classification of articles
    Article Archive
    Latest comments