Search code examples
iosobjective-cnsfilemanager

No such file or directory iOS


I am trying to get size of the file by its path. Those files are saved videos from my app. i used the below code its returning the error saying there is no file or directory.

NSString *urlPath = [self.localPathArray objectAtIndex:indexPath.row];
NSError *err;
NSDictionary * properties = [[NSFileManager defaultManager] attributesOfItemAtPath:urlPath error:&err];

This is how am saving the file path after downloading the file

 - (void)URLSession:(NSURLSession *)session assetDownloadTask:
(AVAssetDownloadTask *)assetDownloadTask didFinishDownloadingToURL:(NSURL *)location {


        NSString *localPath = location.relativePath;
        NSLog(@"localPath: %@", localPath);

       [[NSUserDefaults standardUserDefaults]setValue:self.localPathArray forKey:@"AssetPath"];
    }

See the error screenshot and my app storage details screenshot below Error Storage details


Solution

  • The path with reference to root directory is called absolute. The path with reference to current directory is called relative

    so try

     NSString *localPath = location.path;
    

    instead of

      NSString *localPath = location.relativePath;
    

    for e.g

      - (void)URLSession:(NSURLSession *)session assetDownloadTask:
    (AVAssetDownloadTask *)assetDownloadTask didFinishDownloadingToURL:(NSURL *)location {
    
    
            NSString *localPath = location.path;
            NSLog(@"localPath: %@", localPath);
            [self.localPathArray addObject: localPath];
           [[NSUserDefaults standardUserDefaults]setValue:self.localPathArray forKey:@"AssetPath"];
        }
    

    for sample see this answer already answered in Stack overflow