jspatch– Dynamic Update iOS APP

Source: Internet
Author: User
Tags lua

Original: http://blog.cnbang.net/works/2767/

Jspatch is a recent amateur project that simply introduces a minimal engine into the project and can use JavaScript to invoke any Objective-c native interface to get scripting language capabilities: Dynamically update the app and replace the project native code to fix the bug.

Use

Have you ever had this experience: the new version on the line found that there is a serious bug, may lead to crash rate surge, may make the network request can not be issued, at this time can do is to quickly fix the bug and then submit to wait for a lengthy appstore audit, and then look forward to users to upgrade quickly, pay huge human and time costs To complete the fix for this bug.

Using Jspatch can solve such problems, just introduce jspatch in the project, you can find the bug when the JS script patch, replace the native method, no need to update the app to fix the bug immediately.

Example
01020304050607080910 @implementation JPTableViewController...- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{  NSString *content = self.dataSource[[indexPath row]];  //可能会超出数组范围导致crash  JPViewController *ctrl = [[JPViewController alloc] initWithContent:content];  [self.navigationController pushViewController:ctrl];}...@end

Taking an array element in the code above may cause crash to be outside the array range. If Jspatch is referenced in the project, a JS script can be issued to fix the bug:

010203040506070809101112131415 #import “JPEngine.m"@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    [JPEngine startEngine];    [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://cnbang.net/bugfix.JS"]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {    NSString *script = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];    if (script) {      [JPEngine evaluateScript:script];    }}];   ….    return YES;}@end
010203040506070809101112 //JSdefineClass("JPTableViewController", {  //instance method definitions  tableView_didSelectRowAtIndexPath: function(tableView, indexPath) {    var row = indexPath.row()    if (self.dataSource().length > row) {  //加上判断越界的逻辑      var content = self.dataArr()[row];      var ctrl = JPViewController.alloc().initWithContent(content);      self.navigationController().pushViewController(ctrl);    }  }}, {})

This jptableviewcontroller in the-tableview:didselectrowatindexpath: replaced by this JS script implementation, the user is not aware of the situation fixed the bug.

For more documentation and demos, please refer to the GitHub Project homepage.

Principle

Jspatch uses iOS built-in javascriptcore.framework as the JS engine, but does not use its Jsexport features for Js-oc function intermodulation, but through the objective-c Runtime, Transfer the class name function name from JS to Objective-c, and then use Nsinvocation to invoke the corresponding OC method dynamically. Detailed implementation principles as well as the various pits and hack methods encountered in the implementation process will be introduced in another an article.

Scenario Comparison

There are some scenarios that can be dynamically patched, such as Waxpatch, which can invoke the OC method with Lua, as compared to the Waxpatch,jspatch advantage:

1.JS language
JS than Lua in the field of application development has a broader application, the current front-end development and terminal development has a convergence trend, as an extension of the scripting language, JS is the perfect choice.

2. Comply with Apple rules
Jspatch is more in line with Apple's rules. IOS Developer Program License Agreement 3.3.2 mentions that executable code cannot be issued dynamically, except for code executed by Apple Javascriptcore.framework or WebKit, JS is executed by Javascriptcore.framework.

3. Compact
With the system's built-in javascriptcore.framework, there is no need for an embedded scripting engine, small size.

4. Support Block
Wax stopped development and maintenance a few years ago and does not support the objective-c of the block and LUA programs, although some third parties have implemented blocks, but there are also more restrictions on the use of parameters.

The disadvantage with respect to Waxpatch,jspatch is that iOS6 is not supported because Javascriptcore.framework is required. In addition, the current memory usage will be higher than wax, continuous improvement.

Risk

Jspatch gives the scripting language the ability to invoke all native OC methods, unlike web front-end capabilities that are limited to browsers, with some security risks:

1. If the transmission of the network in the text JS, may be the intermediary to tamper with JS script, execute any method, steal the relevant information in the app. The transfer process can be encrypted or resolved with direct use of HTTPS.

2. If the JS saved on the download is not encrypted locally, the user can also manually replace or tamper with the script on the jailbroken machine. This harm is not the 1th largest, because the operator is the owner of the mobile phone, there is no risk of misappropriation of information in the app. To prevent users from modifying the code to affect the app's operation, you can choose simple encrypted storage.

Other uses

Jspatch can be dynamically patched, free to modify the code in the app, in theory, you can fully use Jspatch to implement a business module, or even the entire app, like wax, but not recommended, because:

    1. Jspatch and Wax are the same through the Objective-c runtime interface through string reflection to find the corresponding class and method to make calls, the middle of the string processing will lose some performance, the other two languages type conversion also has performance loss, if used to make a complete business module, A large number of frequent back and forth Intermodulation, may have performance problems.
    2. The development process needs to use OC's thought to write Js/lua, loses the script language own characteristic.
    3. Jspatch and Wax have no IDE support and low development efficiency.

If you want to dynamically add modules to the app, react native gives a good solution to the above three issues:

    1. JS/OC does not communicate frequently and is delivered in batches during event triggering, increasing efficiency. (see React native communication mechanism detailed)
    2. The development process does not need to consider OC's feelings, follow the react framework of the idea of pure JS development on the line, the rest of the matter react native help you handle it.
    3. React native Even the IDE is ready.

So the dynamic Add business module is still recommended to try react Native, but react Native does not provide the native OC interface reflection calls and method substitution, unable to modify the native code, Jspatch with a small engine to complement the gap, with react Native uses the unified JS language to allow a native app to be in an extensible and modifiable state at all times.

At present Jspatch is in the development stage, the stability and the function still have some problems, welcome everybody to suggest/BUG/PR, together to do this project.

jspatch– Dynamic Update iOS APP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.