1

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. 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. Its declared as this:

@property (retain,nonatomic) PFFile * downloadfile;

this here is the segue:

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

can't get it to save to desktop.

DannyK
  • 47
  • 8
  • 1
    Ignoring the important issue that an iOS app can't access files outside its sandbox, please note that the `writeToFile:` and `createFileAtPath:` methods take file paths, not file URLs. Get rid of the leading `file://`. And get rid of the call to `createFileAtPath:contents:attributes:`. You only need the call to `writeToFile:`. – rmaddy Mar 25 '14 at 21:01
  • @rmaddy So i has to be within the sandbox. Is there a way to display the file within lets say resources on the side panel? Or is it impossible to see the file? – DannyK Mar 25 '14 at 21:44

2 Answers2

6

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 :-

-(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 :)

Shubhank
  • 21,721
  • 8
  • 65
  • 83
Rajat
  • 1,043
  • 12
  • 23
1

iOS devices don't have a desktop. 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.

Even when running on the simulator, you cannot write to the OSX Desktop, because the app is sandboxed into the Simulator file storage. You can save the file to your app sandbox and then access the simulator storage from finder -

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

Community
  • 1
  • 1
Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • Ahhh thanks man. I was not aware of that at all. Now i gotta figure out this path thing since my Library doesn't show – DannyK Mar 25 '14 at 21:33
  • Finder doesn't show all folders by default - Select "Go to finder" in the "Go" menu in finder and then enter "Library" – Paulw11 Mar 25 '14 at 21:53
  • Ok thanks, i think i found an easier way if you go into Xcode organiser it tells you the path for files. So when i write this file, to the path I have been given I soul be able to see it right? – DannyK Mar 25 '14 at 22:39
  • Ok i finally got it to work. what i noticed though is that when I download a file I can't download it again. Or if i download another it replaces the old one. Id like to make it so it doesn't do this, so that a user can download different ones without previous getting deleted. – DannyK Mar 25 '14 at 23:13
  • Then you need to save it as a different file name. You can use the `fileExistsAtPath` method of `NSFileManager` to check before you write the file and if it does then you can manipulate the file name in some way - say adding suffix such as "-1". You will need to do this in a loop until `fileExistsAtPath` returns NO as there may already be a -1 and a -2 and so on – Paulw11 Mar 25 '14 at 23:17
  • Geeez seems like a lot to take in, imma look it up – DannyK Mar 25 '14 at 23:44
  • Oh just for your info, I actually managed to save the file to my Desktop. So I don't know what that was all about – DannyK Mar 26 '14 at 01:12