Thursday, January 6, 2011

Badges on UIBarButtonItems

Badges in iOS are the de-facto way to communicate new/unread information to users.  The SDK allows for setting badges on tab bar items and on the main .app bundle icon.  But what if you want to set a badge on a toolbar item?  There is no official way to make a badge on a toolbar item, even though you as a developer may want to incorporate a badge.

Introducing BarButtonBadge.h, the custom class to easily set a badge on a UIBarButtonItem.  The usage is simple and understandable.

UIBarButtonItem *item = [BarButtonBadge barButtonWithImage:image
                              badgeString:string
                           atRight:BOOL
                           toTarget:target
                     action:action];

The class method accepts 5 mandatory parameters.

  1. UIImage *buttonImage -  The one limitation of the class is that it only produces barButtonItems that contain an icon/picture.  Due to the unpredictable width of a barButtonItem with text, the class does not support a barItem that has a title.  So, use a picture instead.  The barItem will render at 30x30 so a square image is best.
  2. NSString *badgeString - The text you want displayed inside the badge icon.  Can be as simple as @"1" or as elaborate as @"Unread News! Touch here!"
  3. BOOL atRightSideOfScreen - If the barItem is destined to be on the right hand side of the screen, set to YES and the badge icon will be displayed on the left side of the barItem.  Otherwise, it might not be visible on the screen.
  4. id target - The class which will receive the message that your button was tapped
  5. SEL action - The selector message which will be sent to the target
The class returns an autoreleased UIBarButtonItem, so please retain it immediately.

This class comes bundled with another class that is not authored by me, called CustomBadge.  CustomBadge does all the heavy lifting with rendering badges on screen, and is very effective.  The license for CustomBadge is included inside the file.  I have no claims to CustomBadge and do not offer it as my own creation.  BarButtonBadge extends functionality of CustomBadge only.  For this reason, both  classes must be copied into your project in order to work correctly.

You can download a sample project here which contains the necessary custom classes.

Saturday, January 1, 2011

Find me on Twitter @iosiddqd

I've joined twitter.  Follow me @iosiddqd!

Feedback Form: Easy to use in app email!

If you would like to know information about the devices your customers are using, this feedback form class will be helpful to you.  I've received several feedback emails from people, and it's been good to know what OS version the customer is running as well as the version of the app that person is using.

To use this class, you must drag and drop the FeedbackFormController.h/.m files into your project and add the MessageUI.framework to your project.  The classes themselves are documented with comments to make it easy to understand.

Three things:  First, your calling class must be a subclass of UIViewController!  Second, the UIViewController that will be the FeedbackForm parent needs to implement this delegate method:

-(void)messageControllerDismissed:(FeedbackFormController *)controller
 Finally, in order to initialize the class you must use this method:
-(id)initWithDelegate:(id)delegate
 and use your parent UIViewController instance as the parameter. The last part is easy! To send the mail, use the method
-(void)sendFeedbackEmail

Download a sample project here.

JANews.h - Class to download plain-text news

Here's a simple class that can help a developer keep in touch with users. By posting a specially formatted plain-text file online and point this class to the URL, your app can download a custom news message to display in your app along with a URL to direct the user to.

The formatting of your text file is as follows:
A simple news announcement goes here, in front of two hash marks, and a link goes after the hash marks, like so##http://www.google.com
Save something like that in a plain-text, non-rich .txt file with no HTML markup, and post it online with a public link. I suggest Dropbox.

To configure the class, change line 17 in JANews.h to point to the URL of your text file.
#define JANewsURL @"YOUR_PLAIN_TEXT_HTTP_URL_HERE"
Be sure to register any interested class with the NSNotificationCenter with the name @"JANewsDidReceiveNews". You will need to use your registered selector to check that the incoming object is not NSError, because the class sends an error object if the connection fails.


There are methods to serialize downloaded news to device flash memory.  Refer to the .h and .m files for more documentation.


The class can be downloaded from MediaFire!