25.12.2013 Views

URL Loading System Programming Guide - Apple Developer

URL Loading System Programming Guide - Apple Developer

URL Loading System Programming Guide - Apple Developer

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Handling Redirects and Other Request Changes<br />

occurs because the standardized, or canonical, version of the request is used for cache management. In this<br />

special case, the response passed to the delegate is nil and the delegate should simply return the provided<br />

request.<br />

The example implementation in Listing 5-1 allows canonical changes and denies all server redirects.<br />

Listing 5-1<br />

Example of an implementation of connection:willSendRequest:redirectResponse:<br />

#if FOR_NS<strong>URL</strong>SESSION<br />

- (void)<strong>URL</strong>Session:(NS<strong>URL</strong>Session *)session<br />

task:(NS<strong>URL</strong>SessionTask *)task<br />

willPerformHTTPRedirection:(NSHTTP<strong>URL</strong>Response *)redirectResponse<br />

newRequest:(NS<strong>URL</strong>Request *)request<br />

completionHandler:(void (^)(NS<strong>URL</strong>Request *))completionHandler<br />

#elif FOR_NS<strong>URL</strong>CONNECTION<br />

-(NS<strong>URL</strong>Request *)connection:(NS<strong>URL</strong>Connection *)connection<br />

willSendRequest:(NS<strong>URL</strong>Request *)request<br />

redirectResponse:(NS<strong>URL</strong>Response *)redirectResponse<br />

#else // FOR_NS<strong>URL</strong>DOWNLOAD<br />

-(NS<strong>URL</strong>Request *)download:(NS<strong>URL</strong>Connection *)connection<br />

willSendRequest:(NS<strong>URL</strong>Request *)request<br />

redirectResponse:(NS<strong>URL</strong>Response *)redirectResponse<br />

#endif<br />

{<br />

NS<strong>URL</strong>Request *newRequest = request;<br />

if (redirectResponse) {<br />

newRequest = nil;<br />

}<br />

#if FOR_NS<strong>URL</strong>SESSION<br />

completionHandler(newRequest);<br />

#else<br />

return newRequest;<br />

#endif<br />

}<br />

2013-10-22 | Copyright © 2003, 2013 <strong>Apple</strong> Inc. All Rights Reserved.<br />

51

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!