All Projects → bang590 → Jspatch

bang590 / Jspatch

Licence: mit
JSPatch bridge Objective-C and Javascript using the Objective-C runtime. You can call any Objective-C class and method in JavaScript by just including a small engine. JSPatch is generally used to hotfix iOS App.

Programming Languages

objective c
16641 projects - #2 most used programming language
c
50402 projects - #5 most used programming language
C++
36643 projects - #6 most used programming language
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Jspatch

Jengine
JEngine是针对Unity开发者设计的开箱即用的框架,封装了强大的功能,小白也能快速上手,轻松制作可以热更新的游戏 | JEngine is a streamlined and easy-to-use framework designed for Unity Programmers which contains powerful features, beginners can start up quickly and making hot update-able games easily
Stars: ✭ 564 (-95.03%)
Mutual labels:  hotfix
Bdframework.core
[中]Simple! Easy! Powerful Unity3d game workflow! Unity3d framework:c# hotfix(ILRuntime)、asset manager、ui workflow、network debug... and so on
Stars: ✭ 1,196 (-89.45%)
Mutual labels:  hotfix
Hotfix
一种轻量级的可以通过苹果审核的热修复方案,可以替代JSPatch的热修复方案。
Stars: ✭ 114 (-98.99%)
Mutual labels:  hotfix
Tinker Manager
微信tinker补丁管理,后端代码+客户端sdk
Stars: ✭ 670 (-94.09%)
Mutual labels:  hotfix
Tinkerpatch Sample
已经改造完自身App的Application的应用的参考Sample
Stars: ✭ 63 (-99.44%)
Mutual labels:  hotfix
Injectfix
InjectFix is a hot-fix solution library for Unity
Stars: ✭ 1,198 (-89.43%)
Mutual labels:  hotfix
Robust
Robust is an Android HotFix solution with high compatibility and high stability. Robust can fix bugs immediately without a reboot.
Stars: ✭ 4,125 (-63.61%)
Mutual labels:  hotfix
Objc Runtime
A debuggable objc runtime
Stars: ✭ 1,679 (-85.19%)
Mutual labels:  objc-runtime
Tinker app
Android热更新Tinker + 多渠道打包 + 加固的流程详解demo
Stars: ✭ 67 (-99.41%)
Mutual labels:  hotfix
Fastdex
🚀 加快 apk 的编译速度 🚀
Stars: ✭ 1,457 (-87.15%)
Mutual labels:  hotfix
Aceso
A hotfix library for Android
Stars: ✭ 790 (-93.03%)
Mutual labels:  hotfix
Andfix
AndFix is a library that offer hot-fix for Android App.
Stars: ✭ 6,925 (-38.92%)
Mutual labels:  hotfix
Speed tools
敏捷开发工具包
Stars: ✭ 85 (-99.25%)
Mutual labels:  hotfix
Sdkhotfix
在用户规模千万级别的app中验证过,稳定无兼容问题,5分钟让你的SDK拥有热修复能力。A SDK hotfix library with stable verified in tens of millions of users can be accessed in only five minutes.
Stars: ✭ 585 (-94.84%)
Mutual labels:  hotfix
Dsbridge Ios
🌏 A modern cross-platform JavaScript bridge, through which you can invoke each other's functions synchronously or asynchronously between JavaScript and native.
Stars: ✭ 1,673 (-85.24%)
Mutual labels:  jsbridge
Small
A small framework to split app into small parts
Stars: ✭ 5,012 (-55.79%)
Mutual labels:  hotfix
Dynamicoc
深入理解 iOS 热修复原理
Stars: ✭ 76 (-99.33%)
Mutual labels:  hotfix
Nice Ts
基于puerts的Unity游戏框架,集成fairygui,protobufjs并采用addressables管理资源
Stars: ✭ 131 (-98.84%)
Mutual labels:  hotfix
Glean
hotfix for go applications via plugin, supports Linux and MacOS
Stars: ✭ 125 (-98.9%)
Mutual labels:  hotfix
Amigo
A hotfix library for Android platform, and not just this...
Stars: ✭ 1,350 (-88.09%)
Mutual labels:  hotfix

JSPatch

Travis CocoaPods Version License

中文介绍 | 文档 | JSPatch平台

请大家不要自行接入 JSPatch,统一接入 JSPatch 平台,让热修复在一个安全和可控的环境下使用。原因详见 这里

JSPatch bridges Objective-C and JavaScript using the Objective-C runtime. You can call any Objective-C class and method in JavaScript by just including a small engine. That makes the APP obtaining the power of script language: add modules or replacing Objective-C code to fix bugs dynamically.

JSPatch is still in development, welcome to improve the project together.

Notice: Please go to Wiki to get full document.

Example

@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;
  }
});

You can also try to use JSPatch Convertor to convertor code from Objective-C to JavaScript automatically.

Installation

CocoaPods

CocoaPods is a dependency manager for Objective-C, which automates and simplifies the process of using 3rd-party libraries like JSPatch in your projects. See the "Getting Started" guide for more information.

# Your Podfile
platform :ios, '6.0'
pod 'JSPatch'

Manually

Copy JSEngine.m JSEngine.h JSPatch.js in JSPatch/ to your project.

Usage

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];

JavaScript

Base Usage

//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(1.0, function(){
  // do something
})
dispatch_async_main(function(){
  // do something
})

Go to wiki page for more details: Base Usage

defineClass

You can redefine an existing class and override methods.

// 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!');
  }
}, {})

Go to wiki page for more details: Usage of defineClass

Extensions

There are some extensions provide support for custom struct type, C methods and other functional, call +addExtensions: after starting engine to add extensions:

@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})

Extensions can be added dynamiclly in JS, which is recommended:

require('JPEngine').addExtensions(['JPInclude', 'JPCGTransform'])

// `include()` and `CGAffineTransform` is avaliable now.

You can create your own extension to support custom struct type and C methods in project, see the wiki page for more details: Adding new extensions

Enviroment

  • iOS 7+, forward compatibility with iOS 6
  • JavaScriptCore.framework
  • Support armv7/armv7s/arm64
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].