ios - 使用 "Pause"和 "Resume"功能从服务器下载数据

标签 ios iphone objective-c nsurlconnection nsdata

我想下载具有暂停/恢复功能的文件。我阅读了苹果文档,在那里我得到了 NSUrldownload 它支持相同的功能,但不适用于 iOS。我尝试使用 NSUrlconnection,但不起作用。 我不想使用任何第三方库,我想自己实现它,下面是我尝试过的代码片段

NSString *fileName = [NSString stringWithFormat:@"~%@",[[url componentsSeparatedByString:@"/"] lastObject]];

int dataLength = [[self checkDocDirectoryforFileName:fileName] length];
//dataLength = 0;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];

[request setValue:@"audio/mpeg" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"bytes" forHTTPHeaderField:@"Accept-Ranges"];
[request setValue:@"Keep-Alive" forHTTPHeaderField:@"Connection"];
[request setValue:[NSString stringWithFormat:@"%d",dataLength] forHTTPHeaderField:@"Content-Length"];
NSLog(@"Request header  %@", [request allHTTPHeaderFields]);
NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];

最佳答案

关于ios - 使用 "Pause"和 "Resume"功能从服务器下载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22088060/

相关文章:

ios - 其他接口(interface)/设备的操作表

objective-c - 排序和过滤核心数据关系的更有效方法是什么?

iphone - 事件文本字段上的键盘滚动 - 滚动到 View 之外?

iphone - 如何在不实际缩放的情况下更改 View 缩放值?

ios - 如何根据对应的UIImageView的尺寸来决定1x、2x、3x的图片尺寸?

ios - NSDecimal 等于 NSDecimalNumber 值?

ios - ios 中的 three.js 视频纹理在单独启动的播放器中播放 - 想法?

ios - 是否有任何公共(public) API 可以检测 iOS 16(beta 3)中的锁定模式?

ios - UITableViewRowAction 在 iOS 11 中太宽

iphone - 没有互联网连接时,iphone google analytics 调用会发生什么情况?