I am writing an application for Mac OS - browser on WebKit to use for the some site on WebGL. All is ready, the application correctly displays normal HTML sites, but WebGL doesn't work. How can enable WebGL in my app?
Once you have a WebView, there is an undocumented method which can enable WebGL:
WebPreferences *p = [webView preferences];
if ([p respondsToSelector:@selector(setWebGLEnabled:)]) {
[p setWebGLEnabled:YES];
}
In this example I have protected it with respondsToSelector:
to ensure the code will not abort if later versions remove this specific option.
Note that I understand that an application containing code using undocumented interfaces may be rejected if submitted to Apple's Mac App Store.
Another option is to use a different embedded renderer which officially supports WebGL (where Apple's WebKit as demonstrated in Safari only has it as a developer option, presumably intended to be experimental). Since both Firefox and Chrome support WebGL, have a look at Gecko and Chromium Embedded Framework. (Note: I have not been able to confirm whether whether embedded Gecko supports WebGL.)
In Mavericks, this is the correct code.
WebPreferences *preferences = [_webview preferences];
if([preferences respondsToSelector:@selector(setWebGLEnabled:)]){
[preferences performSelector:@selector(setWebGLEnabled:) withObject:[NSNumber numberWithBool:YES]];
}
You need to call performSelector otherwise you'll get a compile error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With