Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

wotmania505

macrumors newbie
Original poster
Feb 28, 2011
6
0
Michigan
I wrote some code to download a URL to my disk, using an NSURLRequest and an NSURLDownload. The download starts fine (at least, it sends downloadDidBegin:(NSURLDownload *)download), but then it sends

Code:
-(void)download:(NSURLDownload *)download didFailWithError:(NSError *)error
My implementation of the function is as follows:

Code:
{
[download release];

NSLog(@"Download failed! Error - %@ %@",
      [error localizedDescription],
      [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
The logged error is

2011-07-17 01:48:43.194 URLDownloader[28031:903] Download failed! Error - The operation couldn’t be completed. (NSURLErrorDomain error -3001.) [the URL in the NSURLRequest]

From what I can tell, this is the NSURLErrorCannotOpenFile error. I cannot find any explanation of why this error occurs, other then "Returned when NSURLDownload was unable to open the downloaded file on disk." from the documentation. So, why is this happening?

Fact that might be helpful(?): NSURLErrorCannotOpenFile is enumerated as kCFURLErrorCannotOpenFile. I have no idea what this means either....
 
Last edited:
If the problem is with writing the file to the disk, then the most obvious thing to look at is how you're specifying the file to write.

There appears to be one NSURLDownload method for setting a specific destination file on disk, and several delegate methods associated with the destination file.

If you're using a specific location on disk, what is it? Do you have permission to write there? Did you specify overwriting? Post your code that sets the destination.

If you're relying on delegate methods, post their code.
 
Alright, my code (from the download) is:

Code:
 NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:theString]]; //theString is some URL
            
            NSURLDownload *theDownload = [[NSURLDownload alloc] initWithRequest:theRequest delegate:self];
            
            if (theDownload)
            {
                NSLog(@"Download");
                NSURL *folderLoc = [self chosenFolder]; //chosenFolder is a folder in my User Directory
                NSString *theDestination = [[folderLoc absoluteString] stringByAppendingString:[self fileNameDetermine:chapNum]];
                NSLog(@"%@",theDestination);
                NSFileManager *theManager = [[NSFileManager alloc] init];
                if ([theManager fileExistsAtPath:theDestination]) {
                    NSLog(@"Clear out the chosen directory!");
                }
                else
                {
                    [theDownload setDestination:theDestination allowOverwrite:NO];
                }
                
            }

The implementation for fileNameDetermine:

Code:
-(NSString*)fileNameDetermine:(int)curChap
{
    NSString *baseString = [[NSNumber numberWithInt:curChap] stringValue];
    NSMutableString *attachString = [[NSMutableString alloc] init];
    
    if (curChap<10) 
    {
        [attachString setString:@"00"];
    }
    else
    {
        [attachString setString:@"0"];
    }
    
    NSString *newString = [NSString stringWithFormat:@"%@%@.html", attachString, baseString];
    [baseString release];
    [attachString release];
    
    
    return newString;
}
 
Last edited:
When you run your code and NSLog the pathname of theDestination file, what does it show?
Post actual output, don't paraphrase it.

Do you have write permission on theDestination file?

Do all the directories leading up to the theDestination file exist?


Your code also has other problems, such as leaks. Ignore that for now, because it's not relevant to the destination-file problem.


EDIT
Code:
NSLog(@"Download");
NSURL *folderLoc = [self chosenFolder]; //chosenFolder is a folder in my User Directory

At this point, folderLoc is an NSURL. It's not a path string; it's an NSURL object. As a consequence, the red-hilited code here:
Code:
NSString *theDestination = [[[COLOR="Red"]folderLoc absoluteString[/COLOR]] stringByAppendingString:[self fileNameDetermine:chapNum]];
will almost certainly produce a string that starts with "file://" and may contain a hostname. That's a URL string, not a path string.

If you carefully read the setDestination: method description of NSURLDownload, it doesn't say "a URL string", it says a path.

There is a method of NSURL that returns the path. Please read the class reference doc for NSURL.
 
Last edited:
Some thoughts:

Do not alloc the file manager, use the class method +defaultManager to obtain it (it already exists).

Look up the NSString methods for handling paths and path components and use them instead of the primitive formatting and appending methods.

Log what you get from -absoluteString (the NSURL method): NSFileManager does not like some aspects of URL formatting, such as "file:///" and the percent escape codes (e.g., space = %20). NSString also has methods for dealing with this.
 
Thanks! I guess I completely misunderstood how to convert NSURL to NSString paths. Again, thank you both for your help!

Also, if there's a way to mark this complete, could someone do that, or let me know? thanks.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.