NotBackUpiCloud.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // NotBackUpiCloud.m
  3. // MingMen
  4. //
  5. // Created by 罗云飞 on 2017/3/18.
  6. // Copyright © 2017年 罗云飞. All rights reserved.
  7. //
  8. #import "NotBackUpiCloud.h"
  9. #import "sys/xattr.h" //设置禁止云同步
  10. @implementation NotBackUpiCloud
  11. static NotBackUpiCloud *sharedObj = nil;
  12. + (instancetype)sharedInstance
  13. {
  14. static dispatch_once_t onceToken = 0;
  15. dispatch_once(&onceToken, ^{
  16. sharedObj = [[super allocWithZone: NULL] init];
  17. });
  18. return sharedObj;
  19. }
  20. + (id) allocWithZone:(struct _NSZone *)zone
  21. {
  22. return [self sharedInstance];
  23. }
  24. - (id) copyWithZone:(NSZone *) zone
  25. {
  26. return self;
  27. }
  28. /*
  29. *写个宏方法 document内的文件夹设置不备iCloud备份
  30. #define AddSkipBackupAttributeToItemAtPath(filePathString) \
  31. { \
  32. NSURL* URL= [NSURL fileURLWithPath: filePathString]; \
  33. if([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]){ \
  34. [URL setResourceValue: [NSNumber numberWithBool: YES] forKey: NSURLIsExcludedFromBackupKey error: nil]; \
  35. } \
  36. }
  37. 在需要的地方这样调用
  38. AddSkipBackupAttributeToItemAtPath(MAP_FILE_DATA_PATH);
  39. */
  40. #pragma mark - 禁止上传icloud
  41. - (void)addNotBackUpiCloud{
  42. //被注释掉的是遍历NSDocumentDirectory目录下所有文件不被备份上传 但是有个隐患就是如果这个方法在没被更新之前又新增的文件还是被上传到iCloud
  43. //为此我把整个NSDocumentDirectory文件夹都设置成不能备份上传到iCloud
  44. //还有补充一点NSLibraryDirectory目录本身就不会被备份上传到iCloud
  45. NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
  46. NSString* docPath = [documentPaths objectAtIndex:0];
  47. NSURL *filePath = [NSURL fileURLWithPath:docPath];
  48. [self addSkipBackupAttributeToItemAtURL:filePath];
  49. /*
  50. NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  51. NSArray *libPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
  52. NSString *docPath = [docPaths objectAtIndex:0];
  53. NSString *libPath = [libPaths objectAtIndex:0];
  54. [self fileList:docPath];
  55. [self fileList:libPath];
  56. */
  57. }
  58. /*
  59. - (void)fileList:(NSString*)directory{
  60. NSError *error = nil;
  61. NSFileManager * fileManager = [NSFileManager defaultManager];
  62. NSArray *fileList = [fileManager contentsOfDirectoryAtPath:directory error:&error];
  63. for (NSString* each in fileList) {
  64. NSMutableString* path = [[NSMutableString alloc]initWithString:directory];
  65. [path appendFormat:@"/%@",each];
  66. NSURL *filePath = [NSURL fileURLWithPath:path];
  67. [self addSkipBackupAttributeToItemAtURL:filePath];
  68. [self fileList:path];
  69. }
  70. }
  71. */
  72. #pragma mark - 设置禁止云同步
  73. - (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL{
  74. assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
  75. NSError *error = nil;
  76. BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES] forKey:NSURLIsExcludedFromBackupKey error: &error];
  77. if(!success){
  78. NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
  79. }
  80. return success;
  81. /*
  82. double version = [[UIDevice currentDevice].systemVersion doubleValue];//判定系统版本。
  83. if(version >=5.1f){
  84. NSError *error = nil;
  85. BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]
  86. forKey: NSURLIsExcludedFromBackupKey error: &error];
  87. if(!success){
  88. NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
  89. }
  90. return success;
  91. }
  92. const char* filePath = [[URL path] fileSystemRepresentation];
  93. const char* attrName = "com.apple.MobileBackup";
  94. u_int8_t attrValue = 1;
  95. int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
  96. return result == 0;
  97. */
  98. }
  99. @end