Titanium使用Hyperloop使用NSNotificationCenter

时间:2018-09-10 12:21:40

标签: ios appcelerator appcelerator-mobile appcelerator-hyperloop

我正在尝试使用Titanium应用程序中的hyperloop监视UIScreenCapturedDidChangeNotification的屏幕录制状态。我已经尝试了一段时间,但找不到在hyperloop中使用NotificationCenter或addObserver的任何示例。基本上,我试图将下面的本机代码带入超级循环中,但是没有运气:

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

这是我的尝试不起作用:

//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);

1 个答案:

答案 0 :(得分:1)

尝试更改一些内容:-

  1. UIScreenMonitor.addMethod中的选择器名称需要冒号,例如。 'handleScreenRecording:'
  2. 也在您的“ addObserverSelectorNameObject”函数调用中,选择器需要一个冒号,例如。 'handleScreenRecording:'
  3. 实例化'new'类的新实例时,添加一个UIScreenMonitor关键字。
  4. 如果没有,
  5. screenMonitor也需要持续存在(例如,不是本地变量)。

希望这行得通。