Jspatch Use of small notes

Source: Internet
Author: User

Hotfix's role is well known, both Android and iOS have their own technologies, but iOS hot updates are more important than Android's Day release (if your project doesn't need grayscale). Because the iOS audit cycle is long, and bad luck will come across a variety of rejections, even if you apply for a quick review, you must also meet one of the two: can accurately tell Apple to reproduce the crash steps, or in the vicinity of special festivals. Perhaps you have struggled with the fear and so many days in fact, is to add three lines of code in a class.

1. Brief introduction

There may be people who have used Jscocoa before jspatch. But there are a series of complex problems, such as the source code has not been maintained for many years, the scale is huge, does not support ARM64. If you want to use also need to upgrade libffi, and try to be compatible with ARM64, it is difficult to compile.

The emergence of Jspatch basically solves all of the above problems. The cost of accessing Jspatch in a project is very low, and the need for a bit of brains may be how to properly submit and download.

On the principle of Jspatch the author's blog has been said very clearly, this article no longer explain that this article is mainly said when some access operations related.

If you are not in the Dong Platinum Blog Park See this article can click to view the original text.

2. Warehouse Setup

JS file must not be casually to the background of a folder to let the front-end to download, although easy to use but in the app or version more prone to confusion. Recommended specially set up a remote warehouse, the warehouse is mainly folders and JS files, when the need to submit a JS file, from the trunk to move out of a branch, in the right place to create a new folder and add a JS file, and then to the main pull Request, which should be a cumbersome but normative process. Folder structure Reference:

In the third-level folder, you can use the build number as well as the version name. After the request download should be the need to spell the project appname,version parameters.

3. Security Policy

Security related work if not done well, the worst case is that someone can call you by JS file Any OC method, we certainly cannot allow this kind of thing to happen. Generally in the JS file submitted to the warehouse after the backend should be MD5 or higher encoding of this section of JS code, and this code with the file exists, in the Meta.json in the memory is this piece of code. The structure of the return value after the send request should be roughly the following:

{data: {isupdate:true,content: ' Require (' mtpoifeedbackm ')    defineclass (' Mtfeedbackrankcell ', {            Setpoifeedback:function (poifeedback) {self                . Origsetpoifeedback (poifeedback)                var temcolor = require (' Uicolor '). Lightgraycolor ();                SELF.DETAILLBL (). SetTextColor (Temcolor);            }) ", Code:" 9c944f39e57f2e50bdb85deb878cc0f798efb9b0 "}}

The first is a field that tells us if there is an update to the JS file that was last downloaded. True to detect if the code returned below is the same code as the content encoding. Of course, this content can also not be returned directly but return a download URL is also fully available.

4. Update frequency

I have seen a lot of people in the use of JS and download JS code is placed in the Didfinishlaunchingwithoptions: This method. I think something is wrong, because if the app user has been placed in the background of the phone (for example), and there is no memory warning, this method should never be called. I suggest: Use the JS file code in Didfinishlaunchingwithoptions: While the code to download the JS file is placed in applicationdidbecomeactive: Because this method is called when the program starts and the background returns to the foreground. And I suggest setting a time interval, based on some data and trade-offs, we're using a time interval of 1 hours. That is, each time you come to this method, the first to detect is the distance from the last request for more than 1 hours, more than the request, otherwise skipped.

5. Access Process

The way to access is simple, the author also provides a demo program, roughly divided into several steps:

① added Javascriptcore.framework in general's Linkframeworks and libraries

This library is mainly used for JS and OC language bridging, such as some data types between the conversion.

②podfile add pod ' Jspatch' and pod install

③ add code to use JS and download JS code

Here the author also gives examples, using and downloading

    [Jpengine StartEngine];    NSString *sourcepath = [[NSBundle Mainbundle] pathforresource:@ "demo" oftype:@ "JS"];    NSString *script = [NSString stringwithcontentsoffile:sourcepath encoding:nsutf8stringencoding Error:nil];    [Jpengine Evaluatescript:script];

[Nsurlconnection sendasynchronousrequest:[nsurlrequest requestwithurl:[nsurl urlwithstring:@ "http://cnbang.net/ Test.js "] Queue:[nsoperationqueue Mainqueue] completionhandler:^ (nsurlresponse *response, NSData *data, NSError * Connectionerror) {    NSString *script = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding];    [Jpengine evaluatescript:script];}];

But if you're an enterprise app, it's definitely not a straightforward way to use it, so be sure to encapsulate at least one manager or the other. Our company's source code will not be posted here, but provide a general idea to build this manager:

6.JSPatch syntax

Here is the author's summary of the grammar: the author of the original text without deliberate to see, in the process of writing gradually familiar.

Here's a sample code to solve two simple bugs.

① Suppose there is a button we should default to let him not click, but previously forgot to set.

OC Code

-(void) Viewwillappear: (BOOL) animated{    [Super viewwillappear:animated];    self.confirm.enabled = NO;}

Jspatch

DefineClass (' Mtbregisterpage ', {            viewwillappear:function (animated) {            self.super (). Viewwillappear ( animated);            Self.confirm (). setenabled (NO);            }})

② Suppose there is a list page with special circumstances temporarily want us to change from black to Gray, and show a pop-up window

OC Code

-(void) Setdealfeedback: (MTDEALFEEDBACKM *) dealfeedback{    //------First Call the original method, the old code remains    Self.detailLbl.textColor = [Uicolor Lightgraycolor];    Uialertview *temalertview = [[Uialertview alloc]initwithtitle:@ "hint" message:@ "purchased items with grey display" delegate:self cancelbuttontitle:@ "OK" otherbuttontitles:nil, nil];    [Temalertview show]; }

Jspatch

Require (' mtpoifeedbackm ') defineclass (' Mtfeedbackrankcell ', {            setpoifeedback:function (poifeedback) {            self . Origsetpoifeedback (poifeedback)            var temcolor = require (' Uicolor '). Lightgraycolor ();            SELF.DETAILLBL (). SetTextColor (Temcolor);            var Temalertview = require (' Uialertview '). Alloc (). Initwithtitle_message_delegate_cancelbuttontitle_ Otherbuttontitles ("Hint", "purchased items with grey display", Self, "OK", null);            Temalertview.show ()            }})

Such code, feel that in the process of using the most frequently occurring bug, is to call the original method and then add a judgment to fault-tolerant or return.

Perhaps the author also feel that this jspatch grammar is not a formal language, we will not devote too much effort to study carefully, so the author himself also provides a simple and rough oc to JS Direct conversion address: http://bang590.github.io/JSPatchConvertor/

This address pro-test some simple writing is the correct conversion, but the more complex syntax can not be done directly by the machine, or need to manually modify. The author is also constantly perfecting the tool. The implementation principle of this converter: http://blog.cnbang.net/tech/2915/

7. More Thinking

①. After the jspatch is plugged in, iOS's online bug doesn't look as "tiger-like" as it used to be, but it's just an emergency plan, and the previous standard process still needs to be followed.

②. Each time this version of the online bug solved with Jspatch, the next version must be written in the project with OC Code, cannot allow the patch code to persist more than one version.

③. Advocating the use of agile development ideas, similar to the main logic or function module entrance method can be drawn more fine, so even if the need to modify, the cost is not too large, the author himself also mentioned, if there is a line of code must be in the middle of a large method to modify, then I can not, you have to use this whole method JS write again, so only set up J Spatchconvertor.

④. Each line bug solved with Jspatch should have a dedicated documentation record, and repeat errors must be written casestudy.

Think of these for the time being, hope this article can be helpful to the developer who is ready to access jspatch.

If you are not in the Dong Platinum Blog Park See this article can click to view the original text.

----------------------

I github:https://github.com/dsxniubility

Jspatch Use of small notes

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.