Open iPhone SDK: Adding Application Badges
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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Access Request, Session and Application in Struts2If we want to use request, Session and application in JSP, what should we do? We can obtain Map type objects such as Req...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.