TestsTested | ✗ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Feb 2017 |
SwiftSwift Version | 3.0 |
SPMSupports SPM | ✗ |
Maintained by Adrien Cadet, Ben Taitelbaum.
An event bus for sending messages between UIWebView/WKWebView and embedded JS. Made with pure Swift.
WKWebView
supportBool
Int
Float
Double
String
Boolean
Int
Float
(available as a Double
)String
Array
(available as a NSArray
)Object
(available as a NSDictionary
)Clone this repo and add Caravel.xcodeproj
into your workspace.
Caravel allows developers to communicate between their UIWebView
and the embedded JS. You can send any kind of message between these two folks.
Have a glance at this super simple sample. Let’s start with the iOS part:
class MyController: UIViewController {
@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Prepare your bus before loading your web view's content
Caravel.getDefault(self, webView: webView, whenReady: { bus in
// In this scope, the JS endpoint is ready to handle any event.
// Register and post your events here
bus.post("MyEvent", data: [1, 2, 3])
self.bus = bus // You can save your bus for firing events later
})
// ... Load web view's content there
}
}
And now, in your JS:
var bus = Caravel.getDefault();
bus.register("AnEventWithAString", function(name, data) {
alert('I received this string: ' + data);
bus.post("AnEventForiOS");
});
And voilà!
Caravel 1.1.0 supports WKWebView
. Keep in mind this component is still in beta and might not work as expected. We won’t ship you a
Anyway. If you’re this kind of guy who likes being involved in some risky business, here is an example about how to use Caravel with it. Be careful, it is a 2-step process.
class MyController: UIViewController {
private var wkWebView: WKWebView?
override func viewDidLoad() {
super.viewDidLoad()
let config = WKWebViewConfiguration()
// First generate a draft using your custom configuration
let draft = Caravel.getDraft(config)
// Build your WKWebView as usual then
self.wkWebView = WKWebView(frame: self.view.bounds, configuration: config)
// Finally initiate Caravel
Caravel.getDefault(self, wkWebView: self.wkWebView!, draft: draft, whenReady: {
// Do whatever you've got to do here
})
// ... Load content into your WKWebView there
}
}
Super duper easy. Just use the same codebase and use the JS script from Caravel. Finally, add this after having loaded the Caravel script:
var Drekkar = Caravel;
Firstly, ensure you are using the bus correctly. Check if you are unregistering the bus when exiting the controller owning your web component (either a UIWebView
or a WKWebView
). Use the unregister method for this.
Caravel automatically cleans up any unused bus when you create a new one. However, this operation is run in the background to avoid any delay on your side. So, a thread collision might happen if you have not unsubscribed your bus properly.
However, if you think everything is good with your codebase, feel free to open a ticket.
To raise iOS events, Caravel must be the delegate of the provided UIWebView
. However, if there is any existing delegate, Caravel saves it before setting its own. So, if you would like to use your custom one, simply set it before any call to Caravel.
To raise iOS events, Caravel adds a custom WKScriptMessageHandler
to the current content controller. If you would like to use your custom one, simply set it before any call to Caravel.
A subscriber could be any object except the watched target (either the UIWebView
or the WKWebView
). We recommend to use the controller as a subscriber (it is a common pattern).
CaravelInit
is an internal event, sent by the JS part for triggering the whenReady
method.
Also, the default bus is named default
. If you use this name for a custom bus, Caravel will automatically switch to the default one.
Finally, when using a WKWebView
, Caravel names its script message handler caravel
.