Growl是一个非常好用的通知框架,可以帮助你轻松制作各种非常酷的事件通知效果。这个教程教你如何简单地在自己的应用程序里加入Growl通知。请阅读正文。


 

在程序里加入growl通知很容易,在growl.info下载SDK,复制Growl.framework到你的程序包中。
 
选一个类和growl交互,可以放在AppController里。
 
导入<Growl/Growl.h>
 
将GrowlApplicationBridge设置为托管:
 
[GrowlApplicationBridge setGrowlDelegate: self];
 
这样会调用registrationDictionaryForGrowl托管消息,返回带有两个数组的字典结构,这个字典包括了你要发送通知的名字。
 
- (NSDictionary *) registrationDictionaryForGrowl
{
    NSArray *notifications;
    notifications = [NSArray arrayWithObject: g_timesUpString];
           
    NSDictionary *dict;
           
    dict = [NSDictionary dictionaryWithObjectsAndKeys:
                            notifications, GROWL_NOTIFICATIONS_ALL,
                            notifications, GROWL_NOTIFICATIONS_DEFAULT, nil];
           
    return (dict);
           
}
 
然后用下面代码发送通知:
 
[GrowlApplicationBridge notifyWithTitle: @"Woop! Time has expired!"
                                                                 description: @"You have been waiting for 37 minutes"
                                                  notificationName: g_timesUpString
                                                                    iconData: nil
                                                                    priority: 0
                                                                    isSticky: NO
                                                           clickContext: nil];
 
你可以阅读SDK文档获取更多功能的解释,不过基本思路就是这样的。