iOS NSURLSessionDownloadTask设置代理文件下载的示例

2025-05-29 0 70

通过设置代理我们可以拿到下载进度,对于大文件,我们还需要做到开始、暂停、继续以及取消等相应操作,这篇文章先简单的介绍一下通过代理来实现文件下载的问题:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80
#import "ViewController.h"

@interface ViewController ()<NSURLSessionDownloadDelegate>

@end

@implementation ViewController

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

[self delegate];

}

-(void)delegate

{

//1.url

NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/images/minion_03.png"];

//2.创建请求对象

NSURLRequest *request = [NSURLRequest requestWithURL:url];

//3.创建session :注意代理为NSURLSessionDownloadDelegate

NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];

//4.创建Task

NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request];

//5.执行Task

[downloadTask resume];

}

#pragma mark ----------------------

#pragma mark NSURLSessionDownloadDelegate

/**

* 写数据

*

* @param session 会话对象

* @param downloadTask 下载任务

* @param bytesWritten 本次写入的数据大小

* @param totalBytesWritten 下载的数据总大小

* @param totalBytesExpectedToWrite 文件的总大小

*/

-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite

{

//1. 获得文件的下载进度

NSLog(@"%f",1.0 * totalBytesWritten/totalBytesExpectedToWrite);

}

/**

* 当恢复下载的时候调用该方法

*

* @param fileOffset 从什么地方下载

* @param expectedTotalBytes 文件的总大小

*/

-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes

{

NSLog(@"%s",__func__);

}

/**

* 当下载完成的时候调用

*

* @param location 文件的临时存储路径

*/

-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location

{

NSLog(@"%@",location);

//1 拼接文件全路径

NSString *fullPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:downloadTask.response.suggestedFilename];

//2 剪切文件

[[NSFileManager defaultManager]moveItemAtURL:location toURL:[NSURL fileURLWithPath:fullPath] error:nil];

NSLog(@"%@",fullPath);

}

/**

* 请求结束

*/

-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error

{

NSLog(@"didCompleteWithError");

}

@end

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 iOS NSURLSessionDownloadTask设置代理文件下载的示例 https://www.kuaiidc.com/90241.html

相关文章

发表评论
暂无评论