Jspatch is an open source project (github link) that simply introduces a minimal engine file into the project, and you can use JavaScript to call any Objective-c native interface and replace any objective-c native method. Currently mainly used to release JS script to replace the native OBJECTIVE-C code, real-time repair online bug.
In addition to repairing online bugs in real-time, even adding a module to the APP dynamically is possible, but there may be performance issues.
Using Jspatch requires a background where scripts can be issued and managed, and deployment work such as transport security needs to be handled.
There is a Jspatch platform that provides a range of services that can be used with jspatch just by introducing an SDK, but still in beta ....
Address: Https://github.com/bang590/JSPatch
Cocopods Installation:
Podfile file
Platform:ios, ' 6.0 '
Pod ' Jspatch '
And then
Pod Install
Let's start with: We'll create a list and change the number of rows by Jspath
1. Let's add a list to the controller to show him 3 lines with the following code:
#import"JRViewController.h"@interfaceJrviewcontroller () <Uitableviewdatasource,Uitableviewdelegate>@property (Nonatomic,Strong)uitableview* Mytableview;@end@implementationjrviewcontroller-(void) Viewdidload {[Super Viewdidload];uitableview* TV =[[UITableView Alloc]initwithframe:Self. View. Bounds Style:Uitableviewstyleplain];Self. Mytableview = TV;Self. Mytableview. Delegate =SelfSelf. Mytableview. DataSource =Self [Self. View Addsubview:Self. Mytableview];}#pragma mark--uitableviewdatasource-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger) section{return 3;} -(UITableViewCell *) TableView: (uitableview *) TableView Cellforrowatindexpath: (nsindexpath *) indexpath{ static nsstring* i= @ "cell"; uitableviewcell* cell = [TableView dequeuereusablecellwithidentifier:i]; if (cell = = nil) {cell =[[uitableviewcell alloc]initwithstyle:uitableviewcellstyledefault REUSEIDENTIFIER:I]; } cell. Textlabel. Text = @ "title"; return cell;}
Run it, show 3 lines, no problem
Paste_image.png
2. Create JS file New file, iOS, .....
Paste_image.png
3. Write in the JS file:
//将JRViewController类中的- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section方法替换掉defineClass(‘JRViewController‘, { tableView_numberOfRowsInSection: function(tableView, section) { return 10; }, });
For the specific use of Jspath, please consult yourself: Https://github.com/bang590/JSPatch/wiki
4. Call this JS file with the following code:
#import"AppDelegate.h"#import"JPEngine.h"@interfaceAppdelegate () @end @implementation appdelegate-(bool) Application: ( UIApplication *) Application didfinishlaunchingwithoptions: (NSDictionary *) launchoptions {[Jpengine startengine]; nsstring* path = [[nsbundle mainbundle]pathforresource:< Span class= "hljs-string" >@ "jspathtest" Oftype:@ "JS"]; nsstring* js = [nsstring Stringwithcontentsoffile:path Encoding:nsutf8stringencoding error:nil]; [Jpengine Evaluatescript:js]; return yes;}
Run it now, it's showing 10 lines!
Paste_image.png
Note:
If we want to use the JS script to replace the native OBJECTIVE-C code, real-time repair online bug, or to the server to issue a JS script!
The code is as follows:
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://........"]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { NSString *script = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; [JPEngine evaluateScript:script];}];
Tools
Replace the OC code with Jspatch script
IOS Repair online bug! on the fly via Jspatch