the*_*end 2 macos cocoa cocoa-touch ios icloud
我在同步一个简单的文本文件时遇到问题,我在尝试打开它时遇到此错误:
{NSFilePath =/private/var/mobile/Library/Mobile Documents/4C224W52W5~com~piso13~opusDomini/currentLogPath,NSUnderlyingError = 0xde9b460"操作无法完成.错误的文件描述符"}
这就是我创建它的方式
-(BOOL)createLogFolderFile{
NSString *uuid = nil;
CFUUIDRef uuidRef = CFUUIDCreate(nil);
uuid = (NSString*)CFUUIDCreateString(nil, uuidRef);
CFRelease(uuidRef);
NSError *error = nil;
[uuid writeToFile:[self filePath] atomically:NO encoding:NSUTF8StringEncoding error:&error];
if (error) {
NSLog(@"Error trying to create log file %@", error);
return FALSE;
}
else{
return TRUE;
}
}
-(NSString*)filePath{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *iCloudPath = [[fileManager URLForUbiquityContainerIdentifier:nil] path];
return [iCloudPath stringByAppendingPathComponent:LOG_FOLDER_FILE_NAME];
}
Run Code Online (Sandbox Code Playgroud)
这就是我读它的方式:
-(NSString*)readLogFolderFromFile{
NSError *error = nil;
NSString *logFolder = [NSString stringWithContentsOfFile:[self filePath] encoding:NSUTF8StringEncoding error:&error];
if (error) {
NSLog(@"Error when trying to read log folder from file: %@" ,error);
return nil;
}
else{
return logFolder;
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用NSMetadataQuery来搜索文件,通知查询完成收集信息结果为正.
救命?
该文件未下载.似乎NSMetadataQuery通知云中文件的存在.要实际获取文件,需要额外的代码:
里面的queryDidFinish收集通知:
NSMetadataItem *item = [query resultAtIndex:0];
self.metadataItem = item;
BOOL isDownloaded = [[item valueForAttribute:NSMetadataUbiquitousItemIsDownloadedKey]boolValue];
if (!isDownloaded) {
NSError *error = nil;
[[NSFileManager defaultManager] startDownloadingUbiquitousItemAtURL: [item valueForAttribute:NSMetadataItemURLKey] error:&error];
NSLog(@"Start downloading file");
if (error) {
NSLog(@"Error trying to download file: %@", error);
}
else{
[self lookForLogFolderFile];
return;
}
}
Run Code Online (Sandbox Code Playgroud)
lookForLogFolderFile只是再次启动查询.几次调用后,我的项目被下载.您还可以在每次调用之间使用计时器来启动NSMetadataQuery.就我而言,只是一个包含一行的文本文件.
| 归档时间: |
|
| 查看次数: |
1580 次 |
| 最近记录: |