This document contain examples on how JavaScript can be used, here is a quick list of examples we have
Example | Desctiption |
---|---|
call original function, catch exception and alert to user if exception got caught | |
Instead of running original method call a different method | |
Call an alloc init to create a new instance | |
Calling a method on an instance and supplying them arguments |
call the original method and catch the exception, in case the exception is caught pop an alert to the user
Patched method
-(int) runSomethingAndReturnIndex;
Problem
method crash with NSException from time to time
Solution
Surround the function with try catch and return -1 instead
try { returnValue = R.beta.originalImplementation();//calls the original implementation }catch (e) { returnValue = -1;} return returnValue;};
Redirect invocation to a different method with different args
Patched method
-(NSString*) doA;
Redirected method
-(NSString*) doBWith:(NSString*) prefix;
Problem
We would like to redirect the call from doA
to doB:
and supply doB`` an empty string for it's
prefix``` argument
Examples['-']['doA'] = function ( ) { var returnValue; // JSBox<NSString *> // write area starts here returnValue = R.beta.callMethod(self, "doBWith:", ""); // write area ends here return returnValue;};
Create a class instance and call alloc init to create a new instance
Init method
@interface Person: NSObject -(instancetype) initWithName: (NSString*) name age:(int) age;@end
Javascript code
var PersonClass = R.beta.NSClassFromString("Person");// create a class of Personvar person = R.beta.callMethod(R.beta.callMethod(PersonClass, "alloc"), "initWithName:age:", "Arthur Dent", 42); // call alloc and initWithName:age:
Calling a method on an instance and supplying them arguments
Target method
For this example we created a method that accepts a bunch or argument from different types to demonstrate how the arguments are passed
@interface Examples: NSObject-(void) doSomethingWithPerson:(Person*) p string:(NSString*) s dictionary:(NSDictionary*) dict double:(double) d integer:(int);@end
Beside Person*
argument all other arguments are convertible from JSBox to JavaScript value (see Javascript Docs)
to create JSBox<Examples> or JSBox<Person> take a look at create a new instance example
// assuming person instance already available from type JSBox<Person>// assuming exmaple instance already available from type JSBox<Examples>R.beta.callMethod(example, "doSomethingWithPerson:string:dictionary:double:integer:", person, "hi", {key:"value"}, 10.423, 2);
opens a url on device
var UIApplicationClass = R.beta.NSClassFromString('UIApplication');var sharedInstance = R.beta.callMethod(UIApplicationClass, 'sharedApplication'); var NSUrlClass = R.beta.NSClassFromString('NSURL');var url = R.beta.callMethod(NSUrlClass, 'URLWithString:', 'http://www.asciimation.co.nz/');R.beta.callMethod(sharedInstance, 'openURL:', url);
For a given NSURL
generate a new NSURL
with a different string (replace http with https in this example)
// httpNSURL is from JSBox<NSURL>var httpUrlStringBoxType = R.beta.callMethod(httpNSURL, 'valueForKeyPath:', 'absoluteString'); // from type JSBox<NSString>var httpUrlString = httpUrlStringBoxType.toJS(); // convert to JavaScript string valuevar httpsUrlString = httpUrlString.replace(/^http:/, 'https:'); var httpsNSURL = R.beta.callMethod(R.beta.callMethod(R.beta.NSClassFromString('NSURL'), 'alloc'), 'initWithString:', httpsUrlString); // generate class NSURL -> call alloc on class -> call initWithString: on instancehttpNSURL = httpsNSURL;// replace the original httpNSURL
Code in Objective C
[Crittercism leaveBreadcrumb:@"User tapped start button"];
Code in JavaScript
var CrittercismClass = R.beta.NSClassFromString("Crittercism"); // create a class of CrittercismR.beta.callMethod(CrittercismClass,"leaveBreadcrumb:", "User tapped start button");
联系客服