JavaScriptCore framework on iOS/OSX is missing some things from browser object model you get used to - setTimeout, XMLHttpRequest, etc. Also a lot of libraries requires to have such objects, for example rx.js requires to have setTimeout.
This projects extends JSContext with native implementation of some functions of BOM using Objective-C
Using cocoapods: pod 'JSCoreBom', '~> 1.1.1'
.
Whenever you would like to extend JSContext with BOM function just use:
JSContext* context = [[JSContext alloc] init];
[[JSCoreBom shared] extendContext:context];
Then just use it:
[context evaluateScript:@"setTimeout(function(){ console.log('Hi in 5 seconds!')},5000"];
Name | Description | Status |
---|---|---|
setTimeout | Implemented using dispatch_after | Done |
console.{info,log,debug,warn,error} | Would forward everything to NSLog | Done |
XmlHTTPRequest | Using NSUrlSession | Proto done |
For logger you can specify custom log handler:
[[JSCoreBom shared] extend:context logHandler:^(NSString* logLevel, NSArray* params, NSString* formattedLogEntry) {
if ([logLevel isEqualToString: @"log"])
[MyCustomLogger log:logEntry];
}];
Like Apple on a page 45 said to!
JSContext* context = [[JSContext alloc] init];
context[@"setTimeout"] = ^(JSValue* function, JSValue* timeout) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)([timeout toInt32] * NSEC_PER_MSEC)), dispatch_get_main_queue(), ^{
[function callWithArguments:@[]];
});
};
XMLHttpRequest:
- Always sync call is made using NSURLConnection, not NSUrlSession yet