在NSDocumentDirectory上禁用数据备份时

在NSDocumentDirectory上禁用数据备份时

本文介绍了在NSDocumentDirectory上禁用数据备份时,CFURLSetResourcePropertyForKey失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试下载图像文件并存储在 NSDocumentDirectory 中。为此,我必须关闭iCloud和iTunes上的数据备份。以下是我的代码:

I am trying to download image files and store in NSDocumentDirectory. In order to do so, I has to turn off data backup on iCloud and iTunes. Below are my codes:

+(void)saveData:(NSData*)thedata:(NSString*)fileName
{
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];
   NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:fileName];
   NSFileManager *fileManager = [NSFileManager defaultManager];
   [fileManager createFileAtPath:localFilePath contents:thedata attributes:nil];
   //prevent files from backup on iCloud or iTune
   NSURL *fileURL = [NSURL URLWithString:localFilePath];
   [self addSkipBackupAttributeToItemAtURL:fileURL];
}

和我的addskipbackupattributetoitematurl:

and for my addskipbackupattributetoitematurl:

+(BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)fileURL
{
   if (![[NSFileManager defaultManager] fileExistsAtPath:[fileURL path]])
   {
       NSLog(@"File %@ doesn't exist!",[fileURL path]);
       return NO;
   }
   NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
   if ([currSysVer isEqualToString:@"5.0.1"])
   {
       const char* filePath = [[fileURL path] fileSystemRepresentation];
       const char* attrName = "com.apple.MobileBackup";
       u_int8_t attrValue = 1;
       int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
       NSLog(@"Excluded '%@' from backup",fileURL);
       return result == 0;
   }
   else if (&NSURLIsExcludedFromBackupKey)
   {
       NSError *error = nil;
       BOOL result = [fileURL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error];
       if (result == NO)
       {
           NSLog(@"Error excluding '%@' from backup. Error: %@",fileURL, error);
           return NO;
       }
       else
       {
           NSLog(@"Excluded '%@' from backup",fileURL);
           return YES;
       }
   }
   else
   {
       return YES;
   }
}

然而, BOOL结果= [fileURL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey错误:& error]; 创建以下消息

我只是想知道是否遇到过这个问题?

I'm just wondering if any encountered this problem??

推荐答案

解决了。一旦我改变了

NSURL *fileURL = [NSURL URLWithString:localFilePath];

NSURL *fileURL = [NSURL fileURLWithPath:localFilePath];

一切正常。

这篇关于在NSDocumentDirectory上禁用数据备份时,CFURLSetResourcePropertyForKey失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 23:40