An event bus for sending messages between UIWebView/WKWebView and embedded JS. Made with pure Swift.
- Easy, fast and reliable event bus system
- Multiple bus support
- Multithreading support
WKWebView
support- iOS ~> JavaScript supported types:
Bool
Int
Float
Double
String
- Any array (using types in this list, including dictionaries)
- Any dictionary (using types in this list, including arrays)
- JavaScript ~> iOS supported types:
Boolean
Int
Float
(available as aDouble
)String
Array
(available as aNSArray
)Object
(available as aNSDictionary
)
Add this line to your Podfile:
pod 'Caravel'
And run pod install
. Once installed, open the Pods
folder. Navigate to caravel/caravel/js
. In this folder, drag and drop caravel.min.js
into your Xcode project. You should load this JS script in any webpage you use Caravel.
Add this line to your Cartfile:
github "coshx/caravel"
And run carthage update
. Once installed, open the Carthage/Checkouts
folder. Navigate to caravel/caravel/js
. In this folder, drag and drop caravel.min.js
into your Xcode project. You should load this JS script in any webpage you use Caravel.
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 🍕 if your app does not work.
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
.