Latest 0.1.1
Homepage https://github.com/kf99916/JSContextFoundation
License MIT
Platforms ios 8.0
Authors

JSContextFoundation is a Swift adapter for the JavaScriptCore Framework to build up the JavaScript runtime environment.

Language Swift 3.1
CocoaPods
Swift Package Manager compatible
License

Requirements

  • iOS 8.0 or higher
  • Swift 2.3 or higher

Installation

Swift 2.3

pod 'JSContextFoundation', '~> 0.0.1'

Swift 3.1

pod 'JSContextFoundation', '~> 0.1.1'

Swift 2.3

.Package(url: "https://github.com/kf99916/JSContextFoundation.git", majorVersion: 0.0.1)

Swift 3.1

.Package(url: "https://github.com/kf99916/JSContextFoundation.git", majorVersion: 0.1.1)

JavaScript Runtime Environment

JSContextFoundation builds up the global objects for the JavaScript runtime environment to allow you execute JavaScript code like as in browser or Node.js. Here is the support list for the global objects:

global;
window;
console
  .log([data][, ...]);
  .info([data][, ...]);
  .warn([data][, ...]);
  .error([data][, ...]);

Usage

import JSContextFoundation

let jsContext = JSContextFoundation()

Import JavaScript Module

To import JavaScript module, the source of module can be file path or URL:

File Path

let path = NSBundle(forClass: self.dynamicType).pathForResource("Module", ofType: "js")!
do {
  try jsContext.requireWithPath(path)
} catch JSContextFoundationError.FileNotFound {
  print(path + " is not found")
} catch JSContextFoundationError.FileNotLoaded {
  print(path + " cannot be loaded")
} catch {
  print("Unknow Exception")
}

// ...

URL

let url = NSURL(string: "https://FQDN/Module.js")!
jsContext.requireWithUrl(url, completionHandler: {error in
   if let error = error {
      switch error {
        case JSContextFoundationError.FileNotFound:
            print(url.absoluteString + " is not found")
        case JSContextFoundationError.FileNotDownloaded:
            print(url.absoluteString + " is not downloaded")
        default:
            print("Unknown Error")
        }

        return
    }

    //  ...
})

All other usage is the same as JSContext. See JSContext in the Apple document for details.

Author

Zheng-Xiang Ke, [email protected]

License

JSContextFoundation is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "JSContextFoundation",
    "version": "0.1.1",
    "summary": "A Swift adapter for the JavaScriptCore Framework to build up the JavaScript runtime environment.",
    "description": "JSContextFoundation is a Swift adapter for the JavaScriptCore Framework to build up the JavaScript runtime environment.",
    "homepage": "https://github.com/kf99916/JSContextFoundation",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Zheng-Xiang Ke": "[email protected]"
    },
    "source": {
        "git": "https://github.com/kf99916/JSContextFoundation.git",
        "tag": "0.1.1"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "JSContextFoundation/**/*.swift",
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This