Jspatch Study Record

Source: Internet
Author: User

This article refers to Jspatch wiki:https://github.com/bang590/jspatch/wiki1. Concept

Jspatch is a lightweight JS engine that can use the JavaScript language to invoke any OBJECT-C interface, replacing any native method. Currently mainly used for the Step JS script to replace the native OBJECTIVE-C code, real-time repair online bug

2. Principle

Using the dynamic nature of OC language, dynamic modification of the methods and properties of the class. When the app launches, it loads our JavaScript files and executes them through JavaScriptCore, using JS-written class functions to tamper with the original OC function. Jspatch only provides code to tamper with the process, such as deploying the line JS code and downloading the logic to write it yourself. Of course you can use JSPATCHSDK this platform, this platform to help us to deploy JS code, download and other logic. JSPATCHSDK is free of charge.

3. Deployment

The introduction of Jspatch via pod or other means, The didfinishlaunchingwithoptions function adds the following code:

*sourcepath = [[NSBundle mainbundle] pathforresource:@ "index" oftype:@ "JS"*script = [NSString Stringwithcontentsoffile:sourcepath encoding:nsutf8stringencoding Error:nil]; [Jpengine Evaluatescript:script];

After the OC code is deployed, we can fix the bug on the line by writing JavaScript in Index.js.

4. Write JS code
    • DefineClass used to define (overwrite) a class
    • DefineClass (ClassDeclaration, [Properties,] instancemethods, Classmethods) classdeclaration: String type, Represents the class name properties: An array of strings representing the list of attributes to add Instancemethods: Instance method Classmethods: Tired method
    • Here is an example:
    • defineclass (    ' Mainviewcontroller ',    {        viewdidload:function() {            Self.super (). Viewdidload ();            Self.view (). SetBackgroundColor (Require (' Uicolor '). Redcolor ());            Self.navigationcontroller (). Setnavigationbarhidden (true);        }    )

Jspatch Study Record

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.