数据不会保存到桌面

Dan*_*nyK 1 pdf arrays parsing save ios

我想通过模拟器从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...");
        }
    }];

}
Run Code Online (Sandbox Code Playgroud)

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

@property (retain,nonatomic) PFFile * downloadfile;
Run Code Online (Sandbox Code Playgroud)

这是segue:

detailVC.downloadfile=[[PDFArray objectAtIndex:indexPath.row]objectForKey:@"PDFFile"];
Run Code Online (Sandbox Code Playgroud)

无法将其保存到桌面.

Raj*_*jat 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;
}
Run Code Online (Sandbox Code Playgroud)

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