现在的app经常需要即时更新,那现在往往就存在不多的解决方案,主流的有iOS8之后的Framework 实现,web APP ,还有现在比较火的JSPatch; 今天先对JSpatch进行热修复学习。
同时在这里给大家一个比较好的网站 http://bang590.github.io/JSPatchConvertor/ .这是一个OC转JS的一个工具网站
JSPatch 基础
http://blog.csdn.net/u014410695/article/details/51151846
简单实例
http://lib.csdn.net/article/ios/35356
OC调JS:
1.#import “JPEngine.h”
2.call [JPEngine startEngine]
3.exec JavasScript by [JPEngine evaluateScript:@”“]
[JPEngine startEngine];
// 直接执行JS代码
[JPEngine evaluateScript:@"\
var alertView = require('UIAlertView').alloc().init();\
alertView.setTitle('Alert');\
alertView.setMessage('AlertView from js'); \
alertView.addButtonWithTitle('OK');\
alertView.show(); \
"];
// 执行网络请求下来的JS代码
[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];
}];
// 执行本地文件中的JS代码
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(function(1.0, function(){
// do something
}))
dispatch_async_main(function(){
// do something
})
学习地址:
源码:(https://github.com/bang590/JSPatch)
文章:http://www.finalshares.com/read-6892?jike-2