Git Product home page Git Product logo

caravel's Introduction

Caravel Logo

CocoaPods

Join the chat at https://gitter.im/coshx/caravel

An event bus for sending messages between UIWebView/WKWebView and embedded JS. Made with pure Swift.

Features

  • 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 a Double)
    • String
    • Array (available as a NSArray)
    • Object (available as a NSDictionary)

Installation

Using CocoaPods

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.

Using Carthage

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.

Using as a submodule

Clone this repo and add Caravel.xcodeproj into your workspace.

Might be useful

Get started

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ร !

WKWebView

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
    }
}

Porting your app from Drekkar to Caravel

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;

Troubleshooting

๐Ÿ˜• Sometimes the bus is not working?!

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.

I want to use my custom UIWebViewDelegate. What should I do?

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.

I want to use my custom WKUserContentController. What should I do?

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.

What object should I use as a subscriber?

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).

Reserved names

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.

Keep in mind event and bus names are case-sensitive.

caravel's People

Contributors

acadet avatar btaitelb avatar gitter-badger avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.