SCWebViewBridge
SCWebViewBridge helps make Javascript calls to Objective-C and Objective-C calls to Javascript easier. Supports passing primitive types as arguments. iOS 5 and above supported.
Example Usage
Calling Objective-C from Javascript
SCWebViewBridge.call( "methodSimple" );
SCWebViewBridge.call(
"methodComplex:array:",
new Date(), [ "Stuff, you’ll need to blah blah… view & engagement[ ],", 4.2, true ] );
Register for calls from Javascript
[self.webViewBridge registerForCalls:self];
Calling Javascript from Objective-C
[self.webViewBridge call:@"testSimple" arguments:nil];
[self.webViewBridge call:@"NamespaceTest.testComplex" arguments:[NSDate date], @"TEST", nil];
Load local HTML file
self.webViewBridge = [[SCWebViewBridge alloc] initWithWebView:self.webView];
[self.webViewBridge loadLocalHtml:@"sample" bundle:[NSBundle mainBundle] error:nil];
Supported Argument Primitive Types
Javascript | Objective-C |
---|---|
Number | NSNumber |
String | NSString |
Date | NSDate |
null |
NSNull |
true and false |
NSNumber |
Array | NSArray |
Object | NSDictionary |
How to Use
- Copy SCWebViewBridge folder into your project
- Include SCWebViewBridge.js in HTML/JS
- See SCWebViewBridgeDemo/sample.html for example of calling Objective-C from Javascript
- See SCWebViewBridgeDemo/ViewController for example of initializing SCWebViewBrdige and calling Javascript from Objective-C
License
SCWebViewBridge, and all the accompanying source code, is released under the MIT license