Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
An Objective-C file watcher class, built around FSEvents.
branch: master

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.xcodeproj
.gitignore
CZAFileWatcher.h
CZAFileWatcher.m
License.md
Readme.md
watch-file.m

Readme.md

CZAFileWatcher

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.

Installation

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

Usage

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.

License

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

Something went wrong with that request. Please try again.