Including App Resources – RubyMotion

Including App Resources

So far, we’ve just been shipping code with our apps, but most projects require a variety of resources: images, sounds, or just raw data. RubyMotion has pretty seamless methods for including each of these, so let’s take look.

Bundling Icons

Dribbble-worthy or not, your app’s icon will appear in many different locations and on varying resolutions: iPhones, iPads, iTunes, and maybe more in the future. To preserve the icon’s visual integrity, Apple encourages developers to ship multiple versions of the icon with every app.

To include your icon files, simply add them to the resources folder. They can have any name, but being descriptive is never a bad idea (such as Icon-72 and Icon-144). Finally, set the icons array of our app in the Rakefile to contain these filenames:

 Motion::Project::App.setup ​do​ |app|
 # ...
  app.icons = [​"Icon-72.png"​, ​"Icon-144.png"​, ...]
 end

Upon your next rake, the simulator’s home screen should be updated with the proper icon.

By default, iOS will apply a curved shine effect on top of your icon, just like the icons for iTunes and the App Store. This might be desired behavior, but if it’s not, then it’s easy to disable. Simply set the prerendered_icon flag to true in the setup block.

 Motion::Project::App.setup ​do​ |app|
 # ...
  app.prerendered_icon = ​true
 end

That’s about it for RubyMotion and app icons. For a complete list of resolutions and more design advice, refer to Apple’s Human Interface Guidelines at the following URL:

http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/mobilehig/IconsImages/IconsImages.html

File Resources

There are more resources than just icons. Most apps have customized interfaces or graphics, and we need to load those from images; you may even need to use specific fonts or some sort of raw data. RubyMotion provides an elegant solution for bundling these files with your app: simply put them in the resources folder.

Any files included in resources will be bundled with your app and accessible in your code. For example, adding a facepng file allows us to create a UIImage instance with UIImage.imageNamed("face"). All supported ttf and otf fonts in resources will be included properly in your app and allow you to use the UIFont.fontWithName("MyFont", size:20) method.

If you want to grab the contents of an arbitrary file like datatxt, we need to grab its path.

 path = NSBundle.mainBundle.pathForResource(​"data"​, ofType​:"txt"​)
 data = NSData.dataWithContentsOfFile(path)

You can then transform the NSData instance as necessary.

Configuration Properties with Info.plist

Want to know a secret? Most of the app properties in the Rakefile and displayed with rake config are actually stored in one central data structure: Infoplist. RubyMotion generates this file behind the scenes using those nifty helper methods; however, the app settings are not exhaustive of the possible properties. Occasionally, we may need to edit Infoplist manually.

Infoplist stores information in a Hash-like structure, which is how RubyMotion gives us more direct access to the file. For example, if we wanted to assign our app a custom URL scheme, we would edit the CFBundleURLTypes property:

 Motion::Project::App.setup ​do​ |app|
 # ...
  app.info_plist[​'CFBundleURLTypes'​] = [
  {
 'CFBundleURLName'​ => ​'com.mycomapny.myapp'​,
 'CFBundleURLSchemes'​ => [​'myapp'​]
  }
  ]
 end

With that, our app will now open if the device is opened to a myapp:// URL. You can also output app.plist to view or debug the complete list of properties.

For a complete list of valid properties in Infoplist, consult Apple’s documentation at the following URL:

https://developer.apple.com/library/mac/documentation/General/Reference/InfoPlistKeyReference/Introduction/Introduction.html#//apple_ref/doc/uid/TP40009247