iOS下载前获取文件大小

时间:2015-07-31 00:47:22

标签: ios download size nsurlsession nsurlsessiondownloadtask

我找不到适合我的解决方案。但是我需要获取正在下载的视频的文件大小,这样我才能确保用户手机上有足够的空间。

我的想法是检查视频的大小,然后如果用户有空间,我会下载它。有什么建议?

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) {...}];

4 个答案:

答案 0 :(得分:6)

这是其他答案的变体,它使用函数(在Swift 4中)在检索大小时调用闭包:

#include <stdio.h>
char f1(char* p)
{
    int i=0,c=1;
    while(*(p+i))
    {
        if(*p==*(++p))
        {
            c+=1;
            p++;
            i++;
            continue;           
        }
        else
        {
            if(c>1)
            {
                printf("%c%d",*p,c);
            }
            else
            {
                printf("%c",*p);
            }
        }
    i++;
    }
}



int main()
{
    int n,i,m;
    char kar[1000];
    scanf("%d",&n);
    for(i=1;i<=(2*n);++i)
    {
        scanf("%d",&m);
        scanf("%s",kar[1000]);
        if(m==1)
        {
            f1(kar);
        }

    }
}

以下是如何使用此功能:

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()
}

答案 1 :(得分:0)

斯威夫特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()

答案 2 :(得分:-1)

使用此功能获取远程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

答案 3 :(得分:-1)

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
    }
}