detecting iphone/ipad in rubymotion
I just found this post in my drafts pile; might be a little old by now, but here goes.
Most standard texts on iOS programming say to use the following code to detect whether you’re running on an iPhone or an iPad:
BOOL iPad = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
Well, that won’t work in RubyMotion because
UI_USER_INTERFACE_IDIOM
is a C macro - meaning that it gets
handled in the preprocessor and never gets turned into a symbol in the
compiled libraries. That’s necessary for the RubyMotion build system
to see it and create the proper Ruby objects to mirror it.
But, if you dig down into the C header files deep enough, you find:
/* The UI_USER_INTERFACE_IDIOM() macro is provided for use when
deploying to a version of the iOS less than 3.2. If the earliest version of
iPhone/iOS that you will be deploying for is 3.2 or greater, you may use
-[UIDevice userInterfaceIdiom] directly.
*/
#define UI_USER_INTERFACE_IDIOM() \
([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? \
[[UIDevice currentDevice] userInterfaceIdiom] : UIUserInterfaceIdiomPhone)
The macro’s only needed for iOS version 3.2 and earlier - and really, why bother?
So, the Ruby code that works for this is:
if UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad
storyboard = UIStoryboard.storyboardWithName "iPad-Storyboard", bundle: NSBundle.mainBundle
else
storyboard = UIStoryboard.storyboardWithName "iPhone-Storyboard", bundle: NSBundle.mainBundle
end