作者:NHHermit | 来源:互联网 | 2023-09-02 20:32
篇首语:本文由编程笔记#小编为大家整理,主要介绍了项目推荐JSPatch相关的知识,希望对你有一定的参考价值。
【编者按】今天推荐项目是来自 @bang590 的 JSPatch,JSPatch 是 ios 动态更新引擎,只需引入小小的引擎文件,即可用 JS 调用和覆盖任意原生代码,达到动态添加/修复 APP 功能的目的。
JSPatch 是一个 iOS 动态更新框架,只需在项目中引入极小的引擎,就可以使用就可以使用 Javascript 调用任何 Objective-C 原生接口,获得脚本语言的优势:为项目动态添加模块,或替换项目原生代码动态修复 bug。
@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[JPEngine startEngine]; NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"js"]; NSString *script = [NSString stringWithContentsOfFile:sourcePath encoding:NSUTF8StringEncoding error:nil];
[JPEngine evaluateScript:script];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.window addSubview:[self genView]];
[self.window makeKeyAndVisible]; return YES;
}
- (UIView *)genView{ return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
}@end
// demo.js
require('UIView, UIColor, UILabel') defineClass('AppDelegate', {
// replace the -genView method genView: function() {
var view = self.ORIGgenView(); view.setBackgroundColor(UIColor.greenColor())
var label = UILabel.alloc().initWithFrame(view.frame()); label.setText("JSPatch"); label.setTextAlignment(1); view.addSubview(label);
return view; } });
1. CocoaPods
CocoaPods 是 Objective-C 的依赖管理工具,利用 JSPatch 这一第三方库可以简化项目过程。
2. 手动安装
复制 JSEngine.m
JSEngine.h
JSPatch.js in to your protjct
JSPatch/ 到你的项目。
Objective-C
1. #import "JPEngine.h"
2. call [JPEngine startEngine]
3. exec JavasScript by [JPEngine evaluateScript:@""]
[JPEngine startEngine];
// exec js directly
[JPEngine evaluateScript:@"\
var alertView = require('UIAlertView').alloc().init();\
alertView.setTitle('Alert');\
alertView.setMessage('AlertView from js'); \
alertView.addButtonWithTitle('OK');\
alertView.show(); \
"];
// exec js file from network
[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];
}];
// exec local js file
NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"js"];NSString *script = [
NSString stringWithContentsOfFile:sourcePath encoding:NSUTF8StringEncoding error:nil];
[JPEngine evaluateScript:script];
//require
require('UIView, UIColor, UISlider, NSIndexPath')
// Invoke class method
var redColor = UIColor.redColor();
// Invoke instance method
var view = UIView.alloc().init();
view.setNeedsLayout();
// set proerty
view.setBackgroundColor(redColor);
// get property
var bgColor = view.backgroundColor();
// multi-params method (use underline to separate)
// OC:NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
var indexPath = NSIndexPath.indexPathForRow_inSection(0, 1);
// method name contains underline (use double undeline to represent)
// OC: [JPObject _privateMethod];
JPObject.__privateMethod()
// use .toJS() to convert NSArray / NSString / NSDictionary to JS type.
var arr = require('NSMutableArray').alloc().init()
arr.addObject("JS")
jsArr = arr.toJS()
console.log(jsArr.push("Patch").join('')) //output: JSPatch
// use hashes to represent struct like CGRect / CGSize / CGPoint / NSRange
var view = UIView.alloc().initWithFrame({x:20, y:20, width:100, height:100});
var x = view.bounds.x;
// wrap function with `block()` when passing block from JS to OC
// OC Method: + (void)request:(void(^)(NSString *content, BOOL success))callback
require('JPObject').request(block("NSString *, BOOL", function(ctn, succ) {
if (succ) log(ctn)
}));
// GCD
dispatch_after(function(1.0, function(){
// do something
}))
dispatch_async_main(function(){
// do something
})
定义类
你可以重新定义一个现有类和重写方法:
// OC
@implementation JPTableViewController
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cOntent= self.dataSource[[indexPath row]]; //may cause out of bound
JPViewController *ctrl = [[JPViewController alloc] initWithContent:content];
[self.navigationController pushViewController:ctrl];
}
- (NSArray *)dataSource
{
return @[@"JSPatch", @"is"];
}
- (void)customMethod
{
NSLog(@"callCustom method")
}
@end
// JS
defineClass("JPTableViewController", {
// instance method definitions
tableView_didSelectRowAtIndexPath: function(tableView, indexPath) {
var row = indexPath.row()
if (self.dataSource().count() > row) { //fix the out of bound bug here
var cOntent= self.dataSource().objectAtIndex(row);
var ctrl = JPViewController.alloc().initWithContent(content);
self.navigationController().pushViewController(ctrl);
}
},
dataSource: function() {
// get the original method by adding prefix 'ORIG'
var data = self.ORIGdataSource().toJS();
return data.push('Good!');
}
}, {})
扩展
存在一些支持 struct 类型,C 函数 以及其他函数的扩展,启动引擎后调用 +addExtensions: 接口增加扩展:
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[JPEngine startEngine];
//add extensions after startEngine
[JPEngine addExtensions:@[@"JPInclude", @"JPCGTransform"]];
NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"js"];
NSString *script = [NSString stringWithContentsOfFile:sourcePath encoding:NSUTF8StringEncoding error:nil];
[JPEngine evaluateScript:script];
}
@end
include('test.js') //include function provide by JPInclude.m
var view = require('UIView').alloc().init()
//CGAffineTransform is supported in JPCGTransform.m
view.setTransform({a:1, b:0, c:0, d:1, tx:0, ty:100})
JS 端动态加载某个扩展
require('JPEngine').addExtensions(['JPInclude', 'JPCGTransform'])
// `include()` and `CGAffineTransform` is avaliable now.
当然,你也可以在项目中创建你自己的扩展去支持 struct 类型和 C 函数。
阅读原文 GitHub 查看作者的源代码。
专业的开发者技术社区
多样化线上知识交流
丰富线下活动和给力工作机会