Latest 1.1.1
Homepage https://github.com/artemyarulin/JSCoreBom
License MIT
Platforms ios 7.0, requires ARC
Frameworks JavaScriptCore
Authors

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

How to use it?

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"];

What does it contain?

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];
}];

How does it work?

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:@[]];
    });
};

Known issues

XMLHttpRequest:

  • Always sync call is made using NSURLConnection, not NSUrlSession yet

Latest podspec

{
    "name": "JSCoreBom",
    "version": "1.1.1",
    "summary": "Extension for JavaScriptCore/JSContext that allows you to start using browser object model function: setTimeout, console.log, etc.",
    "description": "JavaScriptCore is missing some things you get used to - setTimeout, XMLHttpRequest, console.log, etc. This projects extends JSContext with native implementation of BOM using Objective-C",
    "homepage": "https://github.com/artemyarulin/JSCoreBom",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Artem Yarulin": "[email protected]"
    },
    "platforms": {
        "ios": "7.0"
    },
    "source": {
        "git": "https://github.com/artemyarulin/JSCoreBom.git",
        "tag": "1.1.1"
    },
    "source_files": [
        "JSCoreBom/JSCoreBom.{h,m}",
        "JSCoreBom/Modules/*"
    ],
    "public_header_files": "JSCoreBom/JSCoreBom.h",
    "frameworks": "JavaScriptCore",
    "requires_arc": true
}

Pin It on Pinterest

Share This