2

I'm looking for some objective-c code to query a URL and give the download size, similar to this example: Checking Download size before download

but in objective-c, and using ASIHttp is OK.

Community
  • 1
  • 1
amleszk
  • 6,192
  • 5
  • 38
  • 43
  • possible duplicate of [Checking Download size before download](http://stackoverflow.com/questions/657826/checking-download-size-before-download) – user207421 Mar 20 '12 at 00:44

3 Answers3

6

Initiate the HTTP HEAD request:

NSURL *url = [NSURL URLWithString:@"http://www.google.com/"];
NSMutableURLRequest *httpRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
[httpRequest setHTTPMethod:@"HEAD"];
NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:httpRequest delegate:self];

Implement the delegate:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)aResponse {
   long long filesize = [aResponse expectedContentLength];
}
Andy Friese
  • 6,349
  • 3
  • 20
  • 17
0

New since iOS 9, should use NSURLSession instead of NSURLConnection which is deprecated:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:@"http://www.google.com/"
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:10];
[request setHTTPMethod:@"HEAD"];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config
                                                      delegate:nil
                                                 delegateQueue:queue];
[[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
  {
      NSLog(@"%lld", response.expectedContentLength);
  }] resume];
Yuchen
  • 30,852
  • 26
  • 164
  • 234
0

The answer is the same. HTTP HEAD request. There is nothing language-specific in the post you cited.

user207421
  • 305,947
  • 44
  • 307
  • 483