How to find the file size before downloading it in iOS 7?

It is required to find the file size on any server before downloading it in iOS 7 ... I have the NSURLConnectionDelegate method, but it is deprecated after iOS 4.3

Here is the method:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
+4
source share
7 answers

Instead, you should use NSURLConnectionDataDelegateNote: DATADelegate

and its didReceiveResponse:method is to send a HEAD request to get only the header:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;

Then you can get the size of the response with the @Port clause:

long long size = [response expectedContentLength];
+2
source

Make a request using the HEAD method. For example:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"HEAD"];

This request will be identical to GET, but it will not return the body. Then call

long long size = [response expectedContentLength];

NSURLConnection ( , NSURLSession):

NSURL *URL = [NSURL URLWithString:@"http://www.google.com"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request setHTTPMethod:@"HEAD"];
NSHTTPURLResponse *response;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error: nil];
long long size = [response expectedContentLength];
NSLog(@"%lld",size);

Last-Modified ( , ).

if ([response respondsToSelector:@selector(allHeaderFields)]) {
    NSString *lastModifiedString = [[response allHeaderFields] objectForKey:@"Last-Modified"];
}
+11

, NSURLResponse, :

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

}
-(NSURLRequest *)connection:(NSURLConnection *)connection
        willSendRequest:(NSURLRequest *)request
       redirectResponse:(NSURLResponse *)response {
}

.

long long size = [response expectedContentLength];
+1

Swift :

1: NSURLConnectionDataDelegate.

class ViewController: UIViewController,NSURLConnectionDataDelegate {

2. .

var request:NSMutableURLRequest = NSMutableURLRequest(URL: NSURL(string: "http://www.twitter.com")!)

// Use **HEAD** request to get http header information.

request.HTTPMethod = "HEAD"

3: .

var connection:NSURLConnection = NSURLConnection(request: request, delegate: self)!

4. NSURLConnectionDataDelegate

func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse)
{
    var size = response.expectedContentLength
    println("size : \(size)")
}

...

import UIKit

class ViewController: UIViewController,NSURLConnectionDataDelegate {

override func viewDidLoad() {
    super.viewDidLoad()


    var request:NSMutableURLRequest = NSMutableURLRequest(URL: NSURL(string: "http://www.twitter.com")!)
    request.HTTPMethod = "HEAD"

    var connection:NSURLConnection = NSURLConnection(request: request, delegate: self)!

}

func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse)
{
    var size = response.expectedContentLength
    println("size : \(size)")
}
}
+1

, .,,

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse
                                                                      *)response
{
    NSHTTPURLResponse * pHttpUrlResponse = (NSHTTPURLResponse *) response;

    if ([response respondsToSelector:@selector(allHeaderFields)])
    {
        NSString * fileLength = [NSString stringWithFormat:@"%lld",[pHttpUrlResponse expectedContentLength]];
    }    
}

:)

0

, URlSession :

func getPdfSize(for url: URL) {
    var request = URLRequest(url: url)
    request.httpMethod = "HEAD"

    URLSession.shared.dataTask(with: request) { [weak self] (_, response, _) in
        if let response = response {
            let size = response.expectedContentLength
            DispatchQueue.main.async {
                self?.fileSizeLabel.text = ByteCountFormatter.string(fromByteCount: size, countStyle: .file)
            }
        }
    }.resume()
}

, iOS ByteCountFormatter .

Swift 5

0

,

NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL error:&attributesError];
NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
long long fileSize = [fileSizeNumber longLongValue];

:)

-1

Source: https://habr.com/ru/post/1017012/


All Articles