Downloading a Large File - iPhone SDK

前端 未结 3 1210
感动是毒
感动是毒 2020-11-29 16:13

I am using Erica Sadun\'s method of Asynchronous Downloads (link here for the project file: download), however her method does not work with files that have a big size (50 m

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-29 17:15

    Try AFNetworking. And:

    NSString *yourFileURL=@"http://yourFileURL.zip";
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:yourFileURL]];
    AFURLConnectionOperation *operation =   [[AFHTTPRequestOperation alloc] initWithRequest:request];
    
    NSString *cacheDir = [NSSearchPathForDirectoriesInDomains
                              (NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filePath = [cacheDir stringByAppendingPathComponent:
                          @"youFile.zip"];
    
    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];
    
    [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
       //show here your downloading progress if needed
    }];
    
    [operation setCompletionBlock:^{
        NSLog(@"File successfully downloaded");
    }];
    
    [operation start];
    

提交回复
热议问题