提前注釋:下載方法出處在哪,我就不得而知了,不得而知了
我是復(fù)制項目里的代碼,陳年往事不在查出處了,
看到的伙伴如果看到了原文,請聯(lián)系我,我及時注明,??
-------分隔線----------
文章里的代碼塊,可直接復(fù)制到程序里使用
增加如下代理:
NSURLSessionDownloadDelegate,UIWebViewDelegate,UIDocumentInteractionControllerDelegate
/**
* 下載任務(wù)
*/
@property (nonatomic, strong) NSURLSessionDownloadTask* downloadTask;
/**
* resumeData記錄下載位置
*/
@property (nonatomic, strong) NSData* resumeData;
/**
* session
*/
@property (nonatomic, strong) NSURLSession* session;
源生session網(wǎng)絡(luò)請求,點擊方法里寫??這段代碼,開始下載文件
//分解下載,大文件時使用
NSURL* url = [NSURL URLWithString:noteModel.contractFileUrl];
// 創(chuàng)建任務(wù)
self.downloadTask = [self.session downloadTaskWithURL:url];
// 開始任務(wù)
[self.downloadTask resume];
session的懶加載
- (NSURLSession *)session
{
if (nil == _session) {
NSURLSessionConfiguration *cfg = [NSURLSessionConfiguration defaultSessionConfiguration];
self.session = [NSURLSession sessionWithConfiguration:cfg delegate:self delegateQueue:[NSOperationQueue mainQueue]];
}
return _session;
}
下載完畢會調(diào)用NSURLSessionDownloadDelegate
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location
{
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
// response.suggestedFilename : 建議使用的文件名,一般跟服務(wù)器端的文件名一致
NSString *file = [caches stringByAppendingPathComponent:downloadTask.response.suggestedFilename];
// 將臨時文件剪切或者復(fù)制Caches文件夾
NSFileManager *mgr = [NSFileManager defaultManager];
// AtPath : 剪切前的文件路徑
// ToPath : 剪切后的文件路徑
//location 文件臨時地址
[mgr moveItemAtPath:location.path toPath:file error:nil];
[Utils hideLoding:self.view];
/*
// 提示下載完成
[[[UIAlertView alloc] initWithTitle:@"下載完成" message:downloadTask.response.suggestedFilename delegate:self cancelButtonTitle:@"知道了" otherButtonTitles: nil] show];
*/
//文件路徑
NSString *cachePath = file;
/*
/var/mobile/Containers/Data/Application/--/Library/Caches/888.docx
下面這段代碼是調(diào)起預(yù)覽
*/
UIDocumentInteractionController *documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:cachePath]];
documentController.delegate = self;
//直接顯示預(yù)覽
[documentController presentPreviewAnimated:YES];
//顯示包含預(yù)覽的菜單項
//[documentController presentOptionsMenuFromRect:CGRectZero inView:self.view animated:YES];
//顯示不包含預(yù)覽菜單項
[documentController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}
在這里面監(jiān)聽下載進(jìn)度,totalBytesWritten/totalBytesExpectedToWrite
每次寫入沙盒完畢調(diào)用
bytesWritten 這次寫入的大小
totalBytesWritten 已經(jīng)寫入沙盒的大小
totalBytesExpectedToWrite 文件總大小
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten
totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
// NSLog((@"\n[函數(shù)名:%s]\n" "[行號:%d]\n" "%f\n"), __FUNCTION__, __LINE__,(double)totalBytesWritten/totalBytesExpectedToWrite);
}
??調(diào)用預(yù)覽的代理方法
- (UIViewController*)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController*)controller
{
return self;
}
- (UIView*)documentInteractionControllerViewForPreview:(UIDocumentInteractionController*)controller
{
return self.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController*)controller
{
return self.view.frame;
}
點擊預(yù)覽窗口的“Done”(完成)按鈕時調(diào)用
- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController*)_controller
{
}