2016-09-18 7 views
0

Objective-Cによって実装される単純なクロスプラットフォームのC++レイヤー(dylib)をプログラミングしています。プラットフォーム固有の部分は、プラットフォームマクロを介して含まれています。ARC C++抽象レイヤーを介したObjective-C委譲

NSUserNotificationCenterでは、通知をクリックするなど、特定の操作を処理するための委任パターンが必要です。私が直面している問題は、sendを実行するとすぐに通知が送信されますが、インスタンスはその直後にアンロードされるということです。したがって、onclick通知アクションは呼び出されない(didActivateNotification)代わりに、悪いポインタのためにクラッシュします。どうすればこの作品を作れますか?

注: SomeObjectiveC.mmは、私のアプリケーションにあるクラスです。 AppleUserNotificationManager.mNotificationManager+apple.mmで初期化され、両方とも私のDylibに配置されています。

SomeObjectiveC.mm

Notification *notification = new Notification; 
notification->setTitle("Foo bar notification"); 
notification->setMessage("Hello world!"); 

NotificationManager *notificationManager = new NotificationManager; 
notificationManager->send(notification); 

NotificationManager + apple.mm

#include "notificationManager+apple.hpp" 

bool NotificationManager::send(Notification *notification) 
{ 
    AppleUserNotificationManager *notificationManager = [[AppleUserNotificationManager alloc] init]; 

    NSString *title = [NSString stringWithUTF8String:notification->getTitle().c_str()]; 
    NSString *message = [NSString stringWithUTF8String:notification->getMessage().c_str()]; 
    if (notification->getSoundName().empty()) { 
     [notificationManager sendWithTitle:title andMessage:message]; 
    } 

    NSString *soundName = [NSString stringWithUTF8String:notification->getSoundName().c_str()]; 
    [notificationManager sendWithTitle:title andMessage:message andSound: soundName]; 

    return true; 
} 

AppleUserNotificationManager.m

#import "AppleUserNotificationManager.h" 

@implementation AppleUserNotificationManager 

@synthesize userNotification; 

- (id)init 
{ 
    [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate: self]; 
    userNotification = [[NSUserNotification alloc] init]; 

    self = [super init]; 
    return self; 
} 

/** 
* @param NSUserNotificationCenter center 
* @param NSUserNotification notification 
* 
* @return bool 
*/ 
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification{ 
    return YES; 
} 

/** 
* @param NSUserNotificationCenter center 
* @param NSUserNotification notification 
*/ 
- (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification 
{ 
    NSString *notificationText = [notification informativeText]; 
    NSString *urlRegEx = @"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+"; 

    NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx]; 
    if ([urlTest evaluateWithObject:notificationText]) { 
     [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:notificationText]]; 
    } 
} 

/** 
* @param NSString title 
* @param NSString message 
* @param NSString soundName 
*/ 
- (void)sendWithTitle:(NSString *)title andMessage:(NSString *)message andSound:(NSString *)soundName{ 
    userNotification.title = title; 
    userNotification.informativeText = message; 
    userNotification.soundName = soundName; 

    [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification: userNotification]; 
} 

@end 

答えて

0

割り当て前にselfポインタを使用しましたか?
変更すると問題が解決しますか?

- (id)init 
{ 
    self = [super init]; 

    [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate: self]; 
    userNotification = [[NSUserNotification alloc] init]; 

    return self; 
} 
関連する問題