Although obviously the point of Enyo is to be multiplatform, there come times when we need to use platform-specific codes to achieve some things. I've been doing some research into how to determine what platforms we are on, and would appreciate any more tips from anyone else.
To determine if we are in webOS, we can check window.PalmSystem: if(window.PalmSystem) webOSFunction();
To determine if we are running in Phonegap, we can check PhoneGap: if(PhoneGap) phoneGapFunction();
PhoneGap has a "device.platform" property that should return an operating system name, however, it is perhaps less than reliable: it's returns are not completely valid, or necessarily documented -- it returns "iPhone" for all iOS devices, and on several Blackberry devices, it apparently returns a version number of the operating system rather than the name of the system.
0 • •
Comments
- Spam
- Abuse
0 • Off Topic Insightful •- Spam
- Abuse
0 • Off Topic Insightful •if (PhoneGap)since (I guess) the auto caster has a problem with undeclared variables. I useif (typeof PhoneGap != 'undefined'). Also you can useif (typeof chrome != 'undefined')to check if the chrome extension api is available.- Spam
- Abuse
0 • Off Topic Insightful •Of course, please add if anyone has ways to specifically determine iOS, Android, Blackberry, possibly as well as determining other metrics that might be really important to cross platform things. I'll continue adding as I find them.
- Spam
- Abuse
0 • Off Topic Insightful •deviceobject is undefined, and therefore accessing theplatformproperty of it throws an exception.If that Exception is thrown, we assume no PhoneGap and check for the PalmSystem object. If it's there, then we know we're on webOS.
I assume that if we're not using PhoneGap or webOS, that we're running in the browser.
So far everything seems to be working. There may be a more elegant solution, but this works for me since I only include PhoneGap if I'm building for iOS or Android.
- Spam
- Abuse
0 • Off Topic Insightful •if(device && device.platform) { xxx } else { yyy }
I wonder if there's any reason to do it one way or the other in Javascript.
Anyway, on topic, I wonder if there's a way to determine more specifically, such as in the case of device.platform returning a version # instead of a name, as is described in the docs.
- Spam
- Abuse
0 • Off Topic Insightful •An exception would have to walk the stack trace and gather memory dumps for the exception handler, a very slow process indeed.
I would assume that try/catch is much more expensive even in Javascript vs. just handling it properly This should almost work, I sometimes encounter a problem with checking 'device == undefined' unless I set it to null during initialization before PhoneGap runs.
- Spam
- Abuse
0 • Off Topic Insightful •- Spam
- Abuse
0 • Off Topic Insightful •Many scripting languages that I have used do have exception handling, but it's usually quite slow compared to just checking it. Doesn't look like that's always possible here, though, unless you know the vars are set somewhere.
- Spam
- Abuse
0 • Off Topic Insightful •device = {};- Spam
- Abuse
0 • Off Topic Insightful •- Spam
- Abuse
0 • Off Topic Insightful •device = device || {};- Spam
- Abuse
0 • Off Topic Insightful •The problem sugardave is that just accessing the 'device' property will crash Enyo.
- Spam
- Abuse
0 • Off Topic Insightful •if (typeof device != "undefined")Also if you check for window.device you wont get an error if it has not been declared.
- Spam
- Abuse
0 • Off Topic Insightful •http://pastebin.com/wkdsDduX
- Spam
- Abuse
0 • Off Topic Insightful •I'm tempted to break OOP and make it a bit more global and use enyo.application.platform over this.platform.
And yup! You're right MaKleSoft, I completely missed your post the first time. Thanks for pointing that out. I wondered how to do that properly.
- Spam
- Abuse
0 • Off Topic Insightful •- Spam
- Abuse
0 • Off Topic Insightful •- Spam
- Abuse
0 • Off Topic Insightful •http://pastebin.com/g1PLaAi0
- Spam
- Abuse
0 • Off Topic Insightful •http://supportforums.blackberry.com/t5/tkb/articleprintpage/tkb-id/browser_dev@tkb/article-id/88
so that might make some changes to the hasMenu() function. Need to figure out if there's a similar thing for other BlackBerries
- Spam
- Abuse
0 • Off Topic Insightful •- Spam
- Abuse
0 • Off Topic Insightful •- Spam
- Abuse
0 • Off Topic Insightful •Also, the var "platform" for the whole object collided with the "platform" string and destroyed it, so I changed that.
Also added a getVersion() function.
http://pastebin.com/uEeByGhZ
- Spam
- Abuse
0 • Off Topic Insightful •http://pastebin.com/Qyp8PyDf
... maybe one of these days I'll get around to getting a github or something .. but, anyway, this adds a "thisObj" parameter to the browser() function, as I discovered that the "bind" implementation I used isn't well supported, and I was using it incorrectly anyway, so I just switched to enyo.bind() .. adds support for webOS using the browser() function, calls BlackBerry's web invoke on WebWorks with or without PhoneGap, and works in Chrome.
Also adds a getReviewURL function which will return the URL to open for the HP App Catalog, Android Market, or iTunes store. To get the iTunes one to work, you'll need to add a new field to your appinfo.json: iTunesAppId, which would be your application id from the Apple Dev Portal.
- Spam
- Abuse
0 • Off Topic Insightful •http://pastebin.com/6ATAiEhH
PhoneGap device detection should work, so long as you make sure you don't call into this before the "deviceready" event.
Rather than directly calling setup(), the first call that you make into any function in the kind will cause it to configure. If you ARE using PhoneGap, but are not waiting until "deviceready" is fired, results are undefined and untested, but the first call to it after deviceready will initialize it.
Added getPlatformName(), identify "webworks" is valid for isBlackBerry(), add useHTMLAudio() boolean function, browser() takes 2 arguments as described above and now works with webOS, Android 4.0, and WebWorks out-of-the-"box", no plugins required .. getReviewURL() as above ..
Also, this one includes a new helper kind: PlatformSound. It works sort of like a cross between the Enyo 1.0 Sound kind, and the PhoneGap Media API. It encapsulates and extends both of them, giving a unified interface to both the HTML5 audio API and the PhoneGap Media API.
This one has been pretty thoroughly tested in webOS, Android CM7, Android CM9, and on the PlayBook 1.0.8 emulator. I haven't touched anything in iOS, it's too much of a pain in the butt to deploy to until I get a personal developer account with Apple.
- Spam
- Abuse
0 • Off Topic Insightful •- Spam
- Abuse
0 • Off Topic Insightful •Thanks, Ben :)
- Spam
- Abuse
0 • Off Topic Insightful •https://github.com/ericblade/EnyoPlatform
Not only Platform detection, but useful tools for cross platform development, encapsulating some UI features and device functions, a browser call that works pretty much everywhere, and adding cross-platform versions of other things, too, such as the PlatformSound component that wraps enyo.Sound allowing for mostly full function for both HTML and PhoneGap audio.
Next project is to see if I can get Dashboard running on PlayBook and Chrome :)
- Spam
- Abuse
0 • Off Topic Insightful •