Can't receive notification from Camera Extension(Swift) to observer application (obj-c++)

Both the extension and the receiving application are in the same app group.

I can't find the issue. It doesn't seem to be a problem with entitlements. Maybe an issue with the string formatting/conversion? Maybe I am not allowed to send distributed center notifications from the camera extension?

I am sending the notification calling:

  func notifyChangeInUsage() {
      os_log("Notifying the virtual camera change in usage", log: cdsLog, type: .info) // this is logged
      DistributedNotificationCenter.default().postNotificationName(NSNotification.Name("VirtualCamUsageChanged"), object: nil, userInfo: nil, deliverImmediately: true)
  }

And receiving it in the other end, subscribing with

std::string notification = "VirtualCamUsageChanged"
[mObserverClassInstance subscribe:@(notification.c_str())];

where subscribe is the following method, which is tested to be working.

- (void)subscribe:(NSString *)notification {
  [[NSDistributedNotificationCenter defaultCenter]
             addObserver:self
                selector:@selector(callCallback:)
                    name:notification
                  object:nil
      suspensionBehavior:NSNotificationSuspensionBehaviorDeliverImmediately];
}

Accepted Reply

I use CMIOObjectAddPropertyListener for this kind of communication (of state changes from the extension to an app).

That said, I'm surprised at this line

[mObserverClassInstance subscribe:@(notification.c_str())];

because I thought @ could only be used with string literals, so you could write

[mObserverClassInstance subscribe:@"VirtualCamUsageChanged"];

I've never seen @ used with a run-time expression.

Replies

I use CMIOObjectAddPropertyListener for this kind of communication (of state changes from the extension to an app).

That said, I'm surprised at this line

[mObserverClassInstance subscribe:@(notification.c_str())];

because I thought @ could only be used with string literals, so you could write

[mObserverClassInstance subscribe:@"VirtualCamUsageChanged"];

I've never seen @ used with a run-time expression.

xvallsplnmg wrote:

Maybe I am not allowed to send distributed center notifications from the camera extension?

You’re allowed to send them, but you have to understand their scope. There’s a separate distributed notification centre for each user login context, and your CMIO extension isn’t run as the same user as your app.

My experience with CMIO extensions is that it’s best to use CMIO-specific IPC, as suggested by ssmith_c.


ssmith_c wrote:

I've never seen @ used with a run-time expression.

It’s unusual, but it definitely works. And it’s not just strings. It works with NSNumber as well. For example:

NSInteger recordCount = 123;
BOOL success = recordCount != 0;
NSDictionary * response = @{
    @"success": @(success),
    @"recordCount": @(recordCount),
};
NSLog(@"response: %@", response);

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thanks!

Where can I find proper documentation on how to use the CMIOObjectAddPropertyListener/ListenerBlock methods? I Can only find the signatures in the Developer docs: https://developer.apple.com/documentation/coremediaio/1415875-cmioobjectaddpropertylistenerblo?changes=_7_4_6&language=objc.

Do I have to call notifyPropertiesChanged on change? https://developer.apple.com/documentation/coremediaio/cmioextensiondevice/3915834-notifypropertieschanged