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:拷贝文件到外部路径,同时可以修改该拷贝。


目录
相关文章
|
4月前
|
JSON 安全 JavaScript
怎么保护苹果手机移动应用程序ipa中文件安全?
怎么保护苹果手机移动应用程序ipa中文件安全?
53 1
|
4月前
|
移动开发 前端开发 数据安全/隐私保护
iOS发布证书.p12文件无密码解决办法及导出带密码的新.p12文件方法
iOS发布证书.p12文件无密码解决办法及导出带密码的新.p12文件方法
135 0
|
2月前
|
人工智能 搜索推荐 vr&ar
苹果手机iOS18最新升级:植入AI人工智能,国内百度文心一言,国外GPT4o来辅助
iOS 18亮点速览:AI强化的Siri、RCS安卓消息兼容、自定义主屏、辅助功能增强,VR进步,新隐私工具,包括锁定APP和眼动追踪。Passwords app保障安全,Apple Intelligence提升个性化体验。
107 1
|
2月前
|
测试技术 Android开发 iOS开发
IOS手机自动化一些工具的简单有哪些?
IOS手机自动化一些工具的简单有哪些?
|
4月前
|
Android开发
【苹果安卓通用】xlsx 和 vCard 文件转换器,txt转vCard文件格式,CSV转 vCard格式,如何批量号码导入手机通讯录,一篇文章说全
本文介绍了如何快速将批量号码导入手机通讯录,适用于企业客户管理、营销团队、活动组织、团队协作和新员工入职等场景。步骤包括:1) 下载软件,提供腾讯云盘和百度网盘链接;2) 打开软件,复制粘贴号码并进行加载预览和制作文件;3) 将制作好的文件通过QQ或微信发送至手机,然后按苹果、安卓或鸿蒙系统的指示导入。整个过程简便快捷,可在1分钟内完成。
|
4月前
|
安全 开发者 iOS开发
iOS16系统手机设置开启开发者模式才能安装ipa包
iOS16系统手机设置开启开发者模式才能安装ipa包
119 1
|
4月前
|
开发工具 iOS开发
如何导出iPhone手机中app共享文件夹的文件与闪退日志的收集
如何导出iPhone手机中app共享文件夹的文件与闪退日志的收集
55 1
|
4月前
|
安全
要从手机传输一个大文件到笔记本电脑,蓝牙和 wifi 谁快?
要从手机传输一个大文件到笔记本电脑,蓝牙和 wifi 谁快?
|
3月前
|
PHP
软件下载网站源码附手机版和图文教程
PHP游戏应用市场APP软件下载平台网站源码+手机版 可自行打包APP,带下载统计,带多套模板,带图文教程,可以做软件库,也可以做推广app下载等等,需要的朋友可以下载
29 0
|
4月前
|
安全 Android开发 iOS开发
探索安卓与iOS手机操作系统的发展趋势与差异
在移动互联网时代,安卓和iOS手机操作系统一直处于激烈的竞争之中。本文将从技术角度出发,探讨两大操作系统的发展趋势、特点以及差异,并分析其对用户体验和开发者生态的影响,旨在为读者提供对移动操作系统发展方向的深入了解。

热门文章

最新文章

下一篇
云函数