Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 赢得的数据';无法保存到桌面_Ios_Arrays_Parsing_Pdf_Save - Fatal编程技术网

Ios 赢得的数据';无法保存到桌面

Ios 赢得的数据';无法保存到桌面,ios,arrays,parsing,pdf,save,Ios,Arrays,Parsing,Pdf,Save,我试图通过模拟器从解析中提取一个PDF文件,并在我的应用程序上单击一个按钮时将其保存到我的桌面上。问题是,无论我做什么,这个文件都不会显示在桌面或其他任何地方。我正在使用此方法下载按钮 -(void) Savefile { NSFileManager *filemanager = [NSFileManager defaultManager]; [self.downloadfile getDataInBackgroundWithBlock:^(NSData *data, NSErr

我试图通过模拟器从解析中提取一个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中移动它时,它存储我的PDFfile的索引。其声明如下:

@property (retain,nonatomic) PFFile * downloadfile;
这是赛格:

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

无法将其保存到桌面。

iOS设备没有桌面。如果iOS确实有一个桌面,应用程序无论如何都无法写入,因为iOS应用程序是沙盒,所以它们不能在自己的数据存储区域之外写入

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


iOS设备没有桌面。如果iOS确实有一个桌面,应用程序无论如何都无法写入,因为iOS应用程序是沙盒,所以它们不能在自己的数据存储区域之外写入

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


您尝试下载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;
}

希望这将帮助您愉快地编码:)

您尝试下载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;
}

希望这将帮助您愉快地编码:)

忽略iOS应用程序无法访问其沙箱之外的文件这一重要问题,请注意
writeToFile:
createFileAtPath:
方法采用文件路径,而不是文件URL。去掉前面的
文件://
。取消对
createFileAtPath:contents:attributes:
的调用。您只需要调用
writeToFile:
@rmaddy,所以我必须在沙箱中。有没有一种方法可以在侧面板的资源中显示文件?或者无法查看文件?忽略iOS应用程序无法访问其沙箱外部文件这一重要问题,请注意
writeToFile:
createFileAtPath:
方法采用文件路径,而不是文件URL。去掉前面的
文件://
。取消对
createFileAtPath:contents:attributes:
的调用。您只需要调用
writeToFile:
@rmaddy,所以我必须在沙箱中。有没有一种方法可以在侧面板的资源中显示文件?还是看不到文件?啊,谢谢你。我根本没有意识到这一点。现在我必须弄清楚这个路径问题,因为我的库不显示finder默认情况下不显示所有文件夹-在finder的“Go”菜单中选择“Go to finder”,然后进入“Library”Ok谢谢,我想如果你进入Xcode Organizer,我会找到一个更简单的方法,它会告诉你文件的路径。所以当我写这个文件的时候,按照我得到的路径,我能够看到它,对吗?好的,我终于让它工作了。但我注意到,当我下载一个文件时,我不能再下载它了。或者,如果我下载了另一个,它将替换旧的。我想让它不这样做,这样用户就可以下载不同的文件而不必删除以前的文件。然后你需要将它另存为不同的文件名。您可以使用
NSFileManager
fileExistsAtPath
方法在写入文件之前进行检查,如果检查了,则可以以某种方式操作文件名,比如添加后缀“-1”。您需要在循环中执行此操作,直到
fileExistsAtPath
返回否,因为可能已经有了-1和-2,依此类推,谢谢。我根本没有意识到这一点。现在我必须弄清楚这个路径问题,因为我的库不显示finder默认情况下不显示所有文件夹-在finder的“Go”菜单中选择“Go to finder”,然后进入“Library”Ok谢谢,我想如果你进入Xcode Organizer,我会找到一个更简单的方法,它会告诉你文件的路径。所以当我写这个文件的时候,按照我得到的路径,我能够看到它,对吗?好的,我终于让它工作了。但我注意到,当我下载一个文件时,我不能再下载它了。或者,如果我下载了另一个,它将替换旧的。我想让它不这样做,这样用户就可以下载不同的文件而不必删除以前的文件。然后你需要将它另存为不同的文件名。您可以使用
NSFileManager
fileExistsAtPath
方法在写入文件之前进行检查,如果检查了,则可以以某种方式操作文件名,比如添加后缀“-1”。您需要在循环中执行此操作,直到
fileExistsAtPath
返回NO,因为可能已经存在-1和-2,以此类推