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 CILS 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 cocould 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 implements 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 CILS 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 functionvoid 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 nameid 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