我試圖通過HTTP請求從Web服務器上下載文件[> 40MB]。爲此,我使用了Apple提供的SimpleURLConnection示例。在這個示例中,他們只下載圖像文件,所以我修改了代碼以下載pdf文件並將其存儲在應用程序的文檔目錄中。這可以很好地下載小文件,但如果我嘗試下載大文件[> 40Mb],它只會下載6.4MB的數據。請大家幫我解決這個問題,如何在iOS下載大文件?
謝謝
FYI:
代碼寫入文件與下載的數據
- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)data
// A delegate method called by the NSURLConnection as data arrives. We just
// write the data to the file.
{
#pragma unused(theConnection)
NSInteger dataLength;
const uint8_t * dataBytes;
NSInteger bytesWritten;
NSInteger bytesWrittenSoFar;
assert(theConnection == self.connection);
dataLength = [data length];
dataBytes = [data bytes];
bytesWrittenSoFar = 0;
do {
bytesWritten = [self.fileStream write:&dataBytes[bytesWrittenSoFar] maxLength:dataLength - bytesWrittenSoFar];
assert(bytesWritten != 0);
if (bytesWritten == -1) {
[self _stopReceiveWithStatus:@"File write error"];
break;
} else {
bytesWrittenSoFar += bytesWritten;
}
} while (bytesWrittenSoFar != dataLength);
}
你有沒有調試信息?哦,'assert(theConnection == self.connection)'是如此可怕,以至於如果你使用多個連接,這段代碼註定會崩潰。使用'if(theConnection!= self.connection){return;而不是。 – 2011-06-12 14:59:14