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