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

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

1684475100002.png


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

@interface ViewController()<UIDocumentInteractionControllerDelegate,UIDocumentPickerDelegate>
@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 <NSURL *>*)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:拷贝文件到外部路径,同时可以修改该拷贝。


目录
相关文章
|
2月前
|
移动开发 前端开发 数据安全/隐私保护
iOS发布证书.p12文件无密码解决办法及导出带密码的新.p12文件方法
iOS发布证书.p12文件无密码解决办法及导出带密码的新.p12文件方法
45 0
|
2月前
|
JSON 安全 JavaScript
怎么保护苹果手机移动应用程序ipa中文件安全?
怎么保护苹果手机移动应用程序ipa中文件安全?
23 1
|
1月前
|
iOS开发
解决使用document.activeElement.blur()禁止弹出手机默认键盘ios版本17以上会出现闪屏问题
解决使用document.activeElement.blur()禁止弹出手机默认键盘ios版本17以上会出现闪屏问题
17 2
|
2月前
|
移动开发 监控 小程序
mPaaS常见问题之uniapp ios端云打包的配置config文件如何解决
mPaaS(移动平台即服务,Mobile Platform as a Service)是阿里巴巴集团提供的一套移动开发解决方案,它包含了一系列移动开发、测试、监控和运营的工具和服务。以下是mPaaS常见问题的汇总,旨在帮助开发者和企业用户解决在使用mPaaS产品过程中遇到的各种挑战
33 0
|
2月前
|
iOS开发 开发者
【教程】uni-app iOS 打包解决 profile 文件与私钥证书不匹配问题
【教程】uni-app iOS 打包解决 profile 文件与私钥证书不匹配问题
|
2月前
|
Web App开发 前端开发 网络安全
前端分析工具之 Charles 录制 Android/IOS 手机的 https 应用
【2月更文挑战第21天】前端分析工具之 Charles 录制 Android/IOS 手机的 https 应用
61 1
前端分析工具之 Charles 录制 Android/IOS 手机的 https 应用
|
2月前
|
开发工具 数据安全/隐私保护 Android开发
ipa文件怎么安装到iPhone手机上?
ipa文件怎么安装到iPhone手机上?
|
3月前
|
Android开发
用Airtest快速实现手机文件读写与删除功能
用Airtest快速实现手机文件读写与删除功能
|
3月前
|
iOS开发
ipa文件安装到ios系统
ipa文件安装到ios系统
32 0
|
3月前
|
缓存 小程序 Android开发
mPaaS问题之iOS调用插件的时候提示没有配置mpaas. Config文件如何解决
mPaaS配置是指在mPaaS平台上对移动应用进行的各项设置,以支持应用的定制化和优化运行;本合集将提供mPaaS配置的操作指南和最佳实践,助力开发者高效管理和调整移动应用的设置。
70 1