打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Javascript Examples
http://support.rollout.io/docs/javascript-examples


This document contain examples on how JavaScript can be used, here is a quick list of examples we have

ExampleDesctiption

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

Show Alert in case of Exception


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 method


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'sprefix``` 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 new instance


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:

Call method on instance


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

Open URL


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

Replace http with https


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

Crittercism Add Breadcrumb


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");
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
js页面跳转常用的几种方式
Window对象详解
常用的HTML代码
JavaScript 中绑定事件监听函数的通用方法[ addEvent() ]_一直想杀你...
[原]JavaScript的window.setTimeout()方法
JS window.open()属性
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服