通知更改iPhone的/ Documents目录

时间:2010-07-05 19:48:30

标签: iphone notifications file-sharing

我们有一个使用文件共享的应用。 UIFileSharingEnable设置等等,它似乎都运行良好,但我正在寻找一些关于何时在iPhone端添加/删除文件的通知。有人可以建议吗?

提前干杯。

3 个答案:

答案 0 :(得分:23)

答案 1 :(得分:14)

这是使用Grand Central Dispatch(GCD)的替代解决方案,允许您从NSNotificationCenter接收文件更改通知:

将这些变量添加到类的界面中:

// Dispatch queue
dispatch_queue_t _dispatchQueue;

// A source of potential notifications
dispatch_source_t _source;

将以下代码添加到实现中:

    #define fileChangedNotification @"fileChangedNotification"

    // Get the path to the home directory
    NSString * homeDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

    // Create a new file descriptor - we need to convert the NSString to a char * i.e. C style string
    int filedes = open([homeDirectory cStringUsingEncoding:NSASCIIStringEncoding], O_EVTONLY);

    // Create a dispatch queue - when a file changes the event will be sent to this queue
    _dispatchQueue = dispatch_queue_create("FileMonitorQueue", 0);

    // Create a GCD source. This will monitor the file descriptor to see if a write command is detected
    // The following options are available

    /*!
     * @typedef dispatch_source_vnode_flags_t
     * Type of dispatch_source_vnode flags
     *
     * @constant DISPATCH_VNODE_DELETE
     * The filesystem object was deleted from the namespace.
     *
     * @constant DISPATCH_VNODE_WRITE
     * The filesystem object data changed.
     *
     * @constant DISPATCH_VNODE_EXTEND
     * The filesystem object changed in size.
     *
     * @constant DISPATCH_VNODE_ATTRIB
     * The filesystem object metadata changed.
     *
     * @constant DISPATCH_VNODE_LINK
     * The filesystem object link count changed.
     *
     * @constant DISPATCH_VNODE_RENAME
     * The filesystem object was renamed in the namespace.
     *
     * @constant DISPATCH_VNODE_REVOKE
     * The filesystem object was revoked.
     */

    // Write covers - adding a file, renaming a file and deleting a file...
    _source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE,filedes,
                                                       DISPATCH_VNODE_WRITE,
                                                       _dispatchQueue);


    // This block will be called when teh file changes
    dispatch_source_set_event_handler(_source, ^(){
        // We call an NSNotification so the file can change can be detected anywhere
        [[NSNotificationCenter defaultCenter] postNotificationName:fileChangedNotification object:Nil];
    });

    // When we stop monitoring the file this will be called and it will close the file descriptor
    dispatch_source_set_cancel_handler(_source, ^() {
        close(filedes);
    });

    // Start monitoring the file...
    dispatch_resume(_source);

    //...

    // When we want to stop monitoring the file we call this
    //dispatch_source_cancel(source);


    // To recieve a notification about the file change we can use the NSNotificationCenter
    [[NSNotificationCenter defaultCenter] addObserverForName:fileChangedNotification object:Nil queue:Nil usingBlock:^(NSNotification * notification) {
        NSLog(@"File change detected!");
    }];

答案 2 :(得分:4)

老问题,但我遇到了包含目录监视器的this Apple code。请注意,它会触发添加(或删除)文件的时刻;这可能是在OS完成写入文件之前。