9

I have UI Tests target for testing MyApp. To test specific MyApp conditions I need to post notifications from UI Test target to MyApp target. To post notification from UI Test target function I am using this:

NSNotificationCenter.defaultCenter().postNotificationName(name, object: nil, userInfo: aUserInfo)

It looks that this notification never reaches observer from UI Test target, but it works fine when posting this notification from MyApp target.

How to post notification from UI Target to MyApp target?

Using Xcode 7.

5
  • Are you calling addObserver where you want to listen? Are you sure the addObserver is being called before postNotification?
    – picciano
    Nov 10, 2015 at 15:32
  • Posting notification from MyApp target works fine, but when posting from UI Tests target dose't. I updated question as well.
    – Ramis
    Nov 11, 2015 at 7:24
  • 1
    Off topic, but it is good practice (or convention?) to send self as the object argument. Nov 11, 2015 at 7:28
  • The opposite is also true (for my case at least): observer from UI Test target cannot see notifications posted from main app, but see notifications posted from inside test.
    – Cemen
    Jan 12, 2016 at 9:33
  • Quick question, would we be able to use sockets to send / receive data here without entitlements? Jan 24, 2017 at 0:46

3 Answers 3

8

Have similar problem (trying to ensure NSNotification is being posted after certain UI action). Did small research on this.

NSNotification not being received because UI test and app are running in different processes. NSNotification cannot go through process bounds, and NSDistributedNotificationServer is not available on iOS. So, currently there is no default and easy way to post NSNotifications between UI test suite and app instance.

However, there is some ways to communicate between processes, and maybe write small wrapper or even NSDistributedNotificationCenter iOS surrogate for testing purposes. Check out this great article from Realm: https://academy.realm.io/posts/thomas-goyne-fast-inter-process-communication/

3

I wanted to use UI Testing to test for memory leaks and to do this I wanted to get informed in the UI test case when ever a view controller's deinit is called. So I came up with this to provide the IPC mechanism:

/**
  Provides simple IPC messaging. To use this class you need to include     
  #include <notify.h>
  in your bridging header. (Ab)uses UIPasteboard for message delivery, ie. 
  should not be used in production code.
*/
public class SimpleIPC {
  /// Event notification name for libnotify.
  private static let notifyEventName = "com.foo.SimpleIPC"

  /// libnotify token.
  private var token: Int32 = 0

  /// Starts listening to the events
  public func listen(callback: (String? -> Void)) {
      notify_register_dispatch(SimpleIPC.notifyEventName, &token, dispatch_get_main_queue()) { token in
          callback(UIPasteboard.generalPasteboard().string)
      }
  }

  public class func send(message: String) {
      UIPasteboard.generalPasteboard().string = message
      notify_post(SimpleIPC.notifyEventName)
  }

  deinit {
      notify_cancel(token)
  }
}

It uses a combination of libnotify and UIPasteboard for the notification + data delivery. Usable for 1-way communication as is, for 2-way either make the payload include a sender token or use 2 instances with parametrized libnotify event names.

  • Matti
1
  • How would the 2way work over here? I've been having problems setting it up. What exactly the sender token here? Jan 7, 2017 at 0:02
0

Is this for a Mac app or an iOS app? For a Mac app you can use NSDistributedNotificationCenter as such.

In your subscriber:

NSDistributedNotificationCenter.defaultCenter().addObserver(object, selector: #selector(ObjectsClass.myFuncWithoutParentheses), name: "uniqueString", object: nil)

In your publisher:

NSNotificationCenter.defaultCenter().postNotificationName("uniqueString" object:nil)

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.