1

我有一个错误,我的应用程序不会在第一次启动时提取 icloud 信息,但在随后的尝试中会这样做。这是我从 icloud 加载文件的代码。

无效 DLC::loadFromiCloud(std::string fileName){

NSURL *mUbiqUrl = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];

如果(mUbiqUrl){

std::string fullPath = getLibraryPath_iOS() + fileName;

NSString* restorePath = [NSString stringWithUTF8String:(fullPath.c_str())];
    NSError *error = nil;
    NSURL *fileToDownload = [mUbiqUrl URLByAppendingPathComponent:[NSString stringWithUTF8String:(fileName.c_str())]];
    [[NSFileManager defaultManager] startDownloadingUbiquitousItemAtURL:fileToDownload 
                                                error:&error];
    if (error != nil)
    {
        NSLog(@"iCloud L2 error: %@", [error localizedDescription]);
    }
    else 
    {
        NSNumber*  isIniCloud = nil;
        if ([fileToDownload getResourceValue:&isIniCloud forKey:NSURLIsUbiquitousItemKey error:nil])
        {
            // If the item is in iCloud, see if it is downloaded.
            if ([isIniCloud boolValue]) 
            {
                NSNumber*  isDownloaded = nil;
                if ([fileToDownload getResourceValue:&isDownloaded forKey:NSURLUbiquitousItemIsDownloadedKey error:nil]) 
                {
                    if ([isDownloaded boolValue])
                    {
                        NSString *fileData = [NSString stringWithContentsOfURL:fileToDownload encoding:NSUTF8StringEncoding error:&error];
                        //NSLog(@"iCloud data: %@", fileData);
                        [fileData writeToFile:restorePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
                        if (error)
                        {
                            NSLog(@"iCloud L3 error: %@", [error localizedDescription]);
                        }
                    }
                }
            }
        }
    }
}}
4

0 回答 0