gpt4 book ai didi

ios - 如何以编程方式在 iOS 的恢复模式下从服务器下载内容

转载 作者:行者123 更新时间:2023-11-29 02:09:26 25 4
gpt4 key购买 nike

我正在从 iOS 移动应用中的服务器下载大量内容。要下载内容,请遵循 NSUrlConnectionDelegate 方法。//我为连接委托(delegate)方法实现的代码

-(void)downloadWithNsurlconnection{
dirToCreate = [NSString stringWithFormat:@"%@/%@",[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],rootName];
_fileMangr= [NSFileManager defaultManager];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:zipFileAtUrl cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
receivedData = [[NSMutableData alloc] initWithLength:0];
NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
}
- (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;
[_updatingProgress setProgress:progressive];
// NSLog(@"Progress... %.2f",progressive);
}
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
self.updatingProgress.progress = 1.0;
NSString *dataFileName = nil;
NSString *urlString = [NSString stringWithString:[zipFileAtUrl absoluteString]];
NSArray *dataLinkArr = [urlString componentsSeparatedByString:@"/"];
dataFileName = [dataLinkArr lastObject];
// dispatch_async(dispatch_get_main_queue(), ^{
BOOL isDir=YES;
NSError *error1 = nil;

if (zipFileAtUrl!=nil) {

}
else{
NSLog(@"Files are not available in server");
}
if(![_fileMangr fileExistsAtPath:dirToCreate isDirectory:&isDir])
{
if(![_fileMangr createDirectoryAtPath:dirToCreate withIntermediateDirectories:YES attributes:nil error:&error1])
{
}
else{
NSString *localFilePath = [dirToCreate stringByAppendingPathComponent:dataFileName];
if ([receivedData writeToFile:localFilePath atomically:YES]) {
NSLog(@"data file downloaded in directory");
}
else{
NSLog(@"failed dat downloading");
}
}
}
else
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:rootName];
NSError *error1;
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error1];
NSString *localFilePath = [dataPath stringByAppendingPathComponent:dataFileName];
if ([receivedData writeToFile:localFilePath atomically:YES]) {
NSLog(@"data file downloaded");
}
else{
NSLog(@"Not save in documetns folder");
}
}
[self extractSingleActivityAtindex:activityIndx];
}

注意:

例如,如果进程正在进行,当网络断开或网络缓慢时,下载进程已停止在这里,如何进行简历下载过程

我在单独的 View Controller 类中实现了所有这些功能主义者。当应用程序处于后台模式时,让我知道实现相同功能的更多细节。下载功能过程应继续,直到文件下载过程完成。

最佳答案

NSURLConnection不提供暂停和恢复下载的功能,而是使用NSURLSession,它提供恢复、暂停和取消下载过程的功能。

[yourTask suspend] // to pause the task.
[yourTask resume]; // to resume the task.
[yourTask cancel]; // to cancel the task.

这是NSURLSession 的重要链接.

关于ios - 如何以编程方式在 iOS 的恢复模式下从服务器下载内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29443301/

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