欢迎使用CSDN-markdown编辑器

来源:互联网 发布:2016淘宝客退款佣金 编辑:程序博客网 时间:2024/06/06 00:36

import “ViewController.h”

import “DACircularProgressView.h”

@interface ViewController ()

@property (nonatomic, assign) long long totalLength;
@property (nonatomic, assign) long long currentLength;
@property (weak, nonatomic) DACircularProgressView *proView;
@property (strong, nonatomic) NSFileHandle *fileHand;
@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    }

  • (void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event {
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];

    NSURLSessionDownloadTask *downTask = [session downloadTaskWithURL:[NSURL URLWithString:@”http://localhost/05.zip“]];
    [downTask resume];
    }

  • (void)URLSession:(NSURLSession )session downloadTask:(NSURLSessionDownloadTask )downloadTask
    didFinishDownloadingToURL:(NSURL *)location {

    NSLog(@”完成下载”);

    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
    NSString *path1 = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, NO)lastObject];
    NSLog(@”path1=%@ path2=%@”, path, path1);

    NSFileManager *fileM = [NSFileManager defaultManager];
    // NSURL *url = [NSURL URLWithString:[path stringByAppendingPathComponent:downloadTask.response.suggestedFilename]];
    // [fileM moveItemAtURL:[NSURL URLWithString:location.path] toURL:[NSURL URLWithString:[path stringByAppendingPathComponent:downloadTask.response.suggestedFilename]] error:nil];
    [fileM moveItemAtPath:location.path toPath:[path stringByAppendingPathComponent:downloadTask.response.suggestedFilename] error:nil];
    }

/* Sent periodically to notify the delegate of download progress. */
- (void)URLSession:(NSURLSession )session downloadTask:(NSURLSessionDownloadTask )downloadTask
didWriteData:(int64_t)bytesWritten
totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
NSLog(@”正在下载%f”, (double)totalBytesWritten / totalBytesExpectedToWrite);
}

/* Sent when a download has been resumed. If a download failed with an
* error, the -userInfo dictionary of the error will contain an
* NSURLSessionDownloadTaskResumeData key, whose value is the resume
* data.
*/
- (void)URLSession:(NSURLSession )session downloadTask:(NSURLSessionDownloadTask )downloadTask
didResumeAtOffset:(int64_t)fileOffset
expectedTotalBytes:(int64_t)expectedTotalBytes {
NSLog(@”回复”);
}

0 0