gpt4 book ai didi

ios - 如何在 iOS 后台模式下下载文件?和网络连接丢失

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:20:29 28 4
gpt4 key购买 nike

在我的应用程序中,我从服务器下载音频文件,当应用程序处于前台时,当我单击主页按钮或锁定按钮强制应用程序进入后台时,文件下载正常,然后过了一段时间,下载停止,错误出现为 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 iOS 应用程序编程指南 的部分,其中描述了后台下载。另请参阅 What’s New in Foundation Networking 中的 WWDC 2013 视频(稍后将在视频中介绍)。

关于ios - 如何在 iOS 后台模式下下载文件?和网络连接丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38243333/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com