iphone - iPhone 的/Documents 目录更改的通知

标签 iphone notifications file-sharing

我们有一个使用文件共享的应用程序。 UIFileSharingEnable 已设置等,一切似乎都工作正常,但我正在寻找某种通知,说明何时在 iPhone 端添加/删除文件。谁能给点建议吗?

提前干杯。

最佳答案

This thread Apple 开发者论坛上的内容可能会引起您的兴趣,其中建议您运行 kqueue在它自己的线程中,跟踪应用程序的文档文件夹。

苹果技术人员跟进了一些sample code here :

- (void)kqueueFired
{
    int             kq;
    struct kevent   event;
    struct timespec timeout = { 0, 0 };
    int             eventCount;

    kq = CFFileDescriptorGetNativeDescriptor(self->_kqRef);
    assert(kq >= 0);

    eventCount = kevent(kq, NULL, 0, &event, 1, &timeout);
    assert( (eventCount >= 0) && (eventCount < 2) );

    if (eventCount == 1) {
        NSLog(@"dir changed");
    }    

    CFFileDescriptorEnableCallBacks(self->_kqRef, kCFFileDescriptorReadCallBack);
}

static void KQCallback(CFFileDescriptorRef kqRef, CFOptionFlags callBackTypes, void *info)
{
    ViewController *    obj;

    obj = (ViewController *) info;
    assert([obj isKindOfClass:[ViewController class]]);
    assert(kqRef == obj->_kqRef);
    assert(callBackTypes == kCFFileDescriptorReadCallBack);

    [obj kqueueFired];
}

- (IBAction)testAction:(id)sender
{
    #pragma unused(sender)
    NSString *              docPath;
    int                     dirFD;
    int                     kq;
    int                     retVal;
    struct kevent           eventToAdd;
    CFFileDescriptorContext context = { 0, self, NULL, NULL, NULL };
    CFRunLoopSourceRef      rls;

    docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    assert(docPath != 0);

    NSLog(@"%@", docPath);

    dirFD = open([docPath fileSystemRepresentation], O_EVTONLY);
    assert(dirFD >= 0);

    kq = kqueue();
    assert(kq >= 0);

    eventToAdd.ident  = dirFD;
    eventToAdd.filter = EVFILT_VNODE;
    eventToAdd.flags  = EV_ADD | EV_CLEAR;
    eventToAdd.fflags = NOTE_WRITE;
    eventToAdd.data   = 0;
    eventToAdd.udata  = NULL;

    retVal = kevent(kq, &eventToAdd, 1, NULL, 0, NULL);
    assert(retVal == 0);

    assert(self->_kqRef == NULL);

    self->_kqRef = CFFileDescriptorCreate(NULL, kq, true, KQCallback, &context);
    assert(self->_kqRef != NULL);

    rls = CFFileDescriptorCreateRunLoopSource(NULL, self->_kqRef, 0);
    assert(rls != NULL);

    CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);

    CFRelease(rls);

    CFFileDescriptorEnableCallBacks(self->_kqRef, kCFFileDescriptorReadCallBack);
}

关于iphone - iPhone 的/Documents 目录更改的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3181821/

相关文章:

ios - 微信SDK集成iOS

android - 以编程方式检查 Android 系统安全通知访问设置

ios - 使用脚本桥通过 iTunes 将文件从桌面应用程序发送到 iPad

iphone - TableView Footer 随表格滚动

iphone - Localizable.strings(英文)不起作用但 InfoPlist.strings(英文)有效?

php - 我无法接收推送通知 - 我收到响应 "Resource Id #3"

ios - 点击时清除横幅通知

iphone - 如何将sqlite3数据库文件存放在应用程序的Library目录下?

javascript - 如何在没有Flash/Java的情况下实现浏览器到浏览器的通信(允许服务器作为桥梁)?

iOS 文件检索 - NSDocumentDirectory