JSPatch uses note,

Source: Internet
Author: User

JSPatch uses note,

 

Hotfix is widely known for its role. Both Android and iOS have their own technologies. However, compared with the release of Android on the day (If your project does not require grayscale), iOS hot update is more significant. Because iOS does not review for a long period of time and may be rejected due to poor luck, even if you apply for a quick review, you must satisfy either of the two: Be able to accurately tell apple to reproduce the crash steps, or near a special holiday. You may be brave enough to add three lines of code in a class for so many days.

1. Brief Introduction

Before JSPatch, someone may have used JSCocoa. But there are a series of complex problems, such as the source code that has not been maintained for many years, the Code scale is huge, and ARM64 is not supported. If you want to use it, you also need to upgrade libffi and try to be compatible with ARM64. It is very difficult to compile and pass it.

The emergence of JSPatch basically solved all the above problems. The cost of connecting to JSPatch in a project is very low. You may need to think about how to submit and download it properly.

The author's blog on the principles of JSPatch has already made it clear that this article does not explain any more. This article focuses on some access operations.

If you do not see this article in Dong Boran's blog, click to view the original article.

2. repository settings

Js files cannot be downloaded from the front end as soon as they are put in a folder in the background. Although it is easy to use, it is easy to get confused when there are many apps or versions. We recommend that you build a remote repository that contains folders and js files. When you need to submit js files, move the Repository from the trunk to a branch, create a folder in a proper place, and add js files, then give the trunk a Pull Request, which should be a troublesome but standardized process. Folder structure Reference:

In the layer-3 folder, you can use the version name or build number. When sending a request for download, You Need To splice parameters such as the Project appname and version.

3. Security Policy

If security-related work is not done well, the worst case is that people can call any of your OC methods through js files, and we certainly cannot allow this kind of thing to happen. Generally, after the js file is submitted to the warehouse, the backend should perform md5 or higher-means encoding for this js Code, and combine this encoding with the file to obtain the meta. this encoding is stored in json. The structure of the returned values after the request is sent should be roughly as follows:

{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"}}

That is, there is a field that tells us whether there is an update compared to the js file downloaded last time. If it is true, check whether the code returned below is the same as the code obtained after content encoding. Of course, this content can return a download url instead of directly returning it.

4. update frequency

I have seen many people put the code for using and downloading js in didfinishlaunchingwitexceptions: This method. I think it is inappropriate because this method should never be called if the app user has been in the background of the mobile phone (for example) and there is no memory warning. My suggestion is: place the js file code in didfinishlaunchingwitexceptions, and the Code for downloading the js file in applicationDidBecomeActive: This method is called when the program starts and the background returns to the foreground. In addition, we recommend that you set an interval. Based on some data and trade-offs, we set the interval to 1 hour. That is to say, each time you come to this method, you must first check whether the interval between the last request is more than one hour. If the time interval exceeds 1 hour, the request is sent. Otherwise, the request is skipped.

5. Access Process

The access method is very simple. The author also provides the Demo program, which can be roughly divided into several steps:

① Add javascriptcore. framework to General LinkFrameworks and Libraries.

This library is mainly used for bridging js and oc languages, for example, mutual conversion between some data types.

② Add podfile 'jspatch 'and pod install

③ Add and download js code in the code

The author also provides examples for 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];}];

However, if you are an enterprise-level app, you must not use such a simple method directly. You must at least encapsulate a manager. The source code of our company will not be posted here, but it provides a general idea to establish this manager:

6. JSPatch syntax

Here is a summary of the syntax listed by the author: the author does not have to look at the original text, but gradually gets familiar with the writing process.

The following is an example code to solve two simple bugs.

① If there is a button, we should make it unclickable by default, but we forgot to set it.

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. In special cases, we want to change it from black to gray and display a pop-up window.

OC code

-(Void) setDealFeedback :( MTDealFeedbackM *) dealFeedback {// ------ call the original method first, and keep the old code self. detailLbl. textColor = [UIColor lightGrayColor]; UIAlertView * temAlertView = [[UIAlertView alloc] initWithTitle: @ "prompt" message: @ "Gray Display of purchased items" 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 ("prompt", "Gray Display of purchased items", self, "OK", null); temAlertView. show ()}})

For such code, I think that the most frequently used bug occurs in the process of use, that is, calling the original method and then adding a judgment for fault tolerance or return.

Maybe the author thinks that, after all, this JSPatch syntax is not a formal language, and everyone will not invest too much effort to study it carefully, so I also provide a simple and crude OC to JS direct conversion address: http://bang590.github.io/JSPatchConvertor/

Some simple writing methods for this address are correct conversion, but the complicated syntax still cannot be done directly by machines, and manual modification is required. The author is constantly improving this tool. The realization principle of this converter: http://blog.cnbang.net/tech/2915/

7. More Thoughts

①. After JSPatch is connected, the online BUG of iOS does not seem to be as "fierce as a tiger" as before. However, this is only an emergency plan. Previously, the standard process still needs to be followed.

②. For each online Bug solved by JSPatch in the current version, the next version must be written into the project using OC code, and no more than one version of the patch code is allowed.

③. The idea of advocating agile development is similar to that of the main logic or function module entry. This way, even if you need to modify it, the cost will not be too high. As mentioned by the author, if there is a line of code that must be modified in the middle of a large method, then I can't do it anymore. You can only write this method all over again using js, so JSPatchConvertor is set.

④. Each online BUG solved with JSPatch should have a special document record, and casestudy must be written in case of repeated errors.

 

Now, I hope this article will be helpful to developers who are going to access JSPatch.

If you do not see this article in Dong Boran's blog, click to view the original article.

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

My github: https://github.com/dsxNiubility

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.