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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using NS<strong>URL</strong>Download<br />

Displaying Download Progress<br />

}<br />

- (void)download:(NS<strong>URL</strong>Download *)download didReceiveResponse:(NS<strong>URL</strong>Response<br />

*)response<br />

{<br />

// Reset the progress, this might be called multiple times.<br />

// bytesReceived is an instance variable defined elsewhere.<br />

bytesReceived = 0;<br />

}<br />

// Store the response to use later.<br />

[self setDownloadResponse:response];<br />

- (void)download:(NS<strong>URL</strong>Download *)download didReceiveDataOfLength:(unsigned)length<br />

{<br />

long long expectedLength = [[self downloadResponse] expectedContentLength];<br />

bytesReceived = bytesReceived + length;<br />

if (expectedLength != NS<strong>URL</strong>ResponseUnknownLength) {<br />

// If the expected content length is<br />

// available, display percent complete.<br />

float percentComplete = (bytesReceived/(float)expectedLength)*100.0;<br />

NSLog(@"Percent complete - %f",percentComplete);<br />

} else {<br />

// If the expected content length is<br />

// unknown, just log the progress.<br />

NSLog(@"Bytes received - %d",bytesReceived);<br />

}<br />

}<br />

The delegate receives a download:didReceiveResponse: message before it begins receiving<br />

download:didReceiveDataOfLength: messages.<br />

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

45

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

Saved successfully!

Ooh no, something went wrong!