通知更改iPhone的/ Documents目录 [英] Notification of changes to the iPhone's /Documents directory

查看:149
本文介绍了通知更改iPhone的/ Documents目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We have an app that uses file sharing. UIFileSharingEnable is set etc. and it all seems to work fine, but I'm looking for some sort of notification of when files have been added/deleted on the iPhone side. Can anyone advise?

提前干杯。

推荐答案

Apple开发者论坛上的这个主题可能会引起关注,建议您运行 kqueue 在自己的主题中,跟踪应用程序的Documents文件夹。

This thread on the Apple Developer Forums may be of interest, in which it is suggested that you run a kqueue in its own thread, tracking the app's Documents folder.

一位Apple技术人员跟进了一些示例代码

An Apple tech followed up with some 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的/ Documents目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆