Cocoa - NSFileManager removeItemAtPath Not Working

前端 未结 6 657
再見小時候
再見小時候 2021-01-21 04:03

I am trying to delete a file, but somehow nsfilemanager will not allow me to do so. I do use the file in one line of code, but once that action has been ran, I want the file del

6条回答
  •  再見小時候
    2021-01-21 04:42

    Default method AFNetworking 3.0 don't refresh that you downloader files.

    If you want rewrite this file in Objective-C +iOS9.0, you need do that:

    - (NSURLSessionDownloadTask *) downloadDocsFromUrl:(NSString *) url withSuccesBlock:(DocModelBlock) docModelBlock withErrorBlock:(ErrorBlock) errorBlock {
    
        NSURL *URL = [NSURL URLWithString:url];
        NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    
        NSURLSessionDownloadTask *downloadTask = [self.sessionManager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
    
            NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
            NSFileManager *fileManager = [NSFileManager defaultManager];
            NSError *error;
    
            NSURL *documentsDirectoryURL = [fileManager URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:&error];
    
            if ([httpResponse statusCode] == 200) {
                NSURL *urlPath = [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
    
                if ([fileManager fileExistsAtPath:urlPath.path]) {
                    [fileManager removeItemAtPath:urlPath.path error:&error];
                }
            }
    
            return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
        } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
            if (error) {
                errorBlock(error);
            } else {
                docModelBlock(filePath);
            }
        }];
        [downloadTask resume];
    
        return downloadTask;
    }
    

提交回复
热议问题