在我的應用程序中,我從服務器上下載音頻文件,並且當應用程序處於前臺並且當我單擊主頁按鈕或鎖定按鈕強制應用程序轉到後臺時,有一段時間,下載停止並且錯誤爲1005 network connection lost
。有什麼問題?任何人都可以解釋這個問題嗎?如何以iOS的背景模式下載文件?和網絡連接丟失
代碼:
NSURL *url = [NSURL URLWithString:currentURL];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
receivedData = [[NSMutableData alloc] initWithLength:0];
NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
myConnection = connection;
NSLog(@"%@ Download Started", currentURL);
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[receivedData setLength:0];
expectedBytes = [response expectedContentLength];
}
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[receivedData appendData:data];
float progressive = (float)[receivedData length]/(float)expectedBytes;
[downloadProgressView setProgress:progressive];
NSInteger val = progressive*100;
downloadpercentageLabel.text = [NSString stringWithFormat:@"%ld%@",(long)val,@"%"];
//[UIApplication sharedApplication].idleTimerDisabled = YES;
}
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
你是否檢查背景模式下的背景獲取? –
這可能是重複的。你檢查了這個問題:http://stackoverflow.com/questions/8861390/ios-background-downloads-when-the-app-is-not-active? –
@MehmetEfeAkça我已經嘗試了幾個答案,但沒有解決,並且找不到答案。請幫助 –