首页 > 代码库 > iOS AFNetWorking 下载pdf文档
iOS AFNetWorking 下载pdf文档
+ (void)downLoadPdf:(NSString *)url pdf_id:(NSString *)pdf_id block:(APIFilePath)pdfFilePath {
NSMutableDictionary *mPdf_dic = [NSMutableDictionary dictionaryWithDictionary:[Tool getLocalKey:PDFLOCAL_DIC]];
NSURL *URL = [NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
NSLog(@"###%@", targetPath); //原始文件
NSLog(@"**%@", [response suggestedFilename]); //文件名
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
NSLog(@"File downloaded to: %@", filePath);//下载成功后本地保存id以及文件路径
NSString *stringUrl = [filePath absoluteString];
mPdf_dic[pdf_id] = stringUrl;
NSDictionary *pdfDic = [NSDictionary dictionaryWithDictionary:mPdf_dic];
[Tool saveLocalvalue:pdfDic key:PDFLOCAL_DIC];
pdfFilePath(stringUrl);
}];
[downloadTask resume];
NSMutableDictionary *mPdf_dic = [NSMutableDictionary dictionaryWithDictionary:[Tool getLocalKey:PDFLOCAL_DIC]];
NSURL *URL = [NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
NSLog(@"###%@", targetPath); //原始文件
NSLog(@"**%@", [response suggestedFilename]); //文件名
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
NSLog(@"File downloaded to: %@", filePath);//下载成功后本地保存id以及文件路径
NSString *stringUrl = [filePath absoluteString];
mPdf_dic[pdf_id] = stringUrl;
NSDictionary *pdfDic = [NSDictionary dictionaryWithDictionary:mPdf_dic];
[Tool saveLocalvalue:pdfDic key:PDFLOCAL_DIC];
pdfFilePath(stringUrl);
}];
[downloadTask resume];
}
*********************************************************************************************
查看文档时判断本地是否已经下载
#pragma mark - read
- (void)read {
//本地已下载pdf文件
NSDictionary *pdfDic = [Tool getLocalKey:PDFLOCAL_DIC];
if (pdfDic == nil) {
[Tool saveLocalvalue:pdfDic key:PDFLOCAL_DIC];
}
//文件路径
NSString *filePathLocal;
NSString *fileUrl = @"pdf的URL";
NSString *fileKey = @"1471509244417_1471509259460";
NSArray *keys = [pdfDic allKeys];
BOOL isDownload = NO; //是否已经下载
for (NSString *key in keys) {
if ([key isEqualToString:fileKey]) {
isDownload = YES;
break;
} else {
isDownload = NO;
}
}
if (isDownload) {
filePathLocal = pdfDic[fileKey];
[self openPdf:filePathLocal];
} else {
[WYAPI downLoadPdf:fileUrl pdf_id:fileKey block:^(NSString *filePath) {
[self openPdf:filePath];
}];
}
- (void)read {
//本地已下载pdf文件
NSDictionary *pdfDic = [Tool getLocalKey:PDFLOCAL_DIC];
if (pdfDic == nil) {
[Tool saveLocalvalue:pdfDic key:PDFLOCAL_DIC];
}
//文件路径
NSString *filePathLocal;
NSString *fileUrl = @"pdf的URL";
NSString *fileKey = @"1471509244417_1471509259460";
NSArray *keys = [pdfDic allKeys];
BOOL isDownload = NO; //是否已经下载
for (NSString *key in keys) {
if ([key isEqualToString:fileKey]) {
isDownload = YES;
break;
} else {
isDownload = NO;
}
}
if (isDownload) {
filePathLocal = pdfDic[fileKey];
[self openPdf:filePathLocal];
} else {
[WYAPI downLoadPdf:fileUrl pdf_id:fileKey block:^(NSString *filePath) {
[self openPdf:filePath];
}];
}
}
iOS AFNetWorking 下载pdf文档
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。