An Objective-C file watcher class, built around FSEvents.
Switch branches/tags
Nothing to show
Clone or download
Pull request Compare This branch is even with CarterA:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


CZAFileWatcher is a simple Objective-C wrapper around Apple's FSEvents system for watching files. It is small (less than 100 lines of code), compact (a single header/implementation pair, no dependencies), and provides a convenient way of monitoring the filesystem for activity. CZAFileWatcher uses Automatic Reference Counting (ARC) and Objective-C Blocks.


Just add CZAFileWatcher.h and CZAFileWatcher.m to your project and #import "CZAFileWatcher.h" whenever you need to access the class.


First, set up an array of filesystem URLs:

NSURL *desktopURL = [[NSFileManager defaultManager] URLForDirectory:NSDesktopDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
NSArray *URLs = @[desktopURL];

Next, instantiate a CZAFileWatcher object, keeping in mind that the object is immutable after creation:

CZAFileWatcher *watcher = [CZAFileWatcher fileWatcherForURLs:@[desktopURL] changesHandler:^(NSArray *changedURLs) {
    // Respond to the change...

And start the watcher:

[watcher startWatching];

That's all there is to it! You can always stop watching the URLs by calling -stopWatching.

A working example program (watch-file.m) is included in this repository. The watch-file command-line interface (CLI) accepts file paths as arguments and watches them, printing a message whenever a change is detected.


CZAFileWatcher is released under the MIT License, which can be found in the file.