iosappceleratorappcelerator-mobileappcelerator-hyperloop

Titanium use NSNotificationCenter using Hyperloop


I am trying to monitor UIScreenCapturedDidChangeNotification for screen recording status using hyperloop in my Titanium app. I've been trying for a while but I couldn't find any examples of using NotificationCenter or addObserver in hyperloop. Basically I am trying to bring the following native code into hyperloop with no luck:

if (@available(iOS 11.0, *)) {
  [[NSNotificationCenter defaultCenter] addObserver:self
                selector:@selector(handleScreenCaptureChange)
   name:UIScreenCapturedDidChangeNotification object:nil];
}

Here is my attempt that is not working:

//Add event listener to monitor screen recording.
var NotificationCenter = require('Foundation/NSNotificationCenter');
var UIScreenMonitor = Hyperloop.defineClass('UIScreenMonitor', 'NSObject');

UIScreenMonitor.addMethod({
    selector : 'handleScreenRecording',
    instance : true,
    arguments : ['NSNotification'],
    callback : function(screen) {
        alert('Screen recording changed: '+UIScreen.mainScreen.isCaptured());
        console.log('Screen recording changed: ',UIScreen.mainScreen.isCaptured(),screen.isCaptured());
    }
});

var screenMonitor = UIScreenMonitor();
NotificationCenter.defaultCenter.addObserverSelectorNameObject(screenMonitor,'handleScreenRecording',UIScreen.UIScreenCapturedDidChangeNotification,null);

Solution

  • Try changing a few things :-

    1. The selector name in your UIScreenMonitor.addMethod needs a colon eg. 'handleScreenRecording:'
    2. Also in your 'addObserverSelectorNameObject' function call the selector needs a colon eg. 'handleScreenRecording:'
    3. Add a 'new' keyword when instantiating a new instance of UIScreenMonitor class.
    4. screenMonitor also needs to persists for the duration that you need it (ie. not a local var) if it is not.

    Hope this works.