The operation couldn’t be completed. (Cocoa error 260.)
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];
Related videos on Youtube
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, 2022Comments
-
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 almost 11 yearsWhat's
lPdfPath
? Why do you replace the substring? What's the output of newPath at yourNSLog
line? -
Admin almost 11 yearsI 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%20Simulator/6.0/Applications/BBEF320E-7E2A-49DA-9FCF-9CFB01CC0402/ContractApp.app/Pro.iOS.Table.Views.pdf"
-
-
Admin almost 11 yearsfor me I need to use files that are not in the resources folder
-
Admin almost 11 yearsI removed the %20 from the path using
[lPdfPath stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
the 620 error is gone but i get the NSData = (Null) -
Admin almost 11 yearswhen i use the
absoluteString
i get always the %20 on the path that why i usestringByReplacingPercentEscapesUsingEncoding
-
Andreas Ley almost 11 years@hatimhaffane Ah, sorry, you're right. Post edited to provide a working solution.
-
Admin almost 11 yearsthank you but still get the NSData variable as Null i will try to find a solution
-
Andreas Ley almost 11 yearsThe current solution definitely works. If you use the method with
&error
, what does it say? -
Andreas Ley almost 11 yearsThen I suppose the file is simply empty. :)
-
Admin almost 11 yearsyour code works well i think i have an error on creating the new pdf thanks