简体   繁体   English

ARC通过C ++抽象层进行的Objective-C委派

[英]ARC Objective-C delegation through a C++ abstract layer

I'm programming simple cross platform C++ layer (dylib) that gets implemented by Objective-C. 我正在编程由Objective-C实现的简单跨平台C ++层(dylib)。 The parts that are platform specific are included through platform macros. 通过平台宏包括特定于平台的部分。

The NSUserNotificationCenter requires the delegation pattern for handling specific actions, clicking on the notification for example. NSUserNotificationCenter需要委托模式来处理特定的操作,例如单击通知。 The issue I'm facing is that as soon as I execute send , the notification is sent but the instance unloads right after that. 我面临的问题是,一旦我执行send ,便会发送通知,但此后实例立即卸载。 Thus the onclick notification action never gets called (didActivateNotification) instead it crashes for a bad pointer. 因此,onclick通知操作永远不会被调用(didActivateNotification),相反它会因指针错误而崩溃。 How can I make this work? 我该如何进行这项工作?

Note: SomeObjectiveC.mm is a class located in my Application. 注意: SomeObjectiveC.mm是位于我的应用程序中的类。 AppleUserNotificationManager.m is initialized by NotificationManager+apple.mm and both are located in my Dylib. AppleUserNotificationManager.mNotificationManager+apple.mm初始化,并且都位于我的Dylib中。

SomeObjectiveC.mm 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 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 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

Used self pointer before allocated? 在分配之前使用了self指针?
Could below change fix the problem? 下面的更改可以解决问题吗?

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

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

    return self;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM