iOS 8 Push Notifications in RubyMotion

iOS 8 changed the way apps register for push notifications. In order for your app to register for push notifications successfully on both iOS 7 and 8 you’ll need to check the version number and register differently.

if !Device.simulator?
  if Device.ios_version.start_with? "8"
    settings = UIUserNotificationSettings.settingsForTypes((UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound |           UIRemoteNotificationTypeAlert), categories:nil)
    UIApplication.sharedApplication.registerUserNotificationSettings(settings)
    UIApplication.sharedApplication.registerForRemoteNotifications
  else
    UIApplication.sharedApplication.registerForRemoteNotificationTypes(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)
  end
end

RubyMotion Debugging

One of the nice things about RubyMotion is the REPL. Below is the code I paste in the REPL to get an instance of the current view controller.

vc = UIApplication.sharedApplication.delegate.instance_variable_get("@window").rootViewController.topViewController

You can then access any public properties on the class that are exposed with attr_accessible or by using a custom getter and setter. If you have not specified any properties for your instance variables you can use the instance_variable_get helper to access the internal instance variables.

RubyMotion iOS 5 Appearance API

iOS 5 introduced the appearance API so that developers could customize the appearance of certain UI elements throughout the entire application without subclassing those controls.

For example, to change the appearance of a UINavigationBar you can set the appearance in the app delegate with the following code:

navigationBar = UINavigationBar.appearance
navigationBar.setBackgroundImage(UIImage.imageNamed('navigation-bar-background.png'), forBarMetrics: UIBarMetricsDefault)
navigationBar.setTitleTextAttributes({
  UITextAttributeFont => UIFont.fontWithName('Trebuchet MS', size:24),
  UITextAttributeTextShadowColor => UIColor.clearColor,
  UITextAttributeTextColor => '#2a3284'.to_color
})

I had to use the hashrocket => instead of the new colon notation since using the colon notation resulted in the key constants being turned into symbols.

The .to_color helper is part of the BubbleWrap library. I’ll talk about that library in a separate post.

RubyMotion Launch Image

There is no way to specify a launch image directly in the Rakefile with the standard RubyMotion config options. The launch image is considered an advanced configuration option and can be set using the advanced plist settings technique which is detailed here: Advanced Info.plist Settings.

Basically you just use the app’s info_plist property to set any keys or values necessary in the Info.plist file that RubyMotion does not expose configuration options for.

I added this to my Rakefile to make it happen:

app.info_plist['UILaunchImageFile'] = 'launch'

Here, ‘launch’ is the base name for the images. This means that the filenames would look like this:

launch.png : 320 x 480px
launch@2x.png: 640 x 960px
launch-568h@2x.png : 640 x 1136px