iOS下载文件保存到手机文件指定目录

下载文件并且保存文件到手机文件指定目录,实现方法如下:

@interface ViewController()

@end

@implementation ViewController

@end

//下载文件
- (void)downloadFile{

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    
    NSURL *URL = [NSURL URLWithString:self.url]; //文件下载地址
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    
    weakify(self);
    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress *downloadProgress){        

        dispatch_async(dispatch_get_main_queue(), ^{
            self.downloadProgress = downloadProgress.fractionCompleted; //下载进度
        });   
     
    } destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {

        weakSelf.fileName = response.suggestedFilename; //文件名
        weakSelf.fileSize = [self getFileSize:response.expectedContentLength]; //文件大小
        NSURL *documentsDirectoryURL = [[NSFileManager defaultManager]URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
        NSURL *url = [documentsDirectoryURL URLByAppendingPathComponent:weakSelf.fileName]; //文件位置
        return url;

    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {        

       //下载完成
       weakSelf.filePathURL = filePath; //文件位置
    }];
    [downloadTask resume];
}

//保存文件到手机文件指定目录
- (void)saveFileToPhone {
    
    UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"file://%@",self.filePathURL]] inMode:UIDocumentPickerModeExportToService];
    documentPicker.delegate = self;
    documentPicker.modalPresentationStyle = UIModalPresentationFormSheet;
    [self presentViewController:documentPicker animated:YES completion:nil];
}

#pragma mark - UIDocumentInteractionControllerDelegate

-(UIViewController*)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController*)controller{
    return self;
}

-(UIView*)documentInteractionControllerViewForPreview:(UIDocumentInteractionController*)controller {
    return self.view;
}

-(CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController*)controller {
    return self.view.frame;
}

#pragma mark - UIDocumentPickerDelegate

- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray *)urls {
     //保存成功
}

- (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller{
     //取消保存
}

注:

UIDocumentPickerViewController有四种模式:

1 typedef NS_ENUM(NSUInteger, UIDocumentPickerMode) {
2     UIDocumentPickerModeImport,
3     UIDocumentPickerModeOpen,
4     UIDocumentPickerModeExportToService,
5     UIDocumentPickerModeMoveToService
6 } API_DEPRECATED("Use appropriate initializers instead",ios(8.0,14.0)) API_UNAVAILABLE(tvos);

UIDocumentPickerModeImport:用户选择一个外部文件,文件选择器拷贝该文件到应用沙盒,不会修改源文件。
UIDocumentPickerModeOpen:打开一个外部文件,用户可以修改该文件。
UIDocumentPickerModeExportToService:文件选择器拷贝文件到一个外部路径,不会修改源文件。
UIDocumentPickerModeMoveToService:拷贝文件到外部路径,同时可以修改该拷贝。

你可能感兴趣的:(iOS下载文件保存到手机文件指定目录)