iOS下载前获取文件大小
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS下载前获取文件大小相关的知识,希望对你有一定的参考价值。
我找不到适合我的解决方案。但是我需要获取正在下载的视频的文件大小,这样我才能确保用户手机上有足够的空间。
我的想法是检查视频的大小,然后如果用户有空间,我会下载它。有什么建议?
NSURL *url = [NSURL URLWithString:stringURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {...}];
答案
这是其他答案的变体,它使用函数(在Swift 4中)在检索大小时调用闭包:
func getDownloadSize(url: URL, completion: @escaping (Int64, Error?) -> Void) {
let timeoutInterval = 5.0
var request = URLRequest(url: url,
cachePolicy: .reloadIgnoringLocalAndRemoteCacheData,
timeoutInterval: timeoutInterval)
request.httpMethod = "HEAD"
URLSession.shared.dataTask(with: request) { (data, response, error) in
let contentLength = response?.expectedContentLength ?? NSURLSessionTransferSizeUnknown
completion(contentLength, error)
}.resume()
}
以下是此功能的使用方法:
let url = URL(string: "https://upload.wikimedia.org/wikipedia/commons/7/70/Example.png")!
getDownloadSize(url: url, completion: { (size, error) in
if error != nil {
print("An error occurred when retrieving the download size: (error.localizedDescription)")
} else {
print("The download size is (size).")
}
})
另一答案
Swift 3:由于您正在调用dataTask,因此无法使用块外部的值,因此请以这种方式使用它。
var contentLength: Int64 = NSURLSessionTransferSizeUnknown
let request = NSMutableURLRequest(url: url as URL, cachePolicy: NSURLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 30.0);
request.httpMethod = "HEAD";
request.timeoutInterval = 5;
let group = DispatchGroup()
group.enter()
URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
contentLength = response?.expectedContentLength ?? NSURLSessionTransferSizeUnknown
//Here you should use the value
print("contentLength",contentLength)
group.leave()
}).resume()
另一答案
使用此函数可获取远程URL大小。请注意此函数是同步的并且将阻塞线程,因此从与主线程不同的线程调用它:
extension NSURL {
var remoteSize: Int64 {
var contentLength: Int64 = NSURLSessionTransferSizeUnknown
let request = NSMutableURLRequest(URL: self, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 30.0);
request.HTTPMethod = "HEAD";
request.timeoutInterval = 5;
let group = dispatch_group_create()
dispatch_group_enter(group)
NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
contentLength = response?.expectedContentLength ?? NSURLSessionTransferSizeUnknown
dispatch_group_leave(group)
}).resume()
dispatch_group_wait(group, dispatch_time(DISPATCH_TIME_NOW, Int64(5 * NSEC_PER_SEC)))
return contentLength
}
}
然后在与主线程不同的线程上,在需要的任何地方调用remoteSize变量:
let size = url.remoteSize
另一答案
SWIFT 3:
extension NSURL {
var remoteSize: Int64 {
var contentLength: Int64 = NSURLSessionTransferSizeUnknown
let request = NSMutableURLRequest(url: self as URL, cachePolicy: NSURLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 30.0);
request.httpMethod = "HEAD";
request.timeoutInterval = 5;
let group = DispatchGroup()
group.enter()
URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
contentLength = response?.expectedContentLength ?? NSURLSessionTransferSizeUnknown
group.leave()
}).resume()
return contentLength
}
}
以上是关于iOS下载前获取文件大小的主要内容,如果未能解决你的问题,请参考以下文章