IOS 监听文件改变

2015-05-20  本文已影响558人  gscc

NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

if(paths.count==0){return;}

NSString*ksnowDir=[[pathsobjectAtIndex:0]stringByAppendingPathComponent:@"ksnow"];NSLog(@"ksnowdir = %@",ksnowDir);

NSURL*directoryURL=[NSURLURLWithString:ksnowDir];intconstfd=open([[directoryURLpath]fileSystemRepresentation],O_EVTONLY);

if(fd<0){NSLog(@"Unable to open the path = %@",[directoryURLpath]);return;}

dispatch_source_tsource=dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE,fd,DISPATCH_VNODE_WRITE|DISPATCH_VNODE_RENAME,DISPATCH_TARGET_QUEUE_DEFAULT);

dispatch_source_set_event_handler(source,^(){

unsignedlongconsttype=dispatch_source_get_data(source);

switch(type){

caseDISPATCH_VNODE_WRITE:{

NSLog(@"目录内容改变!!!");break;

}

caseDISPATCH_VNODE_RENAME:{

NSLog(@"目录被重命名!!!");break;

}

default:break;

}});

dispatch_source_set_cancel_handler(source,^(){close(fd);

});

self.source=source;

dispatch_resume(self.source);

上一篇 下一篇

猜你喜欢

热点阅读