0

我有一组 c++/obj c 文件来为 Growl(即 Obj C)创建一种 C++ 包装器,但是我被困在一个部分。我需要在我的 Obj C 类中设置一个 Growl Delegate 以便调用注册。

这是我的.mm

#import "growlwrapper.h"

@implementation GrowlWrapper
- (NSDictionary *) registrationDictionaryForGrowl {
    return [NSDictionary dictionaryWithObjectsAndKeys:
            [NSArray arrayWithObject:@"Upload"], GROWL_NOTIFICATIONS_ALL,
            [NSArray arrayWithObject:@"Upload"], GROWL_NOTIFICATIONS_DEFAULT
            , nil];
}
@end

void showGrowlMessage(std::string title, std::string desc) {
    std::cout << "[Growl] showGrowlMessage() called." << std::endl;
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    [GrowlApplicationBridge setGrowlDelegate: @""];
    [GrowlApplicationBridge
        notifyWithTitle: [NSString stringWithUTF8String:title.c_str()]
        description: [NSString stringWithUTF8String:desc.c_str()]
        notificationName: @"Upload"
        iconData: nil
        priority: 0
        isSticky: YES
        clickContext: nil
    ];
    [pool drain];
}

int main() {
    showGrowlMessage("Hello World!", "This is a test of the growl system");
    return 0;
}

和我的.h

#ifndef growlwrapper_h
#define growlwrapper_h

#include <string>
#include <iostream>
#include <Cocoa/Cocoa.h>
#include <Growl/Growl.h>

using namespace std;

void showGrowlMessage(std::string title, std::string desc);
int main();

#endif

@interface GrowlWrapper : NSObject <GrowlApplicationBridgeDelegate>

@end

现在你可以看到我[GrowlApplicationBridge setGrowlDelegate: @""];被设置为一个空字符串,我需要将它设置为一些东西,以便registrationDictionaryForGrowl被调用,目前没有被调用。

但我不知道该怎么做。有什么帮助吗?

4

1 回答 1

0

您需要创建一个实例GrowlWrapper并将其作为委托传递给该setGrowlDelegate:方法。您只想在应用程序中执行此操作一次,因此每次调用时都设置它showGrowlMessage并不理想。您还需要保留对此的强烈引用,GrowlWrapper以便在完成后释放它,或者在使用 ARC 时它保​​持有效。因此,从概念上讲,您在启动时需要以下内容:

growlWrapper = [[GrowlWrapper alloc] init];
[GrowlApplicationBridge setGrowlDelegate:growlWrapper];

在关机时:

[GrowlApplicationBridge setGrowlDelegate:nil];
[growlWrapper release];    // If not using ARC
于 2012-07-21T16:53:29.740 回答