This small library allows you to watch for change events to a folder in your iOS Application's file system using C# and Xamarin.iOS. At its core, DirectoryWatcherTouch is C# wrapper and Xamarin.iOS binding around the Objective-C DirectoryWatcher class by Apple. The FileSystemWatcherTouch class provides a subset of the functionality of the C# .NET FileSystemWatcher class.
You are going to need:
- Apple Xcode
- Xamarin.iOS
- Xamarin Studio or Visual Studio Professional with Xamarin extensions.
- Clone or download this repository to your Mac running OSX. Place it in a convenient location such as Development/Repositories/DirectoryWatcherXamariniOS or similar.
- Build the native, static libDirectoryWatcher.a library. Open a terminal window and navigate to the DirectoryWatcherXamariniOS/DirectoryWatcherNative/DirectoryWatcher folder. (This folder contains a bash shell script for building the native library for the iphonesimulator and arm64. These two 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
- Once the static library has successfully built, you need to point the .NET binding project (DirectoryWatcherXamariniOS) to the correct location. In Xamarin Studio or Visual Studio, open the DirectoryWatcherXamariniOS Solution contained in the DirectoryWatcherXamariniOS/DirectoryWatcherXamariniOS folder. The link to libDirectoryWatcher.a may be broken and you will have to re-reference this file into the project.
- Clean and Build the DirectoryWatcherXamariniOS project. The resulting DirectoryWatcherXamariniOS.dll that can be referenced into your project.
Use DirectoryWatcherXamariniOS...
using DirectoryWatcherXamariniOS;
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.
}
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.
The build_mobile.sh build script included here builds a FAT binary targeting iphonesimulator and arm64. 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.
- Dan Belcher - https://github.com/dbelcher dan@mcneel.com