简体   繁体   English

数据不会保存到桌面

[英]Data won't save to Desktop

I am trying to pull a PDF file from Parse through Simulator and save it onto my Desktop when a button is clicked on my app. 我试图通过模拟器从Parse中提取PDF文件,然后在我的应用程序上单击按钮时将其保存到我的桌面上。 Problem is that no matter what i do file doesn't show up on Desktop nor anywhere else. 问题是,无论我做什么文件都不会显示在桌面上或其他任何地方。 I am using this method for my download button 我正在为下载按钮使用此方法

-(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 is a PFFile property that stores the index of my PDFfile as i move it through segue. downloadFile是一个PFFile属性,当我通过segue移动它时,它存储我的PDFfile的索引。 Its declared as this: 其声明为:

@property (retain,nonatomic) PFFile * downloadfile;

this here is the segue: 这是segue:

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

can't get it to save to desktop. 无法将其保存到桌面。

The path in which you are trying to download Pdf file does not exist in iOS Device, if you want to download file in your device you can try like this :- 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;
}

Hope this will help you Happy Coding :) 希望这对您有所帮助:)

iOS devices don't have a desktop. iOS设备没有台式机。 If iOS did have a desktop, an app would't be able to write to it anyway as iOS apps are sandboxed so they cannot write outside of their own data storage area. 如果iOS 确实有台式机,则由于将iOS应用程序沙盒化,因此应用程序无论如何都无法对其进行写入,因此它们无法在自己的数据存储区域之外进行写入。

Even when running on the simulator, you cannot write to the OSX Desktop, because the app is sandboxed into the Simulator file storage. 即使在模拟器上运行,您也无法写入OSX桌面,因为该应用程序已沙箱化到模拟器文件存储中。 You can save the file to your app sandbox and then access the simulator storage from finder - 您可以将文件保存到应用沙箱,然后从finder访问模拟器存储-

Is there any way to see the file system on the iOS simulator? 有什么办法可以在iOS模拟器上查看文件系统吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM