由于您的 PDF 文件太大,所以如果您进行同步下载,下载时间会太长,所以我坚持您创建一个异步下载器并使用它。我已经放了相同的代码。
第 1 步:创建文件“FileDownloader.h”
#define FUNCTION_NAME NSLog(@"%s",__FUNCTION__)
#import <Foundation/Foundation.h>
@protocol fileDownloaderDelegate <NSObject>
@optional
- (void)downloadProgres:(NSNumber*)percent forObject:(id)object;
@required
- (void)downloadingStarted;
- (void)downloadingFinishedFor:(NSURL *)url andData:(NSData *)data;
- (void)downloadingFailed:(NSURL *)url;
@end
@interface FileDownloader : NSObject
{
@private
NSMutableURLRequest *_request;
NSMutableData *downloadedData;
NSURL *fileUrl;
id <fileDownloaderDelegate> delegate;
double totalFileSize;
}
@property (nonatomic, strong) NSMutableURLRequest *_request;
@property (nonatomic, strong) NSMutableData *downloadedData;
@property (nonatomic, strong) NSURL *fileUrl;
@property (nonatomic, strong) id <fileDownloaderDelegate> delegate;
- (void)downloadFromURL:(NSString *)urlString;
@end
第 2 步:使用 FileDownloader.m 创建一个 .m 文件
#import "FileDownloader.h"
@implementation FileDownloader
@synthesize _request, downloadedData, fileUrl;
@synthesize delegate;
- (void)downloadFromURL:(NSString *)urlString
{
[self setFileUrl:[NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
self._request = [NSMutableURLRequest requestWithURL:self.fileUrl cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0f];
NSURLConnection *cn = [NSURLConnection connectionWithRequest:self._request delegate:self];
[cn start];
}
#pragma mark - NSURLConnection Delegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
if([delegate respondsToSelector:@selector(downloadingStarted)])
{
[delegate performSelector:@selector(downloadingStarted)];
}
totalFileSize = [response expectedContentLength];
downloadedData = [NSMutableData dataWithCapacity:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[downloadedData appendData:data];
if([delegate respondsToSelector:@selector(downloadProgres:forObject:)])
{
[delegate performSelector:@selector(downloadProgres:forObject:) withObject:[NSNumber numberWithFloat:([downloadedData length]/totalFileSize)] withObject:self];
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
if([delegate respondsToSelector:@selector(downloadingFailed:)])
{
[delegate performSelector:@selector(downloadingFailed:) withObject:self.fileUrl];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if([delegate respondsToSelector:@selector(downloadingFinishedFor:andData:)])
{
[delegate performSelector:@selector(downloadingFinishedFor:andData:) withObject:self.fileUrl withObject:self.downloadedData];
}
}
@end
第 3 步:导入文件#import "FileDownloader.h"
并fileDownloaderDelegate
在您的 viewController 中
第 4 步:在 viewCONtroller 的 .m 文件中定义以下委托方法
- (void)downloadingStarted;
- (void)downloadingFinishedFor:(NSURL *)url andData:(NSData *)data;
- (void)downloadingFailed:(NSURL *)url;
第 5 步:创建 FileDownloader 的对象并将 URL 设置为下载它。
FileDownloader *objDownloader = [[FileDownloader alloc] init];
[objDownloader setDelegate:self];
[objDownloader downloadFromURL:@"Your PDF Path URL here];
第 6 步:将文件保存在
- (void)downloadingFinishedFor:(NSURL *)url andData:(NSData *)data;
方法中所需的位置。