美文网首页
IOS 监听文件改变

IOS 监听文件改变

作者: gscc | 来源:发表于2015-05-20 14:47 被阅读558次

    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);

    相关文章

      网友评论

          本文标题:IOS 监听文件改变

          本文链接:https://www.haomeiwen.com/subject/pdvlqttx.html