相关的api:
1:应用程序通过FSEventStreamCreate
or FSEventStreamCreateRelativeToDevice注册。
2:使用循环来获取FSEventStreamScheduleWithRunLoop。
3:告诉后台进程开始文件监控FSEventStreamStart。
4:如果事件产生,然后就回调函数FSEventStreamCallback
5:停止后台进程发送通知FSEventStreamStop
.
6:作废事件 FSEventStreamInvalidate
.
7:释放事件FSEventStreamRelease
.
代码例子:
int main(int argc, const char * argv[])
{
CFStringRef path = CFSTR("/USERS/username/downloads");
CFArrayRef paths= CFArrayCreate(NULL, (const void **)&path, 1, NULL);
FSEventStreamRef stream = FSEventStreamCreate(kCFAllocatorDefault,
&eventStreamCallback,
NULL,
paths,
kFSEventStreamEventIdSinceNow,
1.0, // 事件发生后延迟多少秒调用回调,如果时间设长则有更高的效率,会一次性通知多个事件
kFSEventStreamCreateFlagFileEvents) ;
FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
FSEventStreamStart(stream);
CFRunLoopRun();
return 0;
}
void eventStreamCallback(
ConstFSEventStreamRef streamRef,
void *clientCallBackInfo,
size_t numEvents,
void *eventPaths,
const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId eventIds[])
{
int i;
/* eventPaths
An array of paths to the directories in which event(s) occurred.
The type of this parameter depends on the flags passed to FSEventStreamCreate...(). If kFSEventStreamCreateFlagUseCFTypes was set, then this will be a CFArrayRef containing CFStringRef objects (per CFStringCreateWithFileSystemRepresentation()). Ownership follows the Get rule, and they will be released by the framework after your callback returns. If kFSEventStreamCreateFlagUseCFTypes was not set, then the framework will pass your callback a raw C array of raw C strings that will be deallocated by the framework after your callback returns.
A path might be '/' if ether of these flags is set for the event: kFSEventStreamEventFlagUserDropped, kFSEventStreamEventFlagKernelDropped.
*/
char **paths = eventPaths;
for (i=0; i<numEvents; i++)
{
NSLog(@"path=%s", paths[i]);
if ((eventFlags[i] &kFSEventStreamEventFlagRootChanged) !=0)
{
//do something you want
}
}
}
函数文档
FSEventStreamCreate
extern FSEventStreamRef FSEventStreamCreate(
CFAllocatorRef allocator, FSEventStreamCallback callback, FSEventStreamContext *context, CFArrayRef pathsToWatch, FSEventStreamEventId sinceWhen, CFTimeInterval latency, FSEventStreamCreateFlags flags);
The CFAllocator to be used to allocate memory for the stream. Pass NULL or kCFAllocatorDefault to use the current default allocator.
An FSEventStreamCallback which will be called when FS events occur.
A pointer to the FSEventStreamContext structure the client wants to associate with this stream. Its fields are copied out into the stream itself so its memory can be released after the stream is created. Passing NULL is allowed and has the same effect as passing a structure whose fields are all set to zero.
A CFArray of CFStringRefs, each specifying a path to a directory, signifying the root of a filesystem hierarchy to be watched for modifications.
The service will supply events that have happened after the given event ID. To ask for events "since now" pass the constant kFSEventStreamEventIdSinceNow. Often, clients will supply the highest-numbered FSEventStreamEventId they have received in a callback, which they can obtain via the FSEventStreamGetLatestEventId() accessor. Do not pass zero for sinceWhen, unless you want to receive events for every directory modified since "the beginning of time" -- an unlikely scenario.
The number of seconds the service should wait after hearing about an event from the kernel before passing it along to the client via its callback. Specifying a larger value may result in more effective temporal coalescing, resulting in fewer callbacks and greater overall efficiency.
Flags that modify the behavior of the stream being created. See FSEventStreamCreateFlags.
A valid FSEventStreamRef.