我正在尝试制作一个应用程序,它只监视特定文件夹的更改并输出更改的文件的路径。稍后它将对这些更改的文件进行一些处理。我将如何在原生可可中做到这一点?我尝试过以下所列的内容:http: //developer.apple.com/library/mac/#featuredarticles/FileSystemEvents/_index.html
但我不知道如何有效地完成任务。
代码示例将不胜感激。
我正在尝试制作一个应用程序,它只监视特定文件夹的更改并输出更改的文件的路径。稍后它将对这些更改的文件进行一些处理。我将如何在原生可可中做到这一点?我尝试过以下所列的内容:http: //developer.apple.com/library/mac/#featuredarticles/FileSystemEvents/_index.html
但我不知道如何有效地完成任务。
代码示例将不胜感激。
@interface Notifier : NSObject {
NSArray *paths; //Actually just one.
FSEventStreamRef stream;
struct FSEventStreamContext context;
}
+ (id) notifierWithCallback:(FSEventStreamCallback)newCallback path:(NSString *)newPath;
- (id) initWithCallback:(FSEventStreamCallback)newCallback path:(NSString *)newPath;
- (void) start;
- (void) stop;
@end
#import "Notifier.h"
@implementation Notifier
+ (id) notifierWithCallback:(FSEventStreamCallback)newCallback path:(NSString *)newPath {
return [[[self alloc] initWithCallback:newCallback path:newPath] autorelease];
}
- (id) initWithCallback:(FSEventStreamCallback)newCallback path:(NSString *)newPath {
if((self = [super init])) {
paths = [[NSArray arrayWithObject:newPath] retain];
context.version = 0L;
context.info = newPath;
context.retain = (CFAllocatorRetainCallBack)CFRetain;
context.release = (CFAllocatorReleaseCallBack)CFRelease;
context.copyDescription = (CFAllocatorCopyDescriptionCallBack)CFCopyDescription;
stream = FSEventStreamCreate(kCFAllocatorDefault, newCallback, &context, (CFArrayRef)paths, kFSEventStreamEventIdSinceNow, /*latency*/ 1.0, kFSEventStreamCreateFlagUseCFTypes);
if (!stream) {
NSLog(@"Could not create event stream for path %@", newPath);
[self release];
return nil;
}
FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
}
return self;
}
- (void) dealloc {
[self stop];
FSEventStreamUnscheduleFromRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
CFRelease(stream);
[super dealloc];
}
- (void) start {
FSEventStreamStart(stream);
}
- (void) stop {
FSEventStreamStop(stream);
}
@end
#import "Notifier.h"
static void gotEvent(ConstFSEventStreamRef streamRef,
void *clientCallBackInfo,
size_t numEvents,
void *eventPaths,
const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId eventIds[]);
int main (int argc, char **argv) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSArray *paths = [[NSProcessInfo processInfo] arguments];
NSMutableArray *streams = [NSMutableArray arrayWithCapacity:[paths count]];
for (NSString *path in paths) {
[streams addObject:[Notifier notifierWithCallback:gotEvent path:path]];
}
[streams makeObjectsPerformSelector:@selector(start)];
CFRunLoopRun();
[pool drain];
return EXIT_SUCCESS;
}
static void gotEvent(ConstFSEventStreamRef stream,
void *clientCallBackInfo,
size_t numEvents,
void *eventPathsVoidPointer,
const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId eventIds[]
) {
NSArray *eventPaths = eventPathsVoidPointer;
NSString *streamName = clientCallBackInfo;
NSLog(@"%@: %@", streamName, [eventPaths objectAtIndex:0UL]);
}
示例是做你想做的事情的基本方法。
查看使用 FSEvents 框架文档。它会告诉您启动和运行所需的一切。我在这里给出的代码示例与文档中列出的相同。