setdirection/SDJSBridge — pull request #2 by angelodipaolo (2014-12-19 17:09:35+00:00)
Title: add low-level handler API to JS bridge

Conform to the `SDJSBridgeHandler` protocol to support the handler API in a script object. The script object provides a handler name and handles the `callHandler:` event from JS.

```
@implementation SDJSAlertScript

#pragma mark - SDJSBridgeHandler

- (NSString *)handlerName {
return @"alert";
}

- (void)callHandlerWithData:(JSValue *)data callback:(SDBridgeHandlerCallbackBlock)callback {
self.callback = [callback copy];

NSDictionary *object = [data toObject];
NSString *title = object[@"title"];
NSString *message = object[@"message"];

[self showAlert:title message:message actions:nil];
}

#pragma mark - Alert API

- (UIAlertView *)showAlert:(NSString *)title message:(NSString *)message actions:(NSArray *)actions {
self.actions = actions;

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:nil otherButtonTitles:nil];

for (SDJSAlertAction *action in self.actions) {
[alert addButtonWithTitle:action.title];
}

if (!self.actions.count) {
[alert addButtonWithTitle:@"OK"];
}

[alert show];
return alert;
}

#pragma mark - UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (self.callback) {
self.callback(nil);
}
}

@end
```

As an alternative to the bridge handler protocol, the lower level API allows you to directly register bridge handlers.

```
[webViewController registerBridgeHandlerWithName:@"alert" handler:^(JSValue *data, SDBridgeHandlerCallbackBlock callback) {
NSDictionary *object = [data toObject];
NSString *title = object[@"title"];
NSString *message = object[@"message"];

// show alert...

// call JS callback
callback(nil);
}];
```