Important: Please read the Qt Code of Conduct -

Converting NSNotificationCenter events to Signals

  • I am integrating a third-party framework which implements events via the NSNotificationCenter. The framework is written in Objective-C. I was thinking I needed a way to capture to capture the events and convert them to signals that can be picked up by my iOS application, which is written in C++. Is that correct, or is there a more direct way to do it?

    I created a wrapper around the Objective-C framework calls which allow me to interface via C++, but the wrapper does not derive from QObject, so I can't emit signals directly (.h/.mm combo). Can I register for the notification events within my C++ code?

  • To provide a little more context, I would like a specific class in my main application to receive NSNotificationCenter events from the third-party framework. This class will handle the event and emit the necessary signals as defined in my abstract base class interface, to the main application.

  • Lifetime Qt Champion


    Why not make the wrapper a QObject ?

  • @SGaist I actually did do that in the end. I am still left with the problem of registering my handlers for NSNotificationCenter events. I am having trouble defining "self". I do not know how to define "Self" as an instance of my class. I found an example in Git:
    But I don't want to be dealing with application level events, only events generated by my third party library.

    Do I have to define a delegate?

  • I believe I have solved my problem. I haven't tested it yet since the first call to the third-party library throws an exception. I am, however able to register my notification handler and confirm that its being executed

    @interface EventDelegate : NSObject //UIResponder
      UtasCommunicationHandler* m_comm_handler;
    @implementation EventDelegate
    - (id) initWithObject:(UtasCommunicationHandler*) comm_handler
      self = [super init];
      if (self)
        m_comm_handler = comm_handler;
        [[NSNotificationCenter defaultCenter]
      return self;

  • I solved the exception and have confirmed that I am receiving and handling NSNotificationCenter events in my Object-derived class.

  • Lifetime Qt Champion

    Great !

    What was the problem ?

  • @SGaist The handler I defined for the sample event was misspelled in the implementation. Once I corrected it, the events worked fine. I added three more after that and they all worked

  • Lifetime Qt Champion

    So you got something like "unrecognized selector"

  • @SGaist Actually no. The compiler never complained. It just caused an unhandled exception to be thrown, which appeared to be caused by the call which set up the notification. If I had looked at the exception details, I might have seen the message you suggested, but I noticed the error, fixed it and the problem went away.

  • Lifetime Qt Champion

    The important point is that you could solve it :)

Log in to reply