【问题标题】:Data won't save to Desktop数据不会保存到桌面
【发布时间】:2014-05-03 23:08:23
【问题描述】:

我正在尝试通过模拟器从 Parse 中提取 PDF 文件,并在单击我的应用程序上的按钮时将其保存到我的桌面上。问题是无论我做什么文件都不会出现在桌面或其他任何地方。我正在为我的下载按钮使用这种方法

-(void) Savefile {
    NSFileManager *filemanager = [NSFileManager defaultManager];
    [self.downloadfile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
        if (error)  {

        }
        else if (data) {
            [filemanager createFileAtPath:@"file:///Users/Danny/Desktop" contents:data attributes:nil];
            [data writeToFile:@"file:///Users/Danny/Desktop" atomically:NO ];
            NSLog(@"Downloading file...");
        }
    }];

}

downloadFile 是一个 PFFile 属性,当我通过 segue 移动它时,它存储我的 PDF 文件的索引。它的声明如下:

@property (retain,nonatomic) PFFile * downloadfile;

这是转场:

detailVC.downloadfile=[[PDFArray objectAtIndex:indexPath.row]objectForKey:@"PDFFile"];

无法将其保存到桌面。

【问题讨论】:

  • 忽略 iOS 应用程序无法访问其沙箱外文件的重要问题,请注意 writeToFile:createFileAtPath: 方法采用文件路径,而不是文件 URL。摆脱领先的file://。并摆脱对createFileAtPath:contents:attributes: 的呼叫。您只需致电writeToFile:
  • @rmaddy 所以我必须在沙箱内。有没有办法在侧面板上显示资源?还是看不到文件?

标签: ios arrays parsing pdf save


【解决方案1】:

您尝试下载PDF文件的路径在iOS设备中不存在,如果您想在您的设备中下载文件,您可以这样尝试:-

-(void) Savefile {

    [self.downloadfile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
        if (error)
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wooopss!" message:@"Download failed. Try Again..." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }
        else if (data)
        {
            NSString *downloadFilePath = [NSString stringWithFormat:@"%@/Documents/PDFFile/hello_%@.pdf", NSHomeDirectory(),[self getCurrentDate]];

            if( ![[NSFileManager defaultManager] fileExistsAtPath:downloadFilePath] )
            {
                NSLog(@"File don't exist at that path");
            }
            else
            {
                NSError *error = nil;
                [[NSFileManager defaultManager] removeItemAtPath:downloadFilePath error:&error];
            }

            [[NSFileManager defaultManager] createFileAtPath:downloadFilePath contents:nil attributes:nil];

            NSLog(@"Downloading file...");

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Downloading" message:@"File is being downloaded..." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }
    }];

}
//Below method will return you current timestamp through which you can download new PDF with new name

-(NSString *)getCurrentDate
{

    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    [dateFormatter setDateFormat:@"MMddYYYYmmss"];

    NSString *date = [dateFormatter stringFromDate:[NSDate date]];

    return date;
}

希望这会帮助你快乐编码:)

【讨论】:

    【解决方案2】:

    iOS 设备没有桌面。如果 iOS 确实有桌面,那么应用程序将无法写入它,因为 iOS 应用程序是沙盒化的,因此它们无法在自己的数据存储区域之外写入。

    即使在模拟器上运行,您也无法写入 OSX 桌面,因为该应用已被沙盒化到模拟器文件存储中。您可以将文件保存到您的应用沙箱,然后从 finder 访问模拟器存储 -

    Is there any way to see the file system on the iOS simulator?

    【讨论】:

    • 啊谢谢伙计。我完全没有意识到这一点。现在我必须弄清楚这条路径,因为我的图书馆没有显示
    • Finder 默认不显示所有文件夹 - 在 finder 的“Go”菜单中选择“Go to finder”,然后输入“Library”
    • 好的,谢谢,我想我找到了一个更简单的方法,如果你进入 Xcode 管理器,它会告诉你文件的路径。所以当我写这个文件时,我得到的路径我能看到它吗?
    • 好吧,我终于让它工作了。但我注意到的是,当我下载一个文件时,我无法再次下载它。或者如果我下载另一个它会替换旧的。我想这样做,所以它不会这样做,这样用户就可以下载不同的,而不会被删除。
    • 那你需要另存为不同的文件名。您可以在写入文件之前使用NSFileManagerfileExistsAtPath 方法进行检查,如果确实如此,那么您可以以某种方式操作文件名——比如添加诸如“-1”之类的后缀。您需要循环执行此操作,直到 fileExistsAtPath 返回 NO,因为可能已经存在 -1 和 -2 等等
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多