?btn.userInteractionEnabled = NO;
?? ? ? ? _urlStr = @"http://www.hudiezc.com/admin-test/ios%E5%BC%80%E5%8F%91.pdf";
? ? ? ? /* 創(chuàng)建網絡下載對象 */
? ? ? ? AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
? ? ? ? /* 下載地址 */
? ? ? ? NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:_urlStr]];
? ? ? ?/* 下載路徑 */
? ? ? ? NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
? ? ? ? NSString*filePath = [pathstringByAppendingPathComponent:_urlStr.lastPathComponent];
? ? ? ?/* 開始請求下載 */
? ? ? ? NSURLSessionDownloadTask*downloadTask = [managerdownloadTaskWithRequest:requestprogress:^(NSProgress*_NonnulldownloadProgress) {
// 主線程里修改進度條的值?
? ? ? ? ? ?dispatch_async(dispatch_get_main_queue(), ^{
// 修改進度條的值
? ? ? ? ? ? ? ? self.progress.progress= downloadProgress.fractionCompleted;
? ? ? ? ? ? ? ? if(downloadProgress.fractionCompleted==1) {
//? ? ? ? ? ? ? ? ? ? 證明下載完成了
? ? ? ? ? ? ? ? ? ? for(UIView*viewin[self.downBtnsubviews])
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? [viewremoveFromSuperview];
? ? ? ? ? ? ? ? ? ? }
// 更改按鈕的內容
? ? ? ? ? ? ? ? ? ? [self.downBtnaddSubview:[selflabelTitle:@"點擊分享"fontSize:17.0color:[UIColorwhiteColor] CGFloatX:35*W_RATIOCGFloatY:14*H_RATIOCGFloatW:70*W_RATIOCGFloatH:18*H_RATIOtextAlignment:@""]];
? ? ? ? ? ? ? ? ? ? self.downBtn.tag=1;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? });
? ? ? ? ? ? NSLog(@"下載進度:%.0f%", downloadProgress.fractionCompleted*100);
? ? ? ? ? ? self.completed= downloadProgress.fractionCompleted;
? ? ? ? }destination:^NSURL*_Nonnull(NSURL*_NonnulltargetPath,NSURLResponse*_Nonnullresponse) {
? ? ? ? ? ? //? ? ? ? return targetPath;
? ? ? ? ? ? /* 設定下載到的位置 */
? ? ? ? ? ? self.downAddress= filePath;
? ? ? ? ? ? NSLog(@"下載地址:%@", [NSURLfileURLWithPath:filePath]);
? ? ? ? ? ? return[NSURLfileURLWithPath:filePath];
? ? ? ? }completionHandler:^(NSURLResponse*_Nonnullresponse,NSURL*_NullablefilePath,NSError*_Nullableerror) {
? ? ? ? ? ? NSLog(@"下載完成");
// 主線程里 把下載好的鏈接存起來
? ? ? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{
? ? ? ? ? ? ? ? [udsetObject:self.downAddressforKey:self.fileKey];
? ? ? ? ? ? ? ? btn.userInteractionEnabled=YES;
? ? ? ? ? ? });
? ? ? ? }];
? ? ? ? [downloadTaskresume];
// ?懶加載progress
-(UIProgressView*)progress {
? ? if(!_progress) {
? ? ? ? //進度條高度不可修改
? ? ? ? _progress= [[UIProgressViewalloc]initWithFrame:CGRectMake(0,182*H_RATIO,135*W_RATIO,45*H_RATIO)];
? ? ? ? _progress.transform = CGAffineTransformMakeScale(1.0f, 22.5*H_RATIO);
//? ? ? ? 已經完成的進度
? ? ? ? _progress.progressTintColor = Tab_COLOR;
//? ? ? ? 沒有完成的進度
? ? ? ? _progress.trackTintColor = [UIColor whiteColor];
? ? ? ? _progress.progressViewStyle = UIProgressViewStyleDefault;
? ? }
? ? return _progress;
}