ios - URL File Size With NSURLConnection - Swift -


I am trying to get a file size from the URL before downloading

obj here -c is the code

  NS URL * URL = [NSD URL Youth Ringtones: "User url"]; NSMutableURLRequest * Request = [NSMutableURL request request with url: URL]; [Set http system: @ "head"]; NSHTTPURL response * feedback; [Send NSRR connection SynchronousAust: Returning Response Request: & amp; Reaction error: zero]; Longer size = [expected content lang];   

and here swift code

  var url: NSURL = NSURL (string: "ExmapleURL") var request: NSMutableURLRequest = NSMutableURLRequest (URL: url) request .HTTPMethod = "HEAD" var response = NSHTTPURLResponse () NSURLConnection.sendSynchronousRequest (Request, Returning Response: And Response, Error: Zero)   

But I have an error here < Previous> NSURLConnection.sendSynchronousRequest (request, returning response: and response, error: zero) 'NSHTCPRSpons' is not similar to 'NSURLResponse?'

Did I miss something fast here?

The response parameter is type

  AutoreleasingUnsafeMutablePointer & lt; NSURLResponse? & Gt;   

Which means that you can give the argument of an alternative NSURLResponse as arguments:

  Var response: NSURLResponse? NSURLConnection.sendSynchronousRequest (Request, Returning Response: & amp; Response, Error: Zero)   

You then have a conditionally a NSHTTPURLResponse :

  if httpResponse = feedback? NSHTTPURLResponse {println (httpResponse.expectedContentLength)}   

Note that you should check the return value of sendSynchronousRequest () , which is zero If no connection can be made.

This method is advised to call only for a different thread (or using the sendAsynchronousRequest () ) because it may take some time to do one Connections especially when using a cellular network ??? And the main thread will be blocked otherwise.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -