Dan Rosenstark
Dan Rosenstark

Reputation: 69767

Get Filename from NSURLRequest Without Loading

When a link in a UIWebView is clicked, the delegate gets an NSURLRequest, from which I want the suggested filename.

Getting the Suggested Filename

I currently use the NSURLRequest to create an NSURLConnection. The NSURLConnectionDelegate receives a NSURLResponse object, which contains the suggestedFilename.

Without Loading the URL

Is there any way to get the suggestedFilename from the server without waiting for the entire NSURLResponse to be loaded?

Upvotes: 0

Views: 312

Answers (1)

dgatwood
dgatwood

Reputation: 10417

You're misunderstanding what an NSURLResponse object is. When the didReceiveResponse method is called, the OS has not received anything beyond the headers. Thus, that response object does not contain the body of the response from the server. It contains only the headers.

Inside the NSURLResponse object, you should be able to access the headers that the server sent. Look at the Content-Disposition header. You'll have to pull it apart yourself.

At that point, you can cancel the request if you don't want to receive the data. With that said, the server will be sending data while you do that, so if you want to avoid receiving any of the data, change the request type from GET to HEAD. (If you're doing this underneath a web view, you would do that by using a custom NSURLProtocol. Such an exercise is beyond the scope of this question....)

Upvotes: 1

Related Questions