Resolved NSURLErrorCannotOpenFile Question

Discussion in 'Mac Programming' started by wotmania505, Jul 18, 2011.

  1. wotmania505, Jul 18, 2011
    Last edited: Jul 19, 2011

    wotmania505 macrumors newbie

    Joined:
    Feb 28, 2011
    Location:
    Michigan
    #1
    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....
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    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.
     
  3. wotmania505, Jul 19, 2011
    Last edited: Jul 19, 2011

    wotmania505 thread starter macrumors newbie

    Joined:
    Feb 28, 2011
    Location:
    Michigan
    #3
    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;
    }
     
  4. chown33, Jul 19, 2011
    Last edited: Jul 19, 2011

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    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
    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.
     
  5. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #5
    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.
     
  6. wotmania505 thread starter macrumors newbie

    Joined:
    Feb 28, 2011
    Location:
    Michigan
    #6
    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.
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7

Share This Page