从iOS移动应用中的服务器下载大量内容。 用于按照NSUrlConnectionDelegate方法下载内容。 //我为连接委托方法实现的代码
-(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];
}
注意:
例如,如果进程正在进行,当网络已断开连接或网络速度很慢时,下载过程已停止 这里,如何进行简历下载过程
我在单独的视图控制器类中实现了所有这些功能。 当应用处于后台模式时,让我了解实现相同功能的更多细节。 下载功能过程应该继续,直到文件下载过程完成。
答案 0 :(得分:1)
NSURLConnection不提供暂停和恢复下载的功能,而是使用NSURLSession,它提供了恢复,暂停和取消下载过程的功能。
[yourTask suspend] // to pause the task.
[yourTask resume]; // to resume the task.
[yourTask cancel]; // to cancel the task.
以下是NSURLSession的重要链接。