IOS UIWebView to JavaScript messaging bridge
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
IOSMQ.h
IOSMQ.js
IOSMQ.m
README.md

README.md

Simple Objective-C to JavaScript bridge

I needed to communicate between the app logic and the standard html pages deployed on a regular webserver, so this is the result, it's polling for events from javascript every second which isn't optimal but could easily be changed.

Requires the excellent SBJSON JSON Library

On the Objective-C end:

Initialize the messagequeue and connect it to a UIWebView

mq = [[IOSMQ queueWithWebView:browserview] retain];
[mq setDelegate:self];
[mq start];

Implement the callback in your delegate

- (void)queue:(IOSMQ *)queue message:(IOSMQMessage *)message {
    NSLog( @"Got message:%@ with id:%@", message.message, message.id );
    if( [message.message hasPrefix:@"purchase-status/"] ) {
        NSString *pid = [message.message substringFromIndex:16];
        // check status for specific id and then send the response...
        [queue send:[IOSMQMessage messageWithMessage:[NSString stringWithFormat:@"reply data from app (pid=%@)", pid] withID:message.id]];
    }
}

Send messages to the html app

[mq send:[IOSMQMessage messageWithMessage:@"message from app"]];

If you have another UIWebViewDelegate you want to use you can chain it after ourone.

mq.nextDelegate = mydelegate;

In JavaScript land:

Embed the javascript code in your page or load it

Register your callbacks:

IOSMQ.listen(function(message,id){
	alert('got message from app; message='+message+', id='+id);
	// id is used if you want to reply to the sender
});

Query for data in the native app:

IOSMQ.get( 'purchase-status/123', function( answer ) {
	alert(answer);
} );

Just posting messages to the native app is easy:

IOSMQ.post('msg123');