Skip to content

Latest commit

 

History

History
68 lines (49 loc) · 4.21 KB

README.md

File metadata and controls

68 lines (49 loc) · 4.21 KB

DirectoryWatcherTouch

This small library allows you to watch for change events to a folder in your iOS Application's file system using C# and MonoTouch. At its core, DirectoryWatcherTouch is C# wrapper and MonoTouch binding around the Objective-C DirectoryWatcher class by Apple. The FileSystemWatcherTouch class provides a subset of the functionality of the C# .NET FileSystemWatcher class.

Pre-requisites

You are going to need:

Installation / Configuration

  1. Clone or download this repository to your Mac running OSX. Place it in a convenient location such as Development/Repositories/DirectoryWatcherTouch or similar.

  2. Build the native, static libDirectoryWatcher.a library. Open a terminal window and navigate to the DirectoryWatcherTouch/DirectoryWatcherNative/DirectoryWatcher folder. (This folder contains a bash shell script for building the native library for the iphonesimulator, armv7 and armv7s. These three architectures are bundled into single FAT binary using the lipo tool - see note below) Simply run the following command at the prompt:

    sudo ./build_mobile.sh

  3. Once the static library has successfully built, you need to point the .NET binding project (DirectoryWatcherTouch) to the correct location. In Xamarin Studio or Visual Studio, open the DirectoryWatcherTouch Solution contained in the DirectoryWatcherTouch/DirectoryWatcherTouch folder. The link to libDirectoryWatcher.a may be broken and you will have to re-reference this file into the project.

  4. Clean and Build the DirectoryWatcherTouch project. The resulting DirectoryWatcherTouch.dll that can be referenced into your project.

Usage

Use DirectoryWatcherTouch...

using DirectoryWatcherTouch;

Create a property or backing field...

private FileSystemWatcherTouch DocumentsDirectoryWatcher { get; set; }

Set up a FileSystemWatcher...

// Start watching the Documents folder...
string documentsFolder = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);
DocumentsDirectoryWatcher = new FileSystemWatcherTouch (documentsFolder);
DocumentsDirectoryWatcher.Changed += OnDirectoryDidChange;

Handle change event callbacks...

public void OnDirectoryDidChange (object sender, EventArgs args) {
	Console.WriteLine ("Change detected in the Documents folder");
	// Handle the change...
}

Make sure you clean up after yourself when done...for example, in AppDelegate you might:

public override void WillTerminate (UIApplication application)
{
	base.WillTerminate (application);

	// Stop watching the Documents folder...
	DocumentsDirectoryWatcher.Changed -= OnDirectoryDidChange;
	DocumentsDirectoryWatcher.Dispose (); // This calls Invalidate() on the native watcher.
}

Detailed Info

The native DirectoryWatcher class comes from this Apple iOS Developer Library sample. This class uses the low-level kqueue kernel event notification system. The MonoTouch bindings wrapping this small library attempt (in a small way) to mimic Microsoft .NET FileSystemWatcher class, though only the Changed event is current implemented.

  • Note on the Static Binary

The build_mobile.sh build script included here builds a FAT binary targeting iphonesimulator, armv7, and armv7s. To make the binary slightly smaller for release, you may remove the iphonesimulator target. Regardless, the FAT binary is quite small, so it may not be worth the effort.

Authors