TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | BSD |
ReleasedLast Release | Sep 2015 |
SPMSupports SPM | ✗ |
Maintained by Gaston Montes.
An iOS bridge for sending messages between Swift and Javascript.
SwiftJavascriptBridge is a Swift interface for bridging between WKWebView (Swift) and WebKit (Javascript).
SwiftJavascriptBridge can be use to send message from Swift to Javascript, from Javascript to Swift or to receive messages in Swift from Javascript or in Javascript from Swift.
SwiftJavascriptBridge Version | Minimum iOS Target | Minimum OS X Target | Notes |
---|---|---|---|
1.0.0 | iOS 7 | OS X 10.10.4 | Xcode 7 is required. |
CocoaPods is a dependency manager for iOS, which automates and simplifies the process of using 3rd-party libraries in your projects.
CocoaPods is distributed as a ruby gem, and is installed by running the following commands in Terminal.app:
$ sudo gem install cocoapods
$ pod setup
In the project root folder, run the following command to create a Podfile:
$ pod init YOURXCODEPROJECTFILE
If an YOURXCODEPROJECTFILE
project file is specified or if there is only a single project file in the current directory, targets will be automatically generated based on targets defined in the project.
An empty Podfile was created, so we are going to add dependencies to the Podfile specifying pods versions:
To use the latest version of a Pod, ommit the version specification:
pod 'SwiftJavascriptBridge'
Freezing to a specific Pod version:
pod 'SwiftJavascriptBridge', '0.0.1'
Using logical
operators:
'> 0.1'
, Any version higher than 0.1.'>= 0.1'
, Any version higher or equal to 0.1.'< 0.1'
, Any version lower than 0.1.'<= 0.1'
, Any version lower or equal to 0.1.Using optimistic
operators:
'~> 0.1.0'
, Version 0.1.0 or higher up to 0.2, not including 0.2.'~> 0.1'
, Version 0.1 or higher up to 1.0, not including 1.0.Install Pods dependencies in your project. Run the following commands:
$ pod install
From now on, be sure to always open the generated Xcode workspace (.xcworkspace) instead of the project file when building your project.
SwiftJavascriptBridge is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "SwiftJavascriptBridge"
public func bridgeLoadScriptFromURL(urlString : String)
public func bridgeCallFunction(jsFunctionName: String, data: AnyObject?)
public func bridgeRemoveHandler(handlerName: String)
public func bridgeAddHandler(handlerName: String, handlerClosure: HandlerClosure)
public func bridgeLoadScriptFromURL(urlString : String)
Load the 'urlString’s’ page that contains JavasCript code. After the page load, JavasCript functions can call Swift handlers and Swift function can call JavasCript functions.
Example:
var bridge: SwiftJavascriptBridge = SwiftJavascriptBridge.bridge()
self.bridge.bridgeLoadScriptFromURL("URLToLoad")
public func bridgeAddHandler(handlerName: String, handlerClosure: HandlerClosure)
Add Swift 'handlerName’ handler. Until bridgeLoadScriptFromURL() not called, bridgeAddHandler is going to have no effect. bridgeAddHandler() function can be called at any time, even before the page is loaded.
Parameters
: Example:
var bridge: SwiftJavascriptBridge = SwiftJavascriptBridge.bridge()
self.bridge.bridgeAddHandler("aHandlerName", handlerClosure: { (data: AnyObject?) -> Void in
// Handler called from JS.
NSLog(@"Handler called from JS.", nil);
});
public func bridgeRemoveHandler(handlerName: String)
Remove Swift 'handlerName’ handler. Until bridgeLoadScriptFromURL() not called, bridgeRemoveHandler is going to have no effect.
Parameters
: Example:
var bridge: SwiftJavascriptBridge = SwiftJavascriptBridge.bridge()
self.bridge.bridgeRemoveHandler("aHandlerName")
public func bridgeCallFunction(jsFunctionName: String, data: AnyObject?)
Call the JavasCript function called 'jsFunctionName’. 'jsFunctionName’ must be declared in the page loaded in bridgeLoadScriptFromURL() function or the call is going to have no effect. bridgeCallHandler() function can be called at any time, even before the page it is loaded.
Parameters
: Example:
var bridge: SwiftJavascriptBridge = SwiftJavascriptBridge.bridge()
self.bridge.bridgeCallFunction("swiftCallJSFunction", data: nil)
An example project is included with this repo. To run the example project, clone the repo, and run pod install
from the Example directory first.
NOTE
: The Example project use Nimble 2.0.0rc
testing framework that works with iOS 8.3 or higher. So, the development target of the Example project is 8.3, but the SwiftJavascriptBridge works with iOS 8.0 or higher.
Contributors: https://github.com/Elgatomontes/SwiftJavascriptBridge/graphs/contributors
Forks: https://github.com/Elgatomontes/SwiftJavascriptBridge/network/members
SwiftJavascriptBridge is owned and maintained by Gastón Montes.
SwiftJavascriptBridge is available under the BSD license. See the LICENSE
file for more info.