Hi,
Looking for advice from C++/Obj-C experts, I’m extending the awesome fork of ofxiPhoneWebViewController by @pelayo which had the beginnings of an HTML to OF bridge.
https://github.com/pelayomendez/ofxiPhoneWebViewController
What’s a clean way to pass data? Still new to C++, everything feels like a minefield of memory leaks…
How it works:
In the HTML page you’d use the URL for parameters:
<a href="of://callOFfunction/functionName?foo=bar">Call OF</a>
Here’s the modified ObjC code:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([[request.URL scheme] isEqual:@"of"]) {
if ([[request.URL host] isEqual:@"closeWindow"]) {
delegate->hideView(YES);
delegate->didCloseWindow();
}
if ([[request.URL host] isEqual:@"openInBrowser"]) {
NSString *url = [request.URL query];
NSLog(@"openInBrowser url:%@", url);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: [[[NSString alloc] initWithString: url] autorelease] ]];
}
else if ([[request.URL host] isEqual:@"callOFfunction"]) {
// Here's what I added:
// contains ["/","myFunctionName"]
NSArray *components = [request.URL pathComponents];
// contains "myFunctionName"
string functionName = [[components objectAtIndex: 1] cStringUsingEncoding:[NSString defaultCStringEncoding]];
// contains "foo=bar"
string query = [[request.URL query] cStringUsingEncoding:[NSString defaultCStringEncoding]];
delegate->callExternalFunction(functionName, query);
}
return NO; // Tells the webView not to load the URL
}
else {
return YES; // Tells the webView to go ahead and load the URL
}
}
-
What makes more sense, passing C strings by value or NSString pointers?
-
I’m assuming in C++ you can’t dynamically evaluate members by string, right?
So eventually in the event listener in your OF app you’d have something likeif( args.functionName == "doSomething" ) doSomething( args.query );
-
Maybe using the path instead of the query would be nicer, like of://callOFfunction/functionName/param1/param2
Any suggestions welcome!