如何在后台模式下下载文件iOS?和网络连接丢失
How to download files in background mode iOS? And Network connection Lost
在我的应用程序中,我从服务器下载音频文件,当应用程序处于前台时,当我单击主页按钮或锁定按钮强制应用程序进入后台时,文件下载正常,然后经过一些次,下载停止,错误出现 1005 network connection lost
。有什么问题?谁能解释一下这个问题?
代码:
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;
}
使用背景NSURLSession
。它可以处理网络中断和超过 3 分钟的下载。请参阅 Downloading Content in the Background section of The App Programming Guide for iOS, which describes background downloads. Also refer to WWDC 2013 video in What’s New in Foundation Networking(稍后在视频中介绍)。
在我的应用程序中,我从服务器下载音频文件,当应用程序处于前台时,当我单击主页按钮或锁定按钮强制应用程序进入后台时,文件下载正常,然后经过一些次,下载停止,错误出现 1005 network connection lost
。有什么问题?谁能解释一下这个问题?
代码:
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;
}
使用背景NSURLSession
。它可以处理网络中断和超过 3 分钟的下载。请参阅 Downloading Content in the Background section of The App Programming Guide for iOS, which describes background downloads. Also refer to WWDC 2013 video in What’s New in Foundation Networking(稍后在视频中介绍)。