Deploy WebGL applications as native iOS or Android applications? Deploy WebGL applications as native iOS or Android applications? ios ios

Deploy WebGL applications as native iOS or Android applications?


As an extension to Joris' answer (which appears to be based on the work of Nathan de Vries), the following is the code I needed to enable WebGL within the iOS 5.0 SDK:

Somewhere near the top of your view controller implementation:

@interface UIWebView()- (void)_setWebGLEnabled:(BOOL)newValue;@end

Programmatically creating a UIWebView and enabling WebGL:

UIWebView *webDetailView = [[UIWebView alloc] initWithFrame:mainScreenFrame];id webDocumentView = [webDetailView performSelector:@selector(_browserView)];id backingWebView = [webDocumentView performSelector:@selector(webView)];[backingWebView _setWebGLEnabled:YES];

I created a sample application that demonstrates WebGL running in a iPhone / iPad fullscreen UIWebView, using the WebGL scratchpad site http://glsl.heroku.com as a destination. Be wary that some of those examples there will grind even an iPad 2 to a halt, potentially leading to a hard reboot. The performance there seems to indicate why WebGL is still in an officially unsupported state in mobile WebKit.

Of course, as has been stated, this is not guaranteed to work on future iOS versions and will get your application rejected from the App Store. This is only really useful for hobby work and internal testing.

An example of WebGL running on my iPad 2:

enter image description here


WebKit on iOS actually supports WebGL, as of 4.x (not sure which .x version). It is enabled in the webview used by the iAd framework, all other uses of WebKit (Safari and UIWebView) have WebGL disabled.

It is possible to enable WebGL using private API's (this will not pass the submission process). In your webview subclass:

- (void)setWebGLEnabled:(BOOL)enableWebGL {    UIWebDocumentView* webDocumentView = [self _browserView];    WebView* backingWebView = [webDocumentView webView];    [backingWebView _setWebGLEnabled:enableWebGL];}

(via)

This will at least allow you to start experimenting with WebGL on iOS.

Not sure about WebGL support on Android. Fairly recent comments on the issue in the Android tracker suggest it is not available yet.

A nice support table for WebGL in (mobile) browsers: When can I use WebGL

Best way to go for now seems to be to include your own WebGL enabled version of WebKit in your application wrapper for both iOS and Android.


As the iOS version of WebKit doesn't support WebGL natively, I think you have two options:

  • Implement the WebGL API in the JavaScript context of a WebView yourself by forwarding calls to the native OpenGL via iframe RPC or so. There isn't a clean way of calling (Objective-)C functions from JavaScript on iOS, unfortunately. Performance could be a problem.

  • AOT-Compile or interpret the JavaScript in a third-party runtime, then implement WebGL from there. JIT compilers are disallowed on iOS, so something like V8 won't work. Appcelerator Titanium statically compiles JavaScript as far as I know, and also has an interpreter. You could use that, but you'd still need to implement the WebGL glue yourself.

I'm not aware of any existing WebGL bridges for iOS, so I think you will need to either write it yourself or get someone to do it for you. One problem that might not be possible to overcome is if you use anything other than WebGL to display stuff - e.g. HTML, 2D <canvas>, etc. Combining WebView display with an OpenGL framebuffer is going to be rather tricky.

I don't know much about Android, but considering the rules are more relaxed there, it might be possible to embed a WebGL-compatible browser there.