Open iPhone SDK: Adding Application Badges

If you've used the iPhone or iPod touch for any time, you've likely seen the small red badges that appear over applications on the home screen. These might indicate the number of missed phone calls or unread emails that accumulated since the user last opened Phone or Mail.
There are actually two ways to go about badging applications: one, an extremely simple UIApplication call, the other a slightly more involved tunneling into UIKit. To set an application badge from within the program itself, use setApplicationBadge:. Pass it an NSString as its argument, limiting the string size to 4 or 5 characters at most. For example, you could badge an application with the 3-letter abbreviation for the current month:
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0];
    NSString *caldate = [[now 
            dateWithCalendarFormat:@"%b" 
            timeZone:nil] description];
    [self setApplicationBadge:caldate];

To remove an application badge, pass the empty string, i.e. @””. This removes any existing badge from the icon. If you want an “empty” badge, pass it a space character instead, @” “.
The problem with the UIApplication approach is that to use it you must place your requests directly from the application. And, since many live updates are the results of background daemons, you may want to badge icons from outside the application itself.
The following utility source relies on dynamic linking. Reverse engineering the UIKit framework revealed how the setApplicationBadge worked. It calls SBSetApplicationBadge with a string and an application identifier. As a rule, dynamic linking isn't an approach I generally endorse for day-to- day programming. In this case, there's a compelling need to allow badging by client daemons.
#import <CoreFoundation/CoreFoundation.h>
#import <Foundation/Foundation.h>
#import <UIKit/CDStructures.h>
#import <UIKit/UIWindow.h>
#import <UIKit/UIView-Hierarchy.h>
#import <UIKit/UIHardware.h>
#import <UIKit/UIKit.h>
#import <UIKit/UIApplication.h>
#import <UIKit/UITextView.h>
#import <UIKit/UIView.h>
#include <unistd.h>
#include <dlfcn.h>

void usage(char *appname)
{
   printf("%s application-name (badge)/n", appname);
}

// Invoke the SBSetApplicationBadge function
void badge(char *appid, char *badgeText)
{
   mach_port_t *uiport;
   uiport = _UISpringBoardServerPort();
   void *uikit = 
      dlopen("/System/Library/Framework/UIKit.framework/UIKit", 
                   RTLD_LAZY);
   int (*badge)(mach_port_t* port, char* applicationID, char* text) = 
                     dlsym(uikit, "SBSetApplicationBadge"); 
   badge(uiport, appid, badgeText);
}

// Recover the Application ID from the core App name
id appid (id appname)
{
    NSString *plpath = [NSString stringWithFormat:
           @"/Applications/%@.app/Info.plist", appname];
    id dict = [NSDictionary dictionaryWithContentsOfFile:plpath];
    if (dict) return [dict objectForKey:@"CFBundleIdentifier"];

    plpath = [NSString stringWithFormat:
           @"/Widgets/%@.app/Info.plist", appname];
    dict = [NSDictionary dictionaryWithContentsOfFile:plpath];
    if (dict) return [dict objectForKey:@"CFBundleIdentifier"];

    return NULL;
}

int main(int argc, char **argv)
{
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   if (argc < 2) {usage(argv[0]); exit(-1);} 

   id app = appid([NSString stringWithCString:argv[1]]);
   if (!app) {
     printf("Error: Could not find application for %s/n", argv[1]);
     exit(-1); }

   if (argc == 2) 
      badge([app cString], ""); 
   else
      badge([app cString], argv[2]);

   [pool release];
}


Categories

좋은 웹페이지 즐겨찾기