C语言 在FSEvent回调中访问app委托函数



我已经在app委托文件中创建了这个函数。我想在回调中调用app委托的函数

有什么办法吗?

-(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
}

由于fsevenstreamcreate仅在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和类型转换。回调在objective之外,你不能使用Cocoa,只能使用CFString和其他Core Foundation类型。不仅阅读文档,而且查看框架中的f7ts .h也很有帮助(任何函数上的上下文菜单,然后跳转到定义:有比文档中更多的内容需要阅读)。根据你的发行版本,还可以使用App Store沙盒和安全范围书签。

最新更新