Permalink
Browse files

Added NativeBridge for JS -> Obj-C calls

  • Loading branch information...
1 parent 2c2acb4 commit 66bb1aabcaa1913d5b704c97395ff3435803eaed @drpancake committed Mar 12, 2012
Showing with 89 additions and 1 deletion.
  1. +1 −0 JPCanvasViewController.h
  2. +22 −0 JPCanvasViewController.m
  3. +61 −0 NativeBridge.js
  4. +5 −1 canvas.html
View
1 JPCanvasViewController.h
@@ -11,6 +11,7 @@
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
+#import "SBJson.h"
@interface JPCanvasViewController : NSViewController {
@private
View
22 JPCanvasViewController.m
@@ -73,6 +73,28 @@ - (void)webViewDidFinishLoad:(UIWebView *)theWebView
[self execute:js];
}
+- (BOOL)webView:(UIWebView *)theWebView
+ shouldStartLoadWithRequest:(NSURLRequest *)request
+ navigationType:(UIWebViewNavigationType)navigationType {
+
+ NSString *requestString = [[request URL] absoluteString];
+
+ if ([requestString hasPrefix:@"js-frame:"]) {
+ NSLog(@"request : %@", requestString);
+
+// NSArray *components = [requestString componentsSeparatedByString:@":"];
+//
+// NSString *function = (NSString *)[components objectAtIndex:1];
+// NSString *argsAsString = [(NSString *)[components objectAtIndex:3]
+// stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
+// NSArray *args = (NSArray*)[json objectWithString:argsAsString error:nil];
+
+ return NO;
+ }
+
+ return YES;
+}
+
- (UIView *)view
{
return self.webView;
View
61 NativeBridge.js
@@ -0,0 +1,61 @@
+/* ***** BEGIN LICENSE BLOCK *****
+# Copyright 2010 Alexandre Poirot
+#
+# Contributor(s):
+# Alexandre poirot <poirot.alex@gmail.com>
+#
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library. If not, see <http://www.gnu.org/licenses/>.
+#
+# ***** END LICENSE BLOCK *****/
+
+
+var NativeBridge = {
+ callbacksCount : 1,
+ callbacks : {},
+
+ // Automatically called by native layer when a result is available
+ resultForCallback : function resultForCallback(callbackId, resultArray) {
+ try {
+ var callback = NativeBridge.callbacks[callbackId];
+ if (!callback) return;
+
+ callback.apply(null,resultArray);
+ } catch(e) {alert(e)}
+ },
+
+ // Use this in javascript to request native objective-c code
+ // functionName : string (I think the name is explicit :p)
+ // args : array of arguments
+ // callback : function with n-arguments that is going to be called when the native code returned
+ call : function call(functionName, args, callback) {
+
+ var hasCallback = callback && typeof callback == "function";
+ var callbackId = hasCallback ? NativeBridge.callbacksCount++ : 0;
+
+ if (hasCallback)
+ NativeBridge.callbacks[callbackId] = callback;
+
+ var iframe = document.createElement("IFRAME");
+ iframe.setAttribute("src", "js-frame:" + functionName + ":" + callbackId+ ":" + encodeURIComponent(JSON.stringify(args)));
+ document.documentElement.appendChild(iframe);
+ iframe.parentNode.removeChild(iframe);
+ iframe = null;
+
+
+ }
+
+};
+
+
View
6 canvas.html
@@ -3,11 +3,12 @@
with no margins.
Drawing code derived from:
-http://www.williammalone.com/articles/create-html5-canvas-javascript-drawing-app/
+http://jbkflex.wordpress.com/2011/11/28/html5-canvas-paint-app-for-iphone/
-->
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
+ <script type="text/javascript" src="NativeBridge.js"></script>
<style>
body {
margin: 0px;
@@ -84,6 +85,9 @@
function stopPaint() {
buttonDown = false;
+
+ // NativeBridge test
+ NativeBridge.call("didDrawPoints", [[0, 0], [5, 10]]);
}
// Bind events

0 comments on commit 66bb1aa

Please sign in to comment.