在我的应用程序中,我从服务器上下载音频文件,并且当应用程序处于前台并且当我单击主页按钮或锁定按钮强制应用程序转到后台时,有一段时间,下载停止并且错误为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我已经尝试了几个答案,但没有解决,并且找不到答案。请帮助 –