Jspatch Study Notes

Source: Internet
Author: User

Jspatch to make JS call/replace any OC method, so that the iOS app has the ability to hotfix

1. Introduction of the Execution JS script

[jpengine startengine];

Directly execute JS script

[jpengineevaluatescript:@ "\

  Console.log (' Call jpengine success ');

" ];

Execute local JS file demo.js script

nsstring *sourcepath =  [[ nsbundle mainbundle ] Pathforresource : @ "Demo" ofType : @ "JS" ];

NSString *script = [nsstringstringwithcontentsoffile: SourcePath encoding:nsutf8stringencodingerror: nil];

[jpengineevaluatescript: script];

2. Basic syntax

2.1 Calling the OBJC method

ObjC method

UIView *view = [[UIView alloc] init];

JS method

var view = require (' UIView '). Alloc (). Init ();

Require (' UIView ') creates a variable with the same name on the JS global scope, and the variable points to an object, which is then called directly as follows

var view = Uiview.alloc (). Init ();

Calling a method with a parameter

ObjC method

[View setbackgroundcolor:[uicolor Redcolor]];

JS method

View.setbackgroundcolor (Uicolor.redcolor ());

Invoking a multi-parameter method

ObjC method

[UIView Beginanimations:nil Context:nil];

JS method

UIView. beginanimations_context (null, NULL); //multi-parameter intermediate with "_" Connection

Add new code to the original method

ObjC

@implementation Customviewcontroller

-(void) viewdidload {

[Super viewdidload];

}

@end

Js

defineclass ("Customviewcontroller", {

Viewdidload:function () {

self . Origviewdidload ();

Self.view (). SetBackgroundColor (Require (' uicolor '). Whitecolor ());

},

})

2.2 OBJC Properties

ObjC method

Uicolor *color = View.backgroundcolor;

View.backgroundcolor = [Uicolor Redcolor];

JS method

var color = View.backgroundcolor ();

View.setbackgroundcolor (Uicolor.redcolor ());

Creating properties

ObjC method

@property (nonatomic, strong ) Nsarray *data;

JS method

Data: function() {

var data = Self.get_prop (' data ');

if (data) return data;

var data = [];

for (var i=0; i <; i++) {

Data.push ("JS data" + i);
}

return data;

}

The property is underlined to start

ObjC

Self.set_unserlinestring (' Test-test-Test ');

Js

self.set__underlinestring (' test ' test test ');//Double underline

2.3 Instance Variables

ObjC method

@interface CustomObject ()

NSString *_string;

@end

_string = @ "string";

JS method

Get

var string = Self.valueforkey (' _string ');

Set

Self.setvalue_forkey (' string ', ' _string ');

2.4 Strings, arrays, dictionaries

ObjC method

NSString *string = [NSString stringwithstring:@ "string"];

Nsarray *array = [[Nsarray alloc] initwitharray:@[@ "STRING0", @ "string1", @ "string2"];

Nsdictionary *dict = [[Nsdictionary alloc] initwithdictionary:@{@ "Key0": @ "Value0 " @ "Key1": @ "value1" @ " Key2 ": @" value2 "};

JS method

var string = ' string ';

var array = [' STRING0 ', ' string1 ', ' string2 '];

var dict = {' key1 ':' value1 ',' Key1 ' :' value1 '};

Console.log (Require (' nsstring '). stringwithstring (String). Tojs ( );//Call the Tojs () method to convert the corresponding method to the corresponding JS type

Console.log (Require (' nsarray '). Alloc (). Initwitharray (data). Objectatindex ( 0). Tojs ());

Console.log (Require (' nsdictionary '). Alloc (). Initwithdictionary (Dict). Objectforkey (' key1 '). Tojs ());

2.5Block

ObjC method

@property (nonatomic, copy) NSNumber * (^ addblock) (Nsinteger, Nsinteger);

Setter

Self.addblock = ^ (Nsinteger A, Nsinteger b) {[email protected] (A + b);};

Getter

(Self.addblock);

JS method

Setter

Self.setaddblock (Block ("Nsinteger, Nsinteger", function (A, b) {

Getter

var blk = Self.addblock ();

Blk (UP);

Return a + b;//when using block, the JS return value is NSObject type, where the NSNumber type is returned

}));

From JS to block to OC, there are two restrictions: a. The number of block parameters supports up to 4. (If you need more support, you can modify the source code) B. The block parameter type cannot be double.

Prevent circular references, using __weak and __strong

var weakself = __weak (self);

Self.setcustomblock (block () (function () {

var strongself = __strong (weakself);

Strongself.custommethod ();

}));

2.6GCD

ObjC method

dispatch_async(dispatch_get_global_queue ( Dispatch_queue_priority_default , 0), ^{

dispatch_async(dispatch_get_main_queue(), ^{

});

});

dispatch_after(dispatch_time(Dispatch_ Time_now, (int64_t) (1.0 * nsec_per_sec)), dispatch_get_main_queue(), ^{

});

JS method

Dispatch_async_global_queue (function () {

Dispatch_async_main (function () {

});

});

dispatch_after (1.0, function () {

});

Jspatch Study Notes

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.