数据不会保存到桌面

时间:2014-03-25 20:47:48

标签: ios arrays parsing pdf save

我正在尝试通过模拟器从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;

这是segue:

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

无法将其保存到桌面。

2 个答案:

答案 0 :(得分:9)

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

-(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;
}

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

答案 1 :(得分:1)

iOS设备没有桌面设备。如果iOS 有桌面,那么当iOS应用程序被沙盒化时,应用程序无法写入它,因此无法在自己的数据存储区域外写入。

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

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