CBHFileSystemEventKit
更轻松地监视文件系统事件。
使用
任何使用的起点是 CBHFileSystemWatcher
。它用于描述您想要获取哪些类型的文件系统事件以及您想要在哪里接收它们。当发生符合描述的事件时,将调用回调,并传递一个 CBHFileSystemEvent
,它描述了发生事件的地点和情况。
示例
使用 block 监视目录内容
// [...]
NSString *path = @"/path/to/directory/to/watch";
CBHFileSystemWatcherType type = CBHFileSystemWatcherType_default;
id someContext = nil;
CBHFileSystemWatcher *watcher = [CBHFileSystemWatcher watcherOfPath:path withType:type andBlock:^(CBHFileSystemEvent *event) {
// Do something with the event and someContext.
}];
// [...]
使用 observer 监视目录内容
// [...]
- (void)watchPath
{
NSString *path = @"/path/to/directory/to/watch";
CBHFileSystemWatcherType type = CBHFileSystemWatcherType_default;
SEL selector = @selector(_fileSystemEventOccurred:);
id someContext = nil;
_watcher = [CBHFileSystemWatcher watcherWithObserver:self andSelector:selector ofPath:path withType:type andObject:someContext];
}
- (void)_fileSystemEventOccurred:(CBHFileSystemEvent *)event
{
id context = [event object];
// Do something with the event.
}
// [...]
许可证
CBHFileSystemEventKit 在 ISC 许可证 下提供。