codercat codercat - 2 years ago 186
iOS Question

Detect file change under document directory

My app has iTunes file sharing turned on, and when user add a file in iTunes, it should be able to detect.

The following function from Application Delegate is used to be called
but not any more:

- (void)applicationDidBecomeActive:(UIApplication *)application

Is there an alternative solution?

Answer Source

There is a solution: GCD. GCD allows you to create a dispatch_source_t that watches for vnode changes, like a write in a directory, and then notifies you about it. The basic is: Use the open(path, O_EVTONLY) syscall to obtain a file descriptor to the directory, then call dispatch_source_create()and pass DISPATCH_SOURCE_TYPE_VNODE as type and DISPATCH_VNODE_WRITE as mask. Set the event handler using dispatch_source_set_event_handler and do whatever you need to do when a file is written in the directory, additionally you also want to set a cancellation handler that calls close() to close the file descriptor. Finally, call dispatch_resume() to start monitoring.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download