Skip to content

Latest commit

 

History

History
73 lines (46 loc) · 2.55 KB

README.md

File metadata and controls

73 lines (46 loc) · 2.55 KB

ELJSBridge

Version Build Status Carthage Compatible

Note: This framework is no longer being actively maintained and will not be updated for future versions of Swift or iOS.

ELJSBridge is a Swift wrapper around JavaScriptCore's Objective-C bridge.

Installation

Carthage

Install with Carthage by adding the framework to your project's Cartfile.

github "Electrode-iOS/ELJSBridge" ~> 3.0.1

Manual

ELJSBridge can be installed manually by adding ELJSBridge.xcodeproj to your project and configuring your target to link ELJSBridge.framework.

ELJSBridge depends on the following Electrode-iOS modules:

Usage

Initialize a bridge instance and evaluate a script with the JavaScript context.

let bridge = Bridge()
bridge.context.evaluateScript("var question = 'What is your name?'")

let question: JSValue = bridge.contextValueForName("question")
println(question)
// What is your name?

JSContext

The underlying JSContext value can be changed by setting the context property. Suppose you wanted to retrieve the JavaScript context from a web view.

let webViewContextKeyPath = "documentView.webView.mainFrame.javaScriptContext"

if let context = valueForKeyPath(webViewContextKeyPath) as? JSContext {
    bridge.context = context
}

Exporting Native Objects

Objects that conform to JSExport can be exposed to JavaScript by adding instances to the bridge.

Swift:

bridge.addExport(Scanner(), name: "scanner")

Any methods declared in the JSExport-inherited protocol will be exposed to JavaScript.

JavaScript:

scanner.presentScanner(function(error, scannedValue) {});

Objects that are exported via the addExport method are retained between JavaScript context changes. When the context property is set all exported objects are added to the new JavaScript context value. This is useful when you need to provide a stateful API between context changes like page loads in a web view.