The operation couldn’t be completed. (Cocoa error 260.)

16,372

Solution 1

Cocoa error 260 is a NSFileReadNoSuchFileError (as listed in FoundationErrors.h), meaning the file could not be found at the path you specified.

The problem is that your path still contains encoded spaces (%20), because you're basing it on the URL. You can simply do this:

NSData *pdfData = [NSData dataWithContentsOfFile:[document.fileURL path]];

Solution 2

Try to use NSBundle

NSString *newPath = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"pdf"]

Edit:

Than you can use bundleWithPath method, here is an example:

NSString *documentsDir= [NSString stringWithFormat:@"%@/Documents", NSHomeDirectory()];

NSString *newPath= [[NSBundle bundleWithPath:documentsDir] bundlePath];
Share:
16,372

Related videos on Youtube

rptwsthi
Author by

rptwsthi

I am an ios/android developer. And a hobbyist photographer. Love traveling, nature, movies, pictures, writing, and coding. I am also on Instagram, along with mentioned web presence. I use MacBook Pro to code. Jetter Pen And Diary to write. And My phone, Nikon D5200(occasionally) to click.

Updated on June 04, 2022

Comments

  • rptwsthi
    rptwsthi almost 2 years

    I'm new on IOS development and i'm working on a pdf application and i need to store a PDF file on a NSData variable, I have the PDF path but i get this message error when i try to put this pdf on the NSData variable using dataWithContentsOfFile her is my simple code :

    NSError *error;
    NSString *PdfPath = [NSString stringWithFormat:(@"%@"),document.fileURL ];
    NSString *newPath = [PdfPath stringByReplacingOccurrencesOfString:@"file://localhost" withString:@""];
    NSLog(@"OriginalPdfPath => %@", newPath);
    NSData *pdfData = [NSData dataWithContentsOfFile:newPath options:NSDataReadingUncached error:&error];
    

    NB : the pdf path is in this format : /Users/bluesettle/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/BBEF320E-7E2A-49DA-9FCF-9CFB01CC0402/ContractApp.app/Pro.iOS.Table.Views.pdf

    thanks for your help

    • Andreas Ley
      Andreas Ley almost 11 years
      What's lPdfPath? Why do you replace the substring? What's the output of newPath at your NSLog line?
    • Admin
      Admin almost 11 years
      I mean PdfPath its just an error and i replace the string because i found that the path need to be without the "file://localhost" prefix and the output for the NSlog is "/Users/bluesettle/Library/Application%20Support/iPhone%20Si‌​mulator/6.0/Applicat‌​ions/BBEF320E-7E2A-4‌​9DA-9FCF-9CFB01CC040‌​2/ContractApp.app/Pr‌​o.iOS.Table.Views.pd‌​f"
  • Admin
    Admin almost 11 years
    for me I need to use files that are not in the resources folder
  • Admin
    Admin almost 11 years
    I removed the %20 from the path using [lPdfPath stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEnc‌​oding]; the 620 error is gone but i get the NSData = (Null)
  • Admin
    Admin almost 11 years
    when i use the absoluteString i get always the %20 on the path that why i use stringByReplacingPercentEscapesUsingEncoding
  • Andreas Ley
    Andreas Ley almost 11 years
    @hatimhaffane Ah, sorry, you're right. Post edited to provide a working solution.
  • Admin
    Admin almost 11 years
    thank you but still get the NSData variable as Null i will try to find a solution
  • Andreas Ley
    Andreas Ley almost 11 years
    The current solution definitely works. If you use the method with &error, what does it say?
  • Andreas Ley
    Andreas Ley almost 11 years
    Then I suppose the file is simply empty. :)
  • Admin
    Admin almost 11 years
    your code works well i think i have an error on creating the new pdf thanks