Latest 1.02-4
Homepage https://github.com/gurinderhans/SwiftFSWatcher
License MIT Version 1.0

Created by Gurinder Hans on 04/10/2016.
Copyright 2016 Gurinder Hans

This code is distributed under the terms and conditions of the MIT license.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Platforms osx 10.9
Authors

A simple easy to use / extend File System watcher using Swift

Example (Swift)

import Cocoa
import SwiftFSWatcher

class ViewController: NSViewController {

    // NOTE: - Any instance of `SwiftFSWatcher` must be class scoped, like below:

    /* This will not crash your app. */
    let fileWatcher = SwiftFSWatcher(["/var/www/html/", "/home/Downloads/"])
    let anotherWatcher = SwiftFSWatcher()

    override func viewDidLoad() {
        super.viewDidLoad()

        /* Using `localWatcher` will crash your app. */
        // let localWatcher = SwiftFSWatcher()

        fileWatcher.watch { changeEvents in
            for ev in changeEvents {
                print("eventPath: (ev.eventPath), eventFlag: (ev.eventFlag), eventId: (ev.eventId)")

                // check if this event is of a file created
                if ev.eventFlag == (kFSEventStreamEventFlagItemIsFile + kFSEventStreamEventFlagItemCreated) {
                    print("created file at path: (ev.eventPath)")
                }

            }
        }

        // setup and listen second watcher events on files only
        anotherWatcher.watchingPaths = ["/home/myFile.txt", "/root/bash_session.txt"]
        anotherWatcher.watch { changeEvents in
            for ev in changeEvents {
                print("eventPath: (ev.eventPath), eventFlag: (ev.eventFlag), eventId: (ev.eventId)")

                if ev.eventFlag == (kFSEventStreamEventFlagItemIsFile + kFSEventStreamEventFlagItemInodeMetaMod + kFSEventStreamEventFlagItemModified) {
                    print("file modified at: (ev.eventPath)")
                }
            }
        }
    }
}

Example (Objective-C)

#import "ViewController.h"
#import <SwiftFSWatcher/SwiftFSWatcher-Swift.h>

@implementation ViewController

SwiftFSWatcher * s;

- (void)viewDidLoad {
    [super viewDidLoad];

    s = [[SwiftFSWatcher alloc] init];

    s.watchingPaths = [@[@"/path/to/some/folder/", @"/path/to/myFile.txt"] mutableCopy];

    [s watch:^(NSArray<FileEvent *> * aa) {
        NSLog(@"changed paths: %@", aa);
    }];
}
@end

Installation (two ways)

  • Include pod 'SwiftFSWatcher' in your Podfile

    • You’ll need to add use_frameworks! since the framework is built in Swift.
  • Grab the SwiftFSWatcher.framework and add it to your project or build the .framework yoursleves by downloading this project.

  • Use it in your app

Questions?

Have a question? Feel free to email me.

You added a new feature?

Send it in right now! I can’t wait to see what you’ve done!

Found a Bug?

Oh No! Send in a pull request with the patch (very much appreciated) or just contact me :D

License

MIT License

Latest podspec

{
    "name": "SwiftFSWatcher",
    "version": "1.02-4",
    "summary": "A simple easy to use / extend File System watcher using Swift.",
    "homepage": "https://github.com/gurinderhans/SwiftFSWatcher",
    "license": {
        "type": "MIT",
        "text": "tVersion 1.0nntCreated by Gurinder Hans on 04/10/2016.ntCopyright 2016 Gurinder HansnntThis code is distributed under the terms and conditions of the MIT license.nntPermission is hereby granted, free of charge, to any person obtaining a copyntof this software and associated documentation files (the "Software"), to dealntin the Software without restriction, including without limitation the rightsntto use, copy, modify, merge, publish, distribute, sublicense, and/or sellntcopies of the Software, and to permit persons to whom the Software isntfurnished to do so, subject to the following conditions:nntThe above copyright notice and this permission notice shall be included inntall copies or substantial portions of the Software.nntTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORntIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,ntFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEntAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERntLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,ntOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS INntTHE SOFTWARE.n"
    },
    "authors": {
        "Gurinder Hans": "[email protected]"
    },
    "social_media_url": "http://twitter.com/itsgurinderhans",
    "platforms": {
        "osx": "10.9"
    },
    "source": {
        "git": "https://github.com/gurinderhans/SwiftFSWatcher.git",
        "tag": "1.02-4"
    },
    "source_files": [
        "Classes",
        "src/SwiftFSWatcher/SwiftFSWatcher/SwiftFSWatcher.{h,swift}"
    ]
}

Pin It on Pinterest

Share This