Open iPhone SDK: Adding Application Badges
来源:互联网 发布:音乐下载软件 编辑:程序博客网 时间:2024/06/14 21:11
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 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
- Open iPhone SDK: Adding Application Badges
- IPhone SDK: Application Preferences
- iPhone SDK Sample Code Application Badge
- Adding Open Source 3D Physics to Your iOS Applications (3)Using Bullet in Your iOS Application
- iPhone Open Application Development: Write Native Objective-C Applications for the iPhone
- IOS5 的计算器(depending on Stanford open class : iPad & iPhone application develop 2)
- Adding Windows To Your Console Application
- Adding Tracing Support to the Application
- Adding React to an Existing Application
- Open Settings.app When Button is Tapped in UIAlertView on iPhone SDK [duplicate]
- adding iphone touches to unitygui scrollview
- WIN32: Open a Application
- iPhone Programming: Adding a Contact to the iPhone Address Book
- open iphone Torch
- Iphone Change Application Name
- Localize iPhone Application Name
- Open XML SDK 入门
- Win32 SDK Application
- http 请求报头详解
- 职业生涯
- 获取完整的系统临时文件路径
- 利用C++的stringstream对各种数据类型转换
- E-Mail从入门到精通
- Open iPhone SDK: Adding Application Badges
- 使用Maven构建eclipse web工程
- 动态创建ACCESS数据库
- CE camera GUID
- 窗口切分
- 可拖动的控件
- 搞IT真累啊,开发文档写的乱的一笔,消息名称都没说明......
- C#中数组复制的4种方法
- Dalvik虚拟机简介