gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 18:21:55 25 4
gpt4 key购买 nike

我们有一个使用文件共享的应用程序。 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com