我正在使用此代码下载许多 pdf 文件
for (int i=0; i<[myBooks count]; i++) {
Book_own *temp= (Book_own *)[myBooks objectAtIndex:i]; // myBooks is a mutable array of object Book_own
// to download pdf
NSString *documentName = [temp.bo_path stringByDeletingPathExtension];
NSString *pdfLink = [NSString stringWithFormat:@"http://url.com/files/%@",temp.bo_path];
NSString *linkWithoutSpaces = [pdfLink stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
NSString *urlString = linkWithoutSpaces;
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:url];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = [documentPaths objectAtIndex:0];
NSLog(@"in settings, Document Directory: %@",documentDir);
NSString *pdfPath = [NSString stringWithFormat:@"%@/%@",documentDir,[NSString stringWithFormat:@"%@.pdf",documentName]];
NSLog(@"pdfpath: %@",pdfPath);
[data writeToFile:pdfPath atomically:YES];
NSData *tmp = [NSData dataWithContentsOfURL:url];
if (tmp != nil) {
NSError *error = nil;
[tmp writeToFile:pdfPath options:NSDataWritingAtomic error:&error];
if (error != nil) {
NSLog(@"Failed to save the file: %@", [error description]);
} else {
NSLog(@"downloaded");
}
} else {
NSLog(@"fail to save pdf file");
}
}
这会为我下载文件,但它让我等待了很长时间,我想添加 activityIndicator 或进度条来显示下载进度。但我是 iPhone 的新手,我不知道该怎么做。谁能帮我?