iMonitorMyFiles provides a simple Objective-C interface for responding to file system changes in iOS. If your users can create, modify, and/or delete files in your app's sandbox, this library provides a way to respond to those events. It also insulates you from a low-level C API (yikes!).
To run the example project, clone the repo, and run
pod install from the Example directory first.
Your app must target iOS 7.0+.
iMonitorMyFiles is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'iMonitorMyFiles', '~> 0.1.0'
and use the TABFileMonitor class for all your file monitoring needs:
File monitoring is achieved by creating what GCD calls a "dispatch source" for whatever file or folder you want to monitor. When creating a dispatch source, you provide three interesting things:
After creating a dispatch source, you then set blocks of code to be executed when an event occurs or when the source is canceled (destroyed). In the block you set for when an event occurs, you can determine which event occurred (if you registered for more than one type), and proceed accordingly with if...else...then or switch...case statements.
In order to continue monitoring a file after it has been deleted and recreated, my code destroys the dispatch source and file descriptor and subsequently recreates them. I did this because I noticed in testing that some applications invalidate the file descriptor we're using whenever they modify the file.
iMonitorMyFiles is available under the MIT license. See the LICENSE file for more info.