繁体   English   中英

在 FSEvent 回调中访问 app delegate function

[英]Access app delegate function in FSEvent callback

我在应用程序委托文件中创建了这个 function。 我想在回调中调用 function 的应用程序委托。

请问有什么办法吗。

-(void) monitor{
    FSEventStreamRef stream = FSEventStreamCreate(NULL, 
                                                  &feCallback,
                                                  &cntxt, 
                                                  pathsToWatch, 
                                                  kFSEventStreamEventIdSinceNow, 
                                                  1,
                                                  kFSEventStreamCreateFlagWatchRoot );
} 

static void feCallback(ConstFSEventStreamRef streamRef,
                       void* pClientCallBackInfo,
                       size_t numEvents,
                       void* pEventPaths,
                       const FSEventStreamEventFlags eventFlags[],
                       const FSEventStreamEventId eventIds[]) 
{
    NSLog(@"The file changed!"); 
    // need to to call app delegate function
}

由于 FSEventStreamCreate 仅在 macOS 上可用,您可以通过以下方式进行:

    // call this in init of your app delegate
    FSEventStreamContext  cntxt = {0, (__bridge void *)(self), NULL, NULL, NULL};
    // call FSEventStreamCreate as in your code
    // keep a reference to the stream so you can stop and start it later

然后在您的回调中,AppDelegate 将位于:

    YourAppDelegateClass* appDele = (__bridge YourAppDelegateClass*)pClientCallBackInfo; 

FSEvents 的整个机制非常复杂,我们必须在正确的运行循环中安排它并使用 ARC 和类型转换。 回调在 ObjectiveC 之外,您将无法使用 Cocoa,而只能使用 CFString 和其他 Core Foundation 类型。 不仅阅读文档而且查看框架中的 FSEvents.h 也很有帮助(任何函数的上下文菜单,然后跳转到定义:要阅读的内容比文档中的内容更多)。 根据您的发行版,还可以使用 App Store 沙盒和安全范围内的书签。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM